我正在编写 Web 服务方法来检索用户的数据。为了清楚起见,我的数据库中有一个名为 User 的表,其中包含 ID、fName、lName、emailAddress、用户名、密码、reg_ID。如果我使用 ID 来检索数据,它会起作用,但是如果我在“where”子句中使用 emailAddress,它会给我以下错误。
网络服务方法
[WebMethod(Description = "Returns Details of User with username")]
public DataSet GetUser(string user)
{
DataSet dataSet = new DataSet();
OleDbConnection oleConn = new OleDbConnection(connString);
try
{
oleConn.Open();
string sql = "SELECT * FROM [User] WHERE [username]=" + user;
OleDbDataAdapter dataAdapter = new OleDbDataAdapter(sql, oleConn);
dataAdapter.Fill(dataSet, "User");
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
}
finally
{
oleConn.Close();
}
if (dataSet.Tables["User"].Rows.Count <= 0)
return null;
return dataSet;
}
错误
System.NullReferenceException: Object reference not set to an instance of an object.
at UserManagement.UserRegistration.GetUser(String user) in C:\Users\smartamrit\Desktop\SystemSoftware\UserManagement\UserRegistration.asmx.cs:line 170
第 170 行开始于
if (dataSet.Tables["User"].Rows.Count <= 0
)