Click here to Skip to main content
15,917,709 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a asp.net page containing 20 textboxes, the data that should be populated to these textboxes are availabe in Sql database

How would you display the data from the DB to the textboxes?

I can setup the connection and open it and retrieve all the data from a specific row using SELECT * FROM RegDB where ([Id] = something)

Any simple way if displaying the data in the textboxes?


I know below my code will work.. but i need simple way to get the data from sql and assign to textbox

C#
using (SqlConnection con1 = new SqlConnection("Data Source=.\SQLExpress;Initial Catalog=tempDB;Integrated Security=True"))
        {
            DataTable dt = new DataTable();
            con1.Open();
            SqlDataReader myReader = null;
            SqlCommand myCommand = new SqlCommand("SELECT * FROM RegDB where ([Id] = something)'", con1);

            myReader = myCommand.ExecuteReader();

            while (myReader.Read())
            {
                TextBox1.Text = myReader[0].ToString();
                TextBox2.Text = myReader[1].ToString();
                TextBox3.Text = myReader[2].ToString();
                TextBox4.Text = myReader[3].ToString();
                TextBox5.Text = myReader[4].ToString();
            }
            con1.Close();
        }
Posted
Updated 6-Jan-14 20:43pm
v2

C#
int i = 0;
while (myReader.Read())
{
    (Page.FindControl("TextBox" + (i+1).ToString()) as TextBox).Text = myReader[i].ToString();
    i = i + 1;
}

Happy Coding!
:)
 
Share this answer
 
Comments
Karthik_Mahalingam 7-Jan-14 3:32am    
5,yours and mine almost same :)
i have used adaptor, urs reader.
Try like this.
if this is your exact need, then this will help you.

C#
SqlDataAdapter da = new SqlDataAdapter(myCommand);
                da.Fill(dt);
                if (dt.Rows.Count == 1)
                {
                    DataRow row = dt.Rows[0];
                    for (int i = 0; i < 20; i++)
                    {
                        string value = row[i] + "";
                        (Page.FindControl("TextBox" + i) as TextBox).Text = value; ;
                    }
                }
 
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