[WebMethod]
public string Login(string Username, string Password)
{
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString);
con.Open();
SqlCommand cmd = new SqlCommand("SELECT Username, Password FROM UserData where Username = @Username AND Password = @Password ", con);
cmd.Parameters.AddWithValue("@UserName", Username);
cmd.Parameters.AddWithValue("@Password", Password);
cmd.ExecuteNonQuery();
SqlDataAdapter da = new SqlDataAdapter(cmd);
// Create an instance of DataSet.
DataSet ds = new DataSet();
da.Fill(ds);
con.Close();
string result = "Successful";
int count = ds.Tables[0].Rows.Count;
if (count == 1)
{
SqlConnection con1 = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString);
con1.Open();
DateTime dt = DateTime.Now;
SqlCommand cmd1 = new SqlCommand("INSERT INTO ActivityLog (CreateOn, CreateBy, UpdateOn, UpdateBy) VALUES (@CreateOn,@CreateBy, @UpdateOn, @UpdateBy ", con1);
cmd.Parameters.AddWithValue("@CreateOn", dt);
cmd.Parameters.AddWithValue("@CreateBy", Username);
cmd.Parameters.AddWithValue("@UpdateOn", dt);
cmd.Parameters.AddWithValue("@UpdateBy", Username);
cmd.ExecuteNonQuery();
SqlDataAdapter da1 = new SqlDataAdapter(cmd1);
// Create an instance of DataSet.
DataSet ds1 = new DataSet();
da1.Fill(ds);
con1.Close();
return result;
}
else
{
result = "Fail";
return result;
}
}
}
}
嗨,我是Web服务的新手,我正在尝试创建一个允许用户使用他们的用户名和密码登录的Web服务,如果成功,它会将访问日期时间返回到mysql数据库。 但是在调试之后,我发生了如下错误:
System.NullReferenceException:未将对象引用设置为对象的实例。
我不确定连接是否正确,似乎它没有通过我的UserData表来检查用户是否存在。我已经被困在这一个星期了,如果有人有这方面的想法,我真的很感激!