I have a button on Form1 which has this code
// hide main form
this.Hide();
// show other form
Form2 form2 = new Form2();
form2.ShowDialog();
// close application
this.Close();
Now on Form2 load i have this code which do not show me result,although reader has values. It shows me Form2 with empty textfields
using (SqlConnection connection = new SqlConnection(connectionString))
{
string queryString = "SELECT * FROM test;";
SqlCommand command =
new SqlCommand(queryString, connection);
connection.Open();
SqlDataReader reader1 = command.ExecuteReader();
// Call Read before accessing data.
while (reader1.Read())
{
t1.Text = reader1.GetString(0);
t2.Text = reader1.GetString(1);
}
// Call Close when done reading.
reader1.Close();
}
Avoid this :
t1.Text = reader1.GetString(0);
t2.Text = reader1.GetString(1);
Don't mention the type , because when you use this query
select * from test ;
and then you change the type of your columns , you will get bad results.
So, you may try this :
t1.Text = reader1[0].ToString();
t2.Text = reader1[1].ToString();
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.