tags:

views:

137

answers:

4

i have one database, and it contains some columns. My requirement is that how to display each of the data that i stored in the databse on a text box? my code is shown below (after the connection string)

conn.Open();
mycommnd.ExecuteScalar();
SqlDataAdapter da = new SqlDataAdapter(mycommnd);
DataTable dt = new DataTable();
da.Fill(dt);

What changes that i make after da.Fill(dt) for displaying data on the text box.

A: 

You need to loop inside each columns in the DataTable to get the values and then concatenate them into a string and assign it to a textbox.Text property

        DataTable dt = new DataTable();
        TextBox ResultTextBox;

        StringBuilder result = new StringBuilder();

        foreach(DataRow dr in dt.Rows)
        {
            foreach(DataColumn dc in dt.Columns)
            {
                result.Append(dr[dc].ToString());
            }
        }

        ResultTextBox.Text = result.ToString();
hadi teo
A: 

One form of using the ExecuteScalar:

textBox.Text = mycommnd.ExecuteScalar().ToString();
Aragorn
+2  A: 

Something like:

textBox1.Text = dt.Rows[0].ItemArray[0].ToString();

Depends on the name of your textbox and which value you want to put into that text box.

Aamir
A: 

The DataTable consists of rows and columns, that you can reach in some different ways:

// get value from first row, first column
myTextBox.Text = dt.Rows[0][0]; 
// get value from first row, column named "First",
myTextBox.Text = dt.Rows[0]["First"];
Fredrik Mörk