0

I get a System.NullReferenceException when I'm using MySqlDataReader after I've inserted or updated something in the database.

Here is my code:

protected void Page_Load(object sender, EventArgs e)
{
    using (var conn = new MySqlConnection(ConfigurationManager.ConnectionStrings["mysqlconn"].ConnectionString))
        {
            conn.Open();
            using (MySqlCommand comm = conn.CreateCommand())
            {
                comm.CommandText = "UPDATE zs_orders SET orderNo = @orderNo, firstSent = @firstSent WHERE ID =  @id";
                comm.Parameters.AddWithValue("orderNo", "dasda");
                comm.Parameters.AddWithValue("firstSent", DateTime.Now);
                comm.Parameters.AddWithValue("id", Convert.ToInt32(3));
                comm.ExecuteNonQuery();
                comm.Cancel();
                comm.Dispose();
            }
            conn.Close();
        } 


        using (var myConn = new MySqlConnection(ConfigurationManager.ConnectionStrings["mysqlconn"].ConnectionString))
        {
            myConn.Open();
            using (MySqlCommand cmd = new MySqlCommand("select * from zs_customers;", myConn))
                {
                    using (MySqlDataReader dr = cmd.ExecuteReader())
                    {
                        while (dr.Read()) // << Error
                        {
                            //....
                        }
                        dr.Close();
                        myConn.Close();
                    }
                }
        }
}

And here is the error:

System.NullReferenceException – Object reference not set to an instance of an object.

Row 45:    using (MySqlDataReader dr = cmd.ExecuteReader())
Row 46:    {
Row 47 >>>:     while (dr.Read())
Row 48:         {
Row 49:              //....

[NullReferenceException: Object reference not set to an instance of an object.] zs_test.Page_Load(Object sender, EventArgs e) in c:\Users\JennyJ\skydrive\www\wwwroot\zs-test.aspx.cs:47
System.Web.Util.CalliEventHandlerDelegateProxy.Callback(Object sender, EventArgs e) +51 System.Web.UI.Control.OnLoad(EventArgs e) +92
System.Web.UI.Control.LoadRecursive() +54
System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +772

3
  • Are you sure your zs_customers table isn't empty? Because you inserting zs_orders tables.. And 3 is an int, there is no point to use it like Convert.ToInt32(3) Commented Sep 24, 2013 at 7:08
  • Yes, it works fine if I'm not updating anything first. Commented Sep 24, 2013 at 7:14
  • The error will also appear for example if I create a customer and then navigate to another webform with a list of customers. It seems like something happens with the connection to the db. Commented Sep 24, 2013 at 7:33

2 Answers 2

0

Use if(dr.HasRows) before reading data from SqlDataReader to avoid System.NullReferenceException – Object reference not set to an instance of an object.

Sign up to request clarification or add additional context in comments.

2 Comments

I will get the same error if I'm adding that. There are matching records, but I get ths nullreferenceexeption if i access the dr object.
If you are performing another server events on the page then write above code inside if (!IsPostBack){} block.
0

Well, I've found the error..

It was this line:

                comm.Cancel();

It shouldn't be there. Stupid error.

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.