You can try this..
string sName = myReader.GetString(myReader.GetOrdinal("profesor"));
or
string sName = myReader["profesor"].ToString();
Also see related question
Pregunta
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.
Solución
You can try this..
string sName = myReader.GetString(myReader.GetOrdinal("profesor"));
or
string sName = myReader["profesor"].ToString();
Also see related question
Otros consejos
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