0

This is the code.

void ProdFill()
{
    string coninfo = "datasource=DESKTOP-1F4329L;port=3306;username=root;password=root";
    string query = "select * from prodTest.product;";
    SqlConnection conn = new SqlConnection(coninfo);
    SqlCommand cmdDb = new SqlCommand(query, conn);
    SqlDataReader myReader;

    try
    {
        conn.Open();
        myReader = cmdDb.ExecuteReader();
        while(myReader.Read())
        {
            string prodname = myReader.GetString("pname");
            comboBox2.Items.Add(prodname);
        }
    }
}

It's giving an error at:

string prodname = myReader.GetString("pname"); 

stating that

it has invalid arguments. Cannot convert from string to int.

croxy
  • 4,082
  • 9
  • 28
  • 46

2 Answers2

0

Pass column index inside GetString()

Nody
  • 85
  • 9
0

You can try with following approaches

If you want to get data by column name then you can go with

string prodname = myReader["pname"].ToString();

If you know index of column then you can go with

   string prodname = myReader.GetString(0); //you can replace 0 with index of column

After getting data from myReader, you can add it to combobox

comboBox2.Items.Add(prodname);
Prasad Telkikar
  • 15,207
  • 5
  • 21
  • 44