Getting error in visual studio while reading from mysql database [closed]

StackOverflow https://stackoverflow.com/questions/23684571

  •  23-07-2023
  •  | 
  •  

Вопрос

In the following code I try to fill the combobox

while (myReader.Read())
{
  string sName = myReader.GetString("profesor"); <--- here is the error
  cb_1najprof.Items.Add(sName);
  cb_2najprof.Items.Add(sName);
  cb_3najprof.Items.Add(sName);
  cb_1najsprof.Items.Add(sName);
  cb_2najsprof.Items.Add(sName);
  cb_3najsprof.Items.Add(sName);
}

When the code above is run I get the error cannot convert string to int on the indicated line.

Это было полезно?

Решение

You can try this..

string sName = myReader.GetString(myReader.GetOrdinal("profesor"));

or

string sName = myReader["profesor"].ToString();

Also see related question

How to get data by SqlDataReader.GetValue by column name

Другие советы

GetString takes an integer as an argument, not a string. You have called it with "profesor". Try calling it with eg 1 instead. This integer corresponds to the column you want to read from.

That's because you can't pass a string to the .GetString() method [documentation] You have to pass an int. The int (zero-based) is the column number you want to read.

Yes I know my documentation link is for a SqlDataReader, but the same concept applies for a MySqlDataReader

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top