Sql Data Srouce Control Fetch data from database

Hi in asp.net 2.0 this new datasource control has been added and i liked it alot and i used it on many places so i thought it would be nice if I share my knowledge with all my readers so first of all i would just like to show you how to retrieve some data from your database into C# code behind file.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
string userID = string.Empty;

string UserName="najam";
SqlDataSource myDbSource = new SqlDataSource();
myDbSource.ConnectionString = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
myDbSource.SelectCommand = "select userId from Users where userName='" + UserName + "'";
myDbSource.ProviderName = "System.Data.SqlClient";
DataView dv = (DataView)myDbSource.Select(new DataSourceSelectArguments());
DataTable dt = dv.Table;
if (dt.Rows.Count > 0)
{
userID = dt.Rows[0][0].ToString();
}

Above code will return user id if username supplied. You will see no data adapters or command object.

If you like instead of creating sql datasrouce control in code behind just drag and drop it onto ur aspx page and then in your code behind reference it that will shorten the line of code for fetching data from your database.

comments powered by Disqus