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

Soner Gönül
  • 97,193
  • 102
  • 206
  • 364
JennieJ
  • 1
  • 2
  • 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)` – Soner Gönül Sep 24 '13 at 07:08
  • Yes, it works fine if I'm not updating anything first. – JennieJ Sep 24 '13 at 07: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. – JennieJ Sep 24 '13 at 07:33

2 Answers2

0

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

Mohit
  • 39
  • 1
  • 11
  • 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. – JennieJ Sep 24 '13 at 07:40
  • If you are performing another server events on the page then write above code inside if (!IsPostBack){} block. – Mohit Sep 24 '13 at 07:47
0

Well, I've found the error..

It was this line:

                comm.Cancel();

It shouldn't be there. Stupid error.

JennieJ
  • 1
  • 2