Norman Maina
asked on
Object reference not set to an Instance
Hello All,
Am getting the Object reference not set to an instance error message when I load my form contaning a datagridview whenever the grid has null values.
How do i resolve that?
I have put default fro null as 0 in the datagridview formating.
Am getting the Object reference not set to an instance error message when I load my form contaning a datagridview whenever the grid has null values.
How do i resolve that?
I have put default fro null as 0 in the datagridview formating.
Private Function TotalGrossWgt() As Double
Dim tot As Double = 0
Dim i As Integer = 0
If Not GardenInvoiceDtlDataGridView Is Nothing And Not GardenInvoiceDtlDataGridView.CurrentRow.IsNewRow Then
For i = 0 To GardenInvoiceDtlDataGridView.Rows.Count - 1
tot = tot + Convert.ToDouble(GardenInvoiceDtlDataGridView.Rows(i).Cells("DataGridViewTextBoxColumn21").Value)
Next i
End If
Return tot
End Function
Private Function TotalNetWgt() As Double
Dim tot As Double = 0
Dim i As Integer = 0
If Not GardenInvoiceDtlDataGridView Is Nothing And Not GardenInvoiceDtlDataGridView.CurrentRow.IsNewRow Then
For i = 0 To GardenInvoiceDtlDataGridView.Rows.Count - 1
tot = tot + Convert.ToDouble(GardenInvoiceDtlDataGridView.Rows(i).Cells("DataGridViewTextBoxColumn20").Value)
Next i
End If
Return tot
End Function
Private Function NetWgt() As Double
Dim tot As Double = 0
Dim i As Integer = 0
If Not GardenInvoiceDtlDataGridView Is Nothing And Not GardenInvoiceDtlDataGridView.CurrentRow.IsNewRow Then
For i = 0 To GardenInvoiceDtlDataGridView.Rows.Count - 1
tot = tot + Convert.ToDouble(GardenInvoiceDtlDataGridView.Rows(i).Cells("DataGridViewTextBoxColumn19").Value)
Next i
End If
Return tot
End Function
Private Function TotalQty() As Double
Dim tot As Double = 0
Dim i As Integer = 0
If Not GardenInvoiceDtlDataGridView Is Nothing And Not GardenInvoiceDtlDataGridView.CurrentRow.IsNewRow Then
For i = 0 To GardenInvoiceDtlDataGridView.Rows.Count - 1
tot = tot + Convert.ToDouble(GardenInvoiceDtlDataGridView.Rows(i).Cells("DataGridViewTextBoxColumn18").Value)
Next i
End If
Return tot
End Function
Try
TxtTotalQty.Text = 0
TxtNetWgt.Text = 0
TxtTotalNetWgt.Text = 0
TxtGrossweight.Text = 0
If Not (GardenInvoiceDtlDataGridView.CurrentRow.IsNewRow) And GardenInvoiceDtlDataGridView.Rows.Count > 0 Then
TxtTotalQty.Text = TotalQty().ToString("N")
TxtNetWgt.Text = NetWgt().ToString("N")
TxtTotalNetWgt.Text = TotalNetWgt().ToString("N")
TxtGrossweight.Text = TotalGrossWgt().ToString("N")
End If
Catch ex As Exception
MessageBox.Show(ex.Message, "Tea Tracing| GridFooter Changing", MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try
which line throws the error?
try debug your code and when reach the line that throws the exception, add a validation check to make sure that it's not null.
Is it possible that GardenInvoiceDtlDataGridVi ew.Current Row is nothing, even if GardenInvoiceDtlDataGridVi ew is not Nothing?
yes it is possible, u need to check if GardenInvoiceDtlDataGridVi ew.Current Row is not null first.
ASKER
Error is Thrown here:
If Not (GardenInvoiceDtlDataGridV iew.Curren tRow.IsNew Row) And GardenInvoiceDtlDataGridVi ew.Rows.Co unt > 0 Then
....
sedgwick>let em check as per your suggestion -Thanls very much for the quick response.Will get back to you.
If Not (GardenInvoiceDtlDataGridV
....
sedgwick>let em check as per your suggestion -Thanls very much for the quick response.Will get back to you.
ASKER
sedgwick>I tried
Not working
If not GardenInvoiceDtlDataGridView.Currencell.value is nothing
Not working
try:
if GardenInvoiceDtlDataGridVi ew.Currenc ell is not nothing then
if GardenInvoiceDtlDataGridVi
ASKER
I have put this code on the paint event of the datagridview -it works fine until if finds rows with no values thats when it brings teh object reference not set to an instance error.
Private Function TotalQty() As Double
Dim tot As Double = 0
Dim i As Integer = 0
If Not GardenInvoiceDtlDataGridView Is Nothing And Not GardenInvoiceDtlDataGridView.CurrentRow.IsNewRow Then
For i = 0 To GardenInvoiceDtlDataGridView.Rows.Count - 1
tot = tot + Convert.ToDouble(GardenInvoiceDtlDataGridView.Rows(i).Cells("DataGridViewTextBoxColumn18").Value)
Next i
End If
Return tot
End Function
>Not GardenInvoiceDtlDataGridVi ew.Current Row.IsNewR ow
That is the issue still. CurrentRow is null and you are trying to access it.
Why do you need to test that when you are just looping through all the rows?
If you do need that then use another if condition
If(Not IsNothing(GardenInvoiceDtl DataGridVi ew.Current Row))
That is the issue still. CurrentRow is null and you are trying to access it.
Why do you need to test that when you are just looping through all the rows?
If you do need that then use another if condition
If(Not IsNothing(GardenInvoiceDtl
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
you can also use foreach as well:
or linq:
For Each row As var In GardenInvoiceDtlDataGridView.Rows.Cast(Of DataGridViewRow)()
tot += row.Cells("DataGridViewTextBoxColumn18").Value
Next
or linq:
tot = GardenInvoiceDtlDataGridView.Rows.Cast(Of DataGridViewRow)().Sum(Function(n) n.Cells("DataGridViewTextBoxColumn18").Value)
ASKER
Sorry for late response -it worked like a charm.
Imagine i had commented the exception message to hide the problem.Now my code is fine,thanks to you.
Imagine i had commented the exception message to hide the problem.Now my code is fine,thanks to you.