Posts Tagged ‘C#’

h1

Remove invalid characters from file name and path

February 4, 2015

Code :

string invalid = new string(Path.GetInvalidFileNameChars()) + new string(Path.GetInvalidPathChars());

foreach (char c in invalid)
{
strFileName = strFileName.Replace(c.ToString(), “”);
}

Advertisements
h1

Convert Date to any format from any other format

June 30, 2012

Convert a one date format to any other date format.
Date format must not be a regular Date format like ‘MM/dd/yyyy’. it can be a any format like ‘yyyyMMdd’

Coding :

public string DateFormatter(string Date,string RevFormat,string ReqFormat)
{
DateTime MyDateConverted = DateTime.ParseExact(Date, RevFormat, System.Globalization.CultureInfo.InvariantCulture);
string ReqDate = MyDateConverted.ToString(@ReqFormat);
return ReqDate;
}
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’
}
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
}