An exception of type 'System.Data.SqlClient.SqlException' occurred in System.Data.dll

前端 未结 6 1222
醉梦人生
醉梦人生 2020-12-16 04:07

When I execute my code below, this error message occurs:

"An exception of type \'System.Data.SqlClient.SqlException\' occurred in System.Data.dll bu

6条回答
  •  清歌不尽
    2020-12-16 04:31

    I think your EmpID column is string and you forget to use ' ' in your value.

    Because when you write EmpID=" + id.Text, your command looks like EmpID = 12345 instead of EmpID = '12345'

    Change your SqlCommand to

    SqlCommand cmd = new SqlCommand("SELECT EmpName FROM Employee WHERE EmpID='" + id.Text +"'", con);
    

    Or as a better way you can (and should) always use parameterized queries. This kind of string concatenations are open for SQL Injection attacks.

    SqlCommand cmd = new SqlCommand("SELECT EmpName FROM Employee WHERE EmpID = @id", con);
    cmd.Parameters.AddWithValue("@id", id.Text);
    

    I think your EmpID column keeps your employee id's, so it's type should some numerical type instead of character.

提交回复
热议问题