I am writing web service method to retrieve the data of the user. To make clear, I have a table called User in my database, which has, ID, fName, lName, emailAddress, username, password, reg_ID. If i used the ID to retrieve the data, it will work but instead if i use emailAddress in “where” clause, it gives me following error.
Web service method
[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;
}
Error
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
Line 170 starts at
if (dataSet.Tables["User"].Rows.Count <= 0)
Did you use single quotes while passing the email id or try to use like statement when passing email id. Have you check whether any exception raises in the catch block.
The possible reason is there may occur a exception in catch block, it was suppressed and after that the if statement has executed and throwed a error that user table was not found.