private void button1_Click(object sender, EventArgs e)
{
string name;
name = textBox5.Text;
SqlConnection con10 = new SqlConnection("con strn");
SqlCommand cmd10 = new SqlCommand("select * from sumant where username=@name");
cmd10.Parameters.AddWithValue("@name",name);
cmd10.Connection = con10;
cmd10.Connection.Open();//line 7
SqlDataReader dr = cmd10.ExecuteReader();
}
if ( textBox2.Text == dr[2].ToString())
{
//do something;
}
当我调试到第7行时,一切都正常,但之后
dr
抛出了一个异常:
尝试在没有数据的情况下进行读取是无效的。
我不明白为什么会出现这个异常,因为我确实在表中有用户名为sumant的数据。
请告诉我这个'if'语句是否正确,以及如何修复这个错误?