0

I have no problem in local. But I get this error in uploaded website:

Object reference not set to an instance of an object.

In this link it says this is a bug with ADO but I dont use ADO in my code.

Here is my code:

        SqlConnection sc = new SqlConnection();
        sc.ConnectionString = MyConnectionString;
        sc.Open();
        SqlCommand scm = new SqlCommand(INSERT COMMAND);
        scm.Connection = sc;
        scm.ExecuteNonQuery();
        sc.Close();

I put my above code in USING statements and nothing changed.

Here is my stack trace

NullReferenceException: Object reference not set to an instance of an object.]
Register.BtnRegister_Click(Object sender, ImageClickEventArgs e) +601
System.Web.UI.WebControls.ImageButton.OnClick(ImageClickEventArgs e) +115
System.Web.UI.WebControls.ImageButton.RaisePostBackEvent(String eventArgument) +120

System.Web.UI.WebControls.ImageButton.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +10
System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +13
System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +36
System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +5563

And here is BtnRegister_Click code:

    protected void BtnRegister_Click(object sender, ImageClickEventArgs e)
    {
    if (TxtName.Text == "" || TxtFamily.Text == "" || txtNationalCode.Text == "" || TxtIdNumber.Text == "" || txtMobile.Text == "" || TxtEmail.Text == "" || txtAddress.Text == "" || TxtSecurityCode.Text == "")
    {
        LblMsg.Text = "You should fill all fields";
        return;
    }

    if (txtNationalCode.Text.Length != 10)
    {
        LblMsg.Text = "National code must have 10 digits";
        return;
    }

    if (txtMobile.Text.Length != 11)
    {
        LblMsg.Text = "Mobile number should have 11 digits";
        return;
    }

    if (T_Paticipant.GetDataByNationalCode(txtNationalCode.Text).Rows.Count > 0)
    {
        LblMsg.Text = "This national code used before";
        return;
    }

    if (T_Paticipant.GetDataByEmail(TxtEmail.Text).Rows.Count > 0)
    {
        LblMsg.Text = "This email used before";
        return;
    }

    LblMessage.Text = "Are you sure to register?";
    LblFlag.Text = "0";


    //captcha

    if (this.Session["CaptchaImageText"].ToString().ToLower() == TxtSecurityCode.Text.ToLower())
    {
        Page.ClientScript.RegisterStartupScript(this.GetType(), "showMessage()", "<script>ShowMessage() </script>");
    }
    else
    {
        LblMsg.ForeColor = System.Drawing.Color.Red;
        LblMsg.Text = "Security code is wrong";
    }
    //end captcha
}

Thank you for help.

15
  • 5
    Aside from anything else, you should be using using statements to close your commands and connections. Commented Oct 13, 2012 at 8:07
  • Can you post which line of code you got that exception on? Commented Oct 13, 2012 at 8:10
  • @NickW: I dont know how to debug it line by line, because its not local, I uploaded it. Commented Oct 13, 2012 at 8:13
  • Now you don't assign the Connection to the Command and you should show what INSERT COMMAND is. Commented Oct 13, 2012 at 8:14
  • it's not a bug, it's the way you handle the code. Commented Oct 13, 2012 at 8:14

3 Answers 3

2

set the connectionstring first before opening

    SqlConnection sc = new SqlConnection();
    sc.ConnectionString = MyConnectionString
    SqlCommand scm = new SqlCommand(INSERT COMMAND);
    scm.Connection = sc;           // missing this.
    sc.Open();
    scm.ExecuteNonQuery();
    sc.Close();

or

using (SqlConnection conn = new SqlConnection("connectionstringhere"))
{
    using (SqlCommand comm = new SqlCommand())
    {
        comm.Connection = conn;
        comm.CommandText = "your query";
        comm.CommandType = CommandType.Text;
        // comm.Parameters.AddWithValue("@paramName","value"); // if you have parameters
        try
        {
            conn.Open();
            comm.ExecuteNonQuery();
        }
        catch(SqlException ex)
        {
            // error here
            // ex.Message.Tostring  // holds the error message
        }
    }
}
Sign up to request clarification or add additional context in comments.

5 Comments

I am sorry, But it was correct in my code. In here I writed it wrong
@masoudkeshavarz check my updated answer. set connection to your command.
@masoudkeshavarz: You must make sure you post your actual code in future. There's no point in us finding bugs in irrelevant code. It's worth taking an extra few minutes to make sure your question is as good as it can be before posting. Please read tinyurl.com/so-hints
yes im really sorry. next time I post question after I was sure. Im sorry again.
I used using statement just like you said and nothing changed. Also I dont have any error in local. I updated my question. Thank you for help.
0

It should be new SqlCommand(query, sc)

Comments

0

Thank you everyone for help. This error occurred because value of a session was null and ToString() method couldn't find any data to convert.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.