参数异常
ArgumentException
我正在制作一个简单的登录表单并收到以下异常
ArgumentException was caught. No mapping exists from object type
System.Web.UI.WebControls.TextBox to a known managed provider native
type.
这是 C# 代码
SqlConnection con = new SqlConnection("Data source=.;Initial catalog=InvoiceSystem;user id =sa;password=rfm");
SqlCommand cmd = new SqlCommand("spLogin", con);
cmd.CommandType = CommandType.StoredProcedure;
SqlParameter p1 = new SqlParameter("username", txtUsername);
SqlParameter p2 = new SqlParameter("password", txtPassword);
cmd.Parameters.Add(p1);
cmd.Parameters.Add(p2);
try
{
con.Open();
SqlDataReader rd = cmd.ExecuteReader();
if (rd.HasRows)
{
rd.Read();
Response.Redirect("Default.aspx");
}
else
{
Response.Write("<script>alert('Invalid Username/Password')</script>");
}
}
catch{
}
您尝试将 TextBox
个对象发送到存储过程:
SqlParameter p1 = new SqlParameter("username", txtUsername);
SqlParameter p2 = new SqlParameter("password", txtPassword);
它不知道如何处理这些对象。相反,只需发送包含在这些文本框中的文本:
SqlParameter p1 = new SqlParameter("username", txtUsername.Text);
SqlParameter p2 = new SqlParameter("password", txtPassword.Text);
您正在将 TextBox 本身而不是它的文本值传递给 SqlParameter。将其更改为:
SqlParameter p1 = new SqlParameter("username", txtUsername.Text);
SqlParameter p2 = new SqlParameter("password", txtPassword.Text);
您必须像这样使用 .Text
属性 提取文本:
SqlParameter p1 = new SqlParameter("username", txtUsername.Text);
SqlParameter p2 = new SqlParameter("password", txtPassword.Text);
我正在制作一个简单的登录表单并收到以下异常
ArgumentException was caught. No mapping exists from object type System.Web.UI.WebControls.TextBox to a known managed provider native type.
这是 C# 代码
SqlConnection con = new SqlConnection("Data source=.;Initial catalog=InvoiceSystem;user id =sa;password=rfm");
SqlCommand cmd = new SqlCommand("spLogin", con);
cmd.CommandType = CommandType.StoredProcedure;
SqlParameter p1 = new SqlParameter("username", txtUsername);
SqlParameter p2 = new SqlParameter("password", txtPassword);
cmd.Parameters.Add(p1);
cmd.Parameters.Add(p2);
try
{
con.Open();
SqlDataReader rd = cmd.ExecuteReader();
if (rd.HasRows)
{
rd.Read();
Response.Redirect("Default.aspx");
}
else
{
Response.Write("<script>alert('Invalid Username/Password')</script>");
}
}
catch{
}
您尝试将 TextBox
个对象发送到存储过程:
SqlParameter p1 = new SqlParameter("username", txtUsername);
SqlParameter p2 = new SqlParameter("password", txtPassword);
它不知道如何处理这些对象。相反,只需发送包含在这些文本框中的文本:
SqlParameter p1 = new SqlParameter("username", txtUsername.Text);
SqlParameter p2 = new SqlParameter("password", txtPassword.Text);
您正在将 TextBox 本身而不是它的文本值传递给 SqlParameter。将其更改为:
SqlParameter p1 = new SqlParameter("username", txtUsername.Text);
SqlParameter p2 = new SqlParameter("password", txtPassword.Text);
您必须像这样使用 .Text
属性 提取文本:
SqlParameter p1 = new SqlParameter("username", txtUsername.Text);
SqlParameter p2 = new SqlParameter("password", txtPassword.Text);