对象引用在添加到数据库时未设置为对象的实例

本文关键字:设置 对象 实例 添加 数据库 对象引用 | 更新日期: 2023-09-27 18:11:05

protected void Button1_Click(object sender, EventArgs e)
{
    SqlConnection con = new SqlConnection("Data Source=PIYUSH-PC''SQLEXPRESS;Initial Catalog=piyush;Integrated Security=True");
    SqlCommand cmd = new SqlCommand("SPRegisterUser", con);
    cmd.CommandType = CommandType.StoredProcedure;
    SqlParameter username = new SqlParameter("@Username_V", TextBox1.Text);
    SqlParameter email = new SqlParameter("@Email_V", TextBox2.Text);
    SqlParameter password = new SqlParameter("Password_V", TextBox3.Text);
    cmd.Parameters.Add(username);
    cmd.Parameters.Add(password);
    cmd.Parameters.Add(email);
    try
    {
        con.Open();
        int ReturnCode = (int)cmd.ExecuteScalar();
        if (ReturnCode == -1)
        {
            Response.Write("Username already exists");
        }
        else
        {
            Response.Redirect("WebForm2.aspx");
        }
    }
    catch (Exception e1)
    {
        Response.Write(e1);
    }
    finally
    {
        con.Close();
    }
}

上面的代码运行了,但是显示了下面的-系统。NullReferenceException:对象引用没有设置为对象的实例。在eGaffar_23_6_2014_.WebForm1。Button1_Click(对象发送者,EventArgs e)

对象引用在添加到数据库时未设置为对象的实例

第三个参数必须是@ password_v,因为在传递值的参数时,还需要添加@