-1

I receive this error from my code below. Why am I getting an exception saying that the "value returned null" when my intellisense is telling me that the variable is what it is supposed to be when I hover the variable at the breakpoint.

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

System.Windows.Forms.DataGridViewCell.Value.get returned null.

    private void button1_Click(object sender, EventArgs e)
    {
        string StrQuery;
        DataTable ReturnInfoDataTable = new DataTable();

        using (var connection = new SqlConnection("Server=;Database=;Trusted_Connection=True;"))
        {
            connection.Open();

            for (int i = 0; i < dataGridView1.Rows.Count; i++)
            {
                string enteredCustomerNum = dataGridView1.Rows[i].Cells["Customer_Number"].Value.ToString();
                string enteredInvoiceNum = dataGridView1.Rows[i].Cells["Invoice_Number"].Value.ToString();
                string enteredRefundNum = dataGridView1.Rows[i].Cells["Refund_Number"].Value.ToString();
                string enteredCheckNum = dataGridView1.Rows[i].Cells["Check_Number"].Value.ToString();

                StrQuery = @"SELECT '" + enteredCheckNum + "' AS Check_Number, [Company_Num],[RHH_INV_NUMBER],[RHH_CUST_NUMBER],[RHH_RETURN_NUMBER],[RHH_CR_REFUND_NUM],[RHH_ENTERED_DATE],[RHH_DATE_POSTED],[RHH_DATE_RESOLVED] ,[TOTAL_AMOUNT] FROM[History_Warehouse].[dbo].[tbl_Return_Header] WHERE[RHH_CUST_NUMBER] = '" + enteredCustomerNum + "' AND[RHH_CR_REFUND_NUM] = '"+ enteredRefundNum + "' AND[RHH_RETURN_NUMBER] = '" + enteredInvoiceNum + "'";

                using (var command = new SqlCommand(StrQuery, connection))
                {
                    ReturnInfoDataTable.Load(command.ExecuteReader());
                }
            }
        }

Why do I get a null exception when the value isn't null, and how can I fix it.

recnac
  • 3,511
  • 6
  • 21
  • 43
Bluestreak22
  • 134
  • 1
  • 11
  • What debugging have you done? – G. LC Jul 31 '18 at 16:59
  • 1
    Which value (in what column/row) exactly throws the exception? When you checked the value using intellisense, are you sure it was the same row that throws the exception? – 41686d6564 Jul 31 '18 at 17:02
  • Yes, I seemed to have fixed the issue but I dont understand the issue.. instead of using "string" variables I changed that to "var" and removed the ToString() method, and it passes through fine.. but why.. is it because its not reading the value in the grid as a string? – Bluestreak22 Jul 31 '18 at 17:03
  • 1
    Likely your cells in one row don't contain what you expect. To find out you might need to debug deep into the for loop. – Sach Jul 31 '18 at 17:03
  • @Bluestreak22 Post the solution to your own question – G. LC Jul 31 '18 at 17:04
  • 3
    Possible duplicate of [What is a NullReferenceException, and how do I fix it?](https://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) – Ňɏssa Pøngjǣrdenlarp Jul 31 '18 at 17:06
  • @Bluestreak22 Because in this case you're pretty much saying `var something = null;` which is valid. – 41686d6564 Jul 31 '18 at 17:07
  • but my var isn't null. The string was not Null either. I only tested 1 row in the dataviewgrid. .. was wrong actually about this – Bluestreak22 Jul 31 '18 at 17:10
  • @Bluestreak22 How did you manage to figure that out? Did you check to make sure it's the same row that causes the exception? I asked you that above and you never answered. Edit: `I only tested 1 row in the dataviewgrid` Why do you have a for loop in your code then? – 41686d6564 Jul 31 '18 at 17:11
  • Okay So i have stepped through the for loop, and for whatever reason it is counting a hidden row.. but why would there be two rows in my grid, I've only entered data in one row. Eventually there will be more but I am testing just the 1 row for now. – Bluestreak22 Jul 31 '18 at 17:14
  • 2
    We can't answer that unless we see what you see. How? By you providing a [Minimal, Complete, and Verifiable example](https://stackoverflow.com/help/mcve). You'd probably want to do that in a new question though because this seems to be a different question. – 41686d6564 Jul 31 '18 at 17:17
  • Solved the issue. Again :) – Bluestreak22 Jul 31 '18 at 17:59

1 Answers1

4

Edit: I know this is getting downvotes for a bad question, but I believe I have some information to share that will be helpful..

Basically what was happening is that the DataGridView object has a property called AllowUserTOAddRows and this auto adds a hidden row.. To fix this i simply added dataGridView1.AllowUserToAddRows = false; to the beginning of my onclick event and this solves the issue..

private void button1_Click(object sender, EventArgs e)
{
    string StrQuery;
    DataTable ReturnInfoDataTable = new DataTable();
    dataGridView1.AllowUserToAddRows = false;
    using (var connection = new SqlConnection("Server=;Database=;Trusted_Connection=True;"))
    {
        connection.Open();

        for (int i = 0; i < dataGridView1.Rows.Count; i++)
        {
            string enteredCustomerNum = dataGridView1.Rows[i].Cells["Customer_Number"].Value.ToString();
            string enteredInvoiceNum = dataGridView1.Rows[i].Cells["Invoice_Number"].Value.ToString();
            string enteredRefundNum = dataGridView1.Rows[i].Cells["Refund_Number"].Value.ToString();
            string enteredCheckNum = dataGridView1.Rows[i].Cells["Check_Number"].Value.ToString();

            StrQuery = @"SELECT '" + enteredCheckNum + "' AS Check_Number, [Company_Num],[RHH_INV_NUMBER],[RHH_CUST_NUMBER],[RHH_RETURN_NUMBER],[RHH_CR_REFUND_NUM],[RHH_ENTERED_DATE],[RHH_DATE_POSTED],[RHH_DATE_RESOLVED] ,[TOTAL_AMOUNT] FROM[History_Warehouse].[dbo].[tbl_Return_Header] WHERE[RHH_CUST_NUMBER] = '" + enteredCustomerNum + "' AND[RHH_CR_REFUND_NUM] = '"+ enteredRefundNum + "' AND[RHH_RETURN_NUMBER] = '" + enteredInvoiceNum + "'";

            using (var command = new SqlCommand(StrQuery, connection))
            {
                ReturnInfoDataTable.Load(command.ExecuteReader());
            }
        }
    }
Bluestreak22
  • 134
  • 1
  • 11