Click here to Skip to main content
15,897,704 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
hi....
how to fill the textbox values from the database at page load...
Posted
Updated 22-Oct-11 20:18pm
v2
Comments
Amir Mahfoozi 23-Oct-11 2:40am    
How do you retrieve data from database ? using an ORM or directly ?

DataSet ds;
sqldataAdapter sda=new sqldataAdapter();
sda.Fill(ds);

//your ds contain that data table at column 1

textbox1.text=ds.tables[0].rows[0].itemarray[1].tostring();
 
Share this answer
 
 
Share this answer
 
C#
 protected void Page_Load(object sender, EventArgs e)
    {
     try
        {
            string selectSQL = "select name,gender,Designation,Deptt,Location,dob,emailid from empbirth where userid='" + txtempcode.Text + "'";
            SqlCommand cmd = new SqlCommand(selectSQL, cnn);
            SqlDataReader dr;
            cnn.Open();
            dr = cmd.ExecuteReader();
            
            if (dr.Read())
            {
                txtname.Text = dr[0].ToString();
                txtgender.Text = dr[1].ToString();
                txtdesignation.Text = dr[2].ToString();
                txtdepartment.Text = dr[3].ToString();
                txtlocation.Text = dr[4].ToString();
                txtdob.Text = dr[5].ToString();
                txtEid.Text = dr[6].ToString();

            }
        }
        catch (Exception ex)
        {
            Response.Write(ex.ToString());
        }
}
 
Share this answer
 
Comments
Bala Selvanayagam 23-Oct-11 15:54pm    
nice one
It's very simple.

On pageload event you need to assign value to it's text property value you want to set for that textbox.
 
Share this answer
 
There is loads of data all over internet for beginner level tutorials for ADO.NET and data access with C#...try it i would...

Cheers..
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900