-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
  • 148,824
  • 21
  • 237
  • 306
Ali Raza
  • 124
  • 1
  • 1
  • 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
  • 100,656
  • 22
  • 201
  • 249