Archive for June, 2009

Comma delimeter split in C#

13/06/2009

I was given a text file where I need to separate the each email ids from the grouped text.
Each email ids were identified by the character comma(,) delimeter.

My solution resides in single line of code thats GetString method of ASCII class.
You can find this in System.Text namespace.

FileStream fs = new FileStream(“E:\\SLGroupFriends.txt”, FileMode.Open);
byte[] p = new byte[fs.Length];
fs.Read(p, 0, p.Length);
string r = Encoding.ASCII.GetString(p);
string[] result = r.Split(‘,’);
GridView1.DataSource = result;
GridView1.DataBind();

Retrieving multiple records in C#

08/06/2009

ExecuteDataSet method of DbCommand will returns the multiple rows from the table.

Code Behind:

Database db = DatabaseFactory.CreateDatabase();
string sql = “SELECT * From BankDetails”;
DbCommand cmd = db.GetSqlStringCommand(sql);
DataSet ds = db.ExecuteDataSet(cmd);
GridView1.DataSource = ds;
GridView1.DataBind();

My web.config was:

I simply added this line between ConfigSections tag of Configuration.

<section name=”dataConfiguration” type=”Microsoft.Practices.EnterpriseLibrary.Data.Configuration.DatabaseSettings, Microsoft.Practices.EnterpriseLibrary.Data” />

Then connectionstring tag below

<connectionStrings>
<add name=”MyFirstTest”
providerName=”System.Data.SqlClient”
connectionString=”Data Source=(local);database=BankAccount;user id=sa;pwd=murugesan”/>
</connectionStrings>
<dataConfiguration defaultDatabase=”MyFirstTest”/>