Get SQL data And show it in a text box?
23,682
Solution 1
using (var connection = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString))
using (var command = connection.CreateCommand())
{
command.CommandText = "SELECT ColumnName FROM [check]";
connection.Open();
using (var reader = command.ExecuteReader())
{
while (reader.Read())
TextBox1.Text = reader["ColumnName"].ToString();
}
}
Some comments:
- don't use
*
, specify only those fields that you need - use
using
- less code, guaranteed disposal - I assume this is a test program, otherwise it does not make sense to reset
Text
to a in the loop
Solution 2
TextBox1.Text = myReader["fieldname"].ToString();
also I think you can change while
with if
because for every row from your table you'll overwrite textbox text!
Solution 3
Try this:
TextBox1.AppendText(myReader["columnname"].ToString());
Author by
Alon M
Updated on July 09, 2022Comments
-
Alon M almost 2 years
In the last few days I am trying to get data from my SQL table and get it into my textbox.
The table name : "check".
The code I am using :
SqlDataReader myReader = null; connection = new SqlConnection(System.Configuration.ConfigurationManager .ConnectionStrings["ConnectionString"].ConnectionString); connection.Open(); var command = new SqlCommand("SELECT * FROM [check]", connection); myReader = command.ExecuteReader(); while (myReader.Read()) { TextBox1.Text = myReader.ToString(); } connection.Close();
I am getting nothing as a result. Anyone know why? Maybe I am not calling the SQL correctly?
-
Alon M almost 13 yearsi have changed the code to : protected void Button1_Click(object sender, EventArgs e) { SqlDataReader myReader = null; using (var connection = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString)) using (var command = connection.CreateCommand()) { command.CommandText = "SELECT ItemNow FROM [check]"; connection.Open(); while (myReader.Read()) TextBox1.Text = TextBox1.Text + myReader["ItemNow"].ToString(); } still not working :( thanks all !
-
Hasan Fahim almost 13 years@user796862. You've commented on a different answer. Anyways use AppendText as I specified.
-
marc_s almost 13 years+1 for your comments - every C# database developer should follow those - always!
-
Hasan Fahim almost 13 years@user796862. I don't see myReader = command.ExecuteReader(); in your last comment. Maybe this is causing issue. Anyways did you check the text present in myReader["ItemNow"].ToString() in QuickWatch? What error are you getting? Debug the code and identify the line on which you are getting error.
-
Alon M almost 13 yearsi am done debug, nothing happnd. its saying "All ok." no errors. but the textbox is empty. i done like this :myReader = command.ExecuteReader(); command.CommandText = "SELECT [ItemNow] FROM [check]"; connection.Open(); while (myReader.Read()) TextBox1.Text = TextBox1.Text +myReader["ItemNow"].ToString();
-
Hasan Fahim almost 13 years@Alon M. You need to do this: TextBox1.AppendText(myReader["ItemNow"].ToString()); Also add the value of myReader["ItemNow"].ToString() in Watch and set a break point inside the while loop where text is appended, to check what text is being appended.
-
Alon M almost 13 yearsThanks.. Its working. Ty All :).(i have done some errors in the db it self, this is the reseon it didnt work) thanks all again.