I'm trying to read a column from a data reader into a label (c# winform) My code is as follows:
SqlCommand command1 = new SqlCommand("select plant_name,plant_id from plant order by plant_id ", connection);
try
{
connection.Open();
SqlDataReader dr = command1.ExecuteReader();
while (dr.Read())
{
string plantlable = dr.GetInt32("plant_id").ToString();
labelplantid.Text = plantlable.ToString();
comboBoxplant.Items.Add(dr["plant_name"]);
}
dr.Close();
dr.Dispose();
connection.Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error);
Application.Exit();
}
I get the error "Argument 1: cannot convert from 'string' to 'int' " on the following line
string plantlable = dr.GetInt32("plant_id").ToString();
with the plant_id underlined in RED.
What am I doing wrong? I can't seem to figure it out. plant_id is a column type Int. Using Sql Server 2008 for the database.
Any hints would be appreciated thanks.
dr["plant_id"]should be used instead. See msdn.microsoft.com/en-us/library/… forGetInt32method info.