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.