Hi, I have created a datasource to connect with SQL Data Base. It works fine when I connected it with GridView. I need to read certain item (say FirstName) and store the value to a variable. How can I use this datasource? Could you give me the statements for that?
Thanks ! but how to store particular value to the variable. For example I want to store the phone no. to variable, based on client's name given by user, in a text box on my form.
Member
1 Points
26 Posts
Reading SQLServer Database using Datasource
Sep 04, 2011 09:35 AM|kfsehgal|LINK
asp.net
Member
216 Points
119 Posts
Re: Reading SQLServer Database using Datasource
Sep 04, 2011 10:29 AM|naresh1990|LINK
You can bind that DataSource to the DataBound controls as follows
ex: Repeater
Thanx & Regards
Naresh Podishetty | MCTS | Software Engineer | India
Member
1 Points
26 Posts
Re: Reading SQLServer Database using Datasource
Sep 04, 2011 10:52 AM|kfsehgal|LINK
Thanks ! but how to store particular value to the variable. For example I want to store the phone no. to variable, based on client's name given by user, in a text box on my form.
Member
216 Points
119 Posts
Re: Reading SQLServer Database using Datasource
Sep 04, 2011 11:00 AM|naresh1990|LINK
first retrive the data from the DB table filter with the username like
LINQ query
var data = (from n in db.Tablename where n.Username==TxtUname.Text select n).FirstOrDefault();
then assign phone num to some string var as
string phno=data.Phone_num.ToString();
Thanx & Regards
Naresh Podishetty | MCTS | Software Engineer | India
All-Star
94120 Points
18123 Posts
Re: Reading SQLServer Database using Datasource
Sep 05, 2011 09:55 PM|Decker Dong - MSFT|LINK
Try this way:
using (SqlDataAdapter adapter = new SqlDataAdapter("select * from xxx where phoneid=@phoneid",new SqlConnection("Your conn str")))
{
adapter.SelectCommand.Parameters.AddWithValue("@phoneid",textbox1.Text);
DataTable dt = new DataTable();
adapter.Fill(dt);
GridView1.DataSource = dt;
GridView1.DataBind();
}
PS: You can also directly use SqlDataSource+ControlParameter to deal with the problem, see: http://www.exforsys.com/tutorials/asp.net-2.0/asp.net-2.0-gridview-filtering.html