0

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.

2
  • You tried to get integer value from a non-number string, which doesn't make sense. dr["plant_id"] should be used instead. See msdn.microsoft.com/en-us/library/… for GetInt32 method info. Commented Nov 3, 2016 at 2:48
  • thank you.. this worked ....labelplantid.Text= dr["plant_id"].ToString(); Commented Nov 3, 2016 at 3:08

3 Answers 3

2

The SqlDataReader.GetInt32 method takes an integer as a parameter. That integer marks the index of the field you are trying to reference. In your case, "plant_name" would be index 0 and "plant_id" would be index 1, as that is the order that you specified in the SQL query.

You are getting an error because instead of passing the index, you are treating GetInt32 as a dictionary getter and trying to access "plant_id" directly. Instead, try the following:

string plantlable = dr.GetInt32(1).ToString();

Alternatively, you can get the value directly as an object from the SqlDataReader using indexer (array) notation:

string plantlable = dr["plant_id"].ToString();
Sign up to request clarification or add additional context in comments.

1 Comment

thankyou.. this worked ... string plantlable = dr.GetInt32(1).ToString(); labelplantid.Text = plantlable.ToString();
0

By using this Line dr.GetInt32("plant_id") you are trying to read an integer value from the DataReader. and the Error message says that you are trying to convert a string to an Integer, which means that the plant_id column will be either a Text or a Varchar or something similar(not an integer) Could you please crosscheck the type?.

If so then you can try SqlDataReader.GetString method to read that value, in this case you need not to add .ToString(), th ecode will be :

  labelplantid.Text = dr.GetString("plant_id");

2 Comments

I've looked at the table and here is the create statement.. pretty sure plant_id is int CREATE TABLE [dbo].[plant]( [ID] [int] NOT NULL, [plant_id] [int] NOT NULL, [plant_name] [varchar](25) NOT NULL, CONSTRAINT [PK_plant] PRIMARY KEY CLUSTERED
BTW.. using the code you provided still results in the same error :/
0

For those looking for the answer.. here it is:

labelplantid.Text= dr["plant_id"].ToString();

or this

string plantlable = dr.GetInt32(1).ToString();
labelplantid.Text = plantlable.ToString();

either one works. Thanks for the prompt answers :)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.