retreive values from the database in asp.net using c#

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • muskan
    New Member
    • Nov 2006
    • 35

    retreive values from the database in asp.net using c#

    hello,

    I have written below code for retreiving the values from database into the textboxes only when I enter id into the textbox.
    but it doesn't show any values ino the textboxes.

    please tell how it will be done.

    da = new SqlDataAdapter( "select * from customers where customerid=" + '" + txtcustid.Text + "' ,conn);

    DataSet ds = new DataSet();
    da.Fill(ds,"cus tomers");

    for(s=0;s<ds.Ta bles[0].Rows.Count ;s++)
    {
    if( txtcustid.Text= = ds.Tables[0].Rows[s]["customerid "].ToString() )
    {
    txtcustid.Text =ds.Tables[0].Rows[s] ["customerid "].ToString();

    Txtcustname .Text =ds.Tables[0].Rows[s]["contactnam e"].ToString();
    Txtdept.Text= ds.Tables[0].Rows[s]["organisati on"].ToString();
    }
    break;
    }
  • sandeepk84
    New Member
    • Oct 2006
    • 97

    #2
    Originally posted by muskan
    hello,

    I have written below code for retreiving the values from database into the textboxes only when I enter id into the textbox.
    but it doesn't show any values ino the textboxes.

    please tell how it will be done.

    da = new SqlDataAdapter( "select * from customers where customerid=" + '" + txtcustid.Text + "' ,conn);

    DataSet ds = new DataSet();
    da.Fill(ds,"cus tomers");

    for(s=0;s<ds.Ta bles[0].Rows.Count ;s++)
    {
    if( txtcustid.Text= = ds.Tables[0].Rows[s]["customerid "].ToString() )
    {
    txtcustid.Text =ds.Tables[0].Rows[s] ["customerid "].ToString();

    Txtcustname .Text =ds.Tables[0].Rows[s]["contactnam e"].ToString();
    Txtdept.Text= ds.Tables[0].Rows[s]["organisati on"].ToString();
    }
    break;
    }
    Hi...
    i think u may have some pblm while u specify d sqlquery
    ie. while calling d SqlDataAdapter( ) constructor by passing d query...
    try like dis...

    da = new SqlDataAdapter( "select * from customers where
    customerid ='"+ txtcustid.Text + "'" ,conn);

    hope this helps...
    thanx and rgrds..
    sand...

    Comment

    • Rathorevivek
      New Member
      • Dec 2006
      • 17

      #3
      Hi,
      Here is the modified code.

      DataSet ds =new DataSet();
      SqlDataAdapter da= new SqlDataAdapter( "select * from Customers","dat abase=Test;serv er=Test;uid=tes t;pwd=test");
      da.Fill(ds);
      for(i=0;i<ds.Ta bles[0].Rows.Count;i++ ) ///or if(ds.Tables[0].Rows.Count>0)
      {
      TextBox1.Text= ds.Tables[0].Rows[0]["CustomerID "].ToString();
      TextBox2.Text=d s.Tables[0].Rows[0]["CustomerNa me"].ToString();
      TextBox3.Text=. ............... ............... ......
      }

      I had tested it and its working well.
      Hope It will help you.

      Vivek Rathore.

      Originally posted by muskan
      hello,

      I have written below code for retreiving the values from database into the textboxes only when I enter id into the textbox.
      but it doesn't show any values ino the textboxes.

      please tell how it will be done.

      da = new SqlDataAdapter( "select * from customers where customerid=" + '" + txtcustid.Text + "' ,conn);

      DataSet ds = new DataSet();
      da.Fill(ds,"cus tomers");

      for(s=0;s<ds.Ta bles[0].Rows.Count ;s++)
      {
      if( txtcustid.Text= = ds.Tables[0].Rows[s]["customerid "].ToString() )
      {
      txtcustid.Text =ds.Tables[0].Rows[s] ["customerid "].ToString();

      Txtcustname .Text =ds.Tables[0].Rows[s]["contactnam e"].ToString();
      Txtdept.Text= ds.Tables[0].Rows[s]["organisati on"].ToString();
      }
      break;
      }

      Comment

      • aaryan
        New Member
        • Nov 2006
        • 82

        #4
        Originally posted by muskan
        hello,

        I have written below code for retreiving the values from database into the textboxes only when I enter id into the textbox.
        but it doesn't show any values ino the textboxes.

        please tell how it will be done.

        da = new SqlDataAdapter( "select * from customers where customerid=" + '" + txtcustid.Text + "' ,conn);

        DataSet ds = new DataSet();
        da.Fill(ds,"cus tomers");

        for(s=0;s<ds.Ta bles[0].Rows.Count ;s++)
        {
        if( txtcustid.Text= = ds.Tables[0].Rows[s]["customerid "].ToString() )
        {
        txtcustid.Text =ds.Tables[0].Rows[s] ["customerid "].ToString();

        Txtcustname .Text =ds.Tables[0].Rows[s]["contactnam e"].ToString();
        Txtdept.Text= ds.Tables[0].Rows[s]["organisati on"].ToString();
        }
        break;
        }
        hi muskan,
        i remember reading it somewhere that if we want to filter a query this is the bad way of doing it
        [HTML]da = new SqlDataAdapter( "select * from customers where customerid=" + '" + txtcustid.Text + "' ,conn); [/HTML]
        to know much about it, you can go thro the following link. hope it will help you.

        Comment

        Working...