locked
Reading SQLServer Database using Datasource RRS feed

  • Question

  • User-300925313 posted
    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
    Sunday, September 4, 2011 9:35 AM

Answers

  • User-26034409 posted

    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();

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Sunday, September 4, 2011 11:00 AM

All replies

  • User-26034409 posted

    You can bind that DataSource to the DataBound controls as follows

    ex: Repeater

    RepeaterID.DataSource = DataSourceID;
    
    RepeaterID.DataBind();
    Sunday, September 4, 2011 10:29 AM
  • User-300925313 posted

    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.

    Sunday, September 4, 2011 10:52 AM
  • User-26034409 posted

    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();

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Sunday, September 4, 2011 11:00 AM
  • User3866881 posted

    I want to store the phone no. to variable, based on client's name given by user, in a text box on my form.

    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

    Monday, September 5, 2011 9:55 PM