-1

When I run my problem, it shows this error:

The best overloaded method match for 'System.Data.Common.DbDataReader.GetString(int)' has some invalid arguments.

My code:

SqlConnection con = new SqlConnection(ConnectionClass.connectionclass);
    SqlCommand com;
    SqlDataReader dr;
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
    {
        con.Open();
string query = "SELECT * FROM student WHERE Name = '"+comboBox1.Text+"'";
        com = new SqlCommand(query, con);
        dr = com.ExecuteReader();
        while (dr.Read())
        {
            string abc = dr.GetString("Name");

        }

    }
Patrick Hofman
  • 153,850
  • 22
  • 249
  • 325
Ali Raza
  • 174
  • 1
  • 2
  • 8

1 Answers1

3

You should supply the column ordinal number, not the column name to the SqlDataReader.GetString method:

var ordinal = dr.GetOrdinal("Name");
while (dr.Read())
{
    string abc = dr.GetString(ordinal);

}
Martin Liversage
  • 104,481
  • 22
  • 209
  • 256