Posts Tagged ‘.Net’

h1

Confirm Dialog in .Net WinForms

April 26, 2011

We can use MessageBox as Confirm Dialog box

Coding

if(MessageBox.Show(“Confirm this?”, “Test”, MessageBoxButtons.YesNo) == DialogResult.Yes)
{
/// Code for ‘Yes’
}
else
{
/// Code for ‘No’
}
Advertisements
h1

Making Non-selectable options and option groups in select

December 10, 2010

Making Non-selectable options and indented option groups are pretty easy in select control. disabled the options is not a correct way to do, use optgroup option.

Making Non-selectable options :

<select >
<option >groups</option>
<optgroup label=”group1″> </optgroup>
<option> group 1 item 1</option>
<option> group 1 item 2</option>
<optgroup label=”group2″> </optgroup>
<option> group 2 item 1</option>
<option> group 2 item 2</option>
</select>

Making indented option groups:

<select >
<option >groups</option>
<optgroup label=”group1″>
<option> group 1 item 1</option>
<option> group 1 item 2</option>
</optgroup>
<optgroup label=”group2″>
<option> group 2 item 1</option>
<option> group 2 item 2</option
>/optgroup>
</select>

 

h1

Find a ‘Value’ or ‘Text’ is Exist in ASP DropDownList

October 21, 2010

If the ‘Value’ or ‘Text’ is not Exist it throws the error. We can find that item is exist in that DropDownList or not.

Code:

ListItem Item1 = drpLst.Items.FindByValue(“value”);
//or
ListItem Item1 = drpLst.Items.FindByText(“text”); 

if (Item1 == null)
{
//Not Exist
}
else
{
//Exist
}

h1

Select distinct rows from Datatable using Linq

July 30, 2010

var distinctRows = (from DataRow dRow in dTable.Rows select new {col1=dRow[“dataColumn1”],col2=dRow[“dataColumn2”]}).Distinct();

foreach (var row in distinctRows)
{
var value1=row.col1.ToString();
var value2=row.col2.ToString();
}

don’t forget to add System.Linq Class

h1

Convert XML to Datatable

July 19, 2010

DataSet ds= new DataSet();
DataTable dtComp = new DataTable();
System.IO.StringReader rdr;
string sXML =  xmldoc.ToString();
rdr = new System.IO.StringReader(sXML);
ds.ReadXml(rdr);
if ( ds.Tables.Count > 0)
{
dtComp = ds.Tables[0];
}

Convert XML to Datatable with XML Schema : Function

private DataSet getDataSet(string sXML, string xSchema)
{
DataSet ds = new DataSet();
DataTable dt = new DataTable();
System.IO.StringReader rdr;
rdr = new System.IO.StringReader(sXML);
System.IO.StringReader rdrSchema;
rdrSchema = new System.IO.StringReader(xSchema);
ds.ReadXmlSchema(rdrSchema);
ds.ReadXml(rdr);
if (ds.Tables.Count > 0)
{
return ds;
}
return ds;
}
h1

Dynamically Writing into Web.config File

July 13, 2010

Dynamically Writing into Web.config File is not good practice because dynamically write anything in the Web.config file application will restart. Anyway you don’t bother about the application restart can do like this.

Coding:

private void SetConfigSettings()
{
string path = Server.MapPath(“Web.config”);
string newValue = “New VAlue”;
XmlDocument xDoc = new XmlDocument();
xDoc.Load(path);
XmlNodeList nodeList = xDoc.GetElementsByTagName(“appSettings”);
XmlNodeList nodeAppSettings = nodeList[0].ChildNodes;
XmlAttributeCollection xmlAttCollection = nodeAppSettings[0].Attributes;
xmlAttCollection[0].InnerXml = txtKey.Text; // key attribute
xmlAttCollection[1].InnerXml = newConnectionString; // value attribute
xDoc.Save(path); // save the web.config file
}

h1

Getting date from irregular date format:

July 12, 2010

 C# not clear about some irregular date formats like  ‘yyyyMMdd’ (20100712).  We can get date from these format of text. Use this coding to covert that dateTime format to needed date time format.

Coding:

String strDate = “20100712”; 

DateTime MyDateConverted = DateTime.ParseExact(strDate, “yyyyMMdd”, System.Globalization.CultureInfo.InvariantCulture); 

strDate = MyDateConverted.ToString(@”MMMM d, yyyy”);