Thursday, September 26, 2019

c# - Object reference not set to an instance of an object while adding to database

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();
}
}


The above code runs but shows the following-
System.NullReferenceException: Object reference not set to an instance of an object. at eGaffar_23_6_2014_.WebForm1.Button1_Click(Object sender, EventArgs e)

No comments:

Post a Comment

hard drive - Leaving bad sectors in unformatted partition?

Laptop was acting really weird, and copy and seek times were really slow, so I decided to scan the hard drive surface. I have a couple hundr...