check the last row of datagridview

I use VB.net 2008. i have a datagridview already populate data. my project required to loop throught the Datagridview to save it to database.
when it loop to the last row. which is empty row. i got below error

nullReferenceException was unhandled.

any solution?
aaronthamAsked:
Who is Participating?
 
OneMHzConnect With a Mentor Commented:
Just a guess here, but if the last row is empty, the value you're getting from DGVPPSlot(0, AA).Value is probably null.  So calling ToString on it is probably why you're getting the NullReferenceException.  An alternative to the Try/Catch method described would be to put that value into a local variable, check it for null, and if it is, pass a default value to Gstrsql.  Something like:


Dim AAVal = DGVPPSlot(0, AA).Value
Dim AAString as String
If AAVal is Nothing
    AAString = "some default"
Else
    AAString = AAVal.ToString
End If
 
Dim Gstrsql As String = "Insert into Qitem (ItemID,QuoteID,StdMem) values('" & QDD & "','" & QID & "','" & Trim(AAString) & "')"

Open in new window

0
 
vs1784Commented:
put your code in try,catch and check the exception in catch block and handle it
0
 
aaronthamAuthor Commented:
i put try and catch i got below error msg.

Object reference not set to an instance of an object
0
Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

 
vs1784Commented:
Can you post some code?
0
 
aaronthamAuthor Commented:

the code work. it will have error when it reach to the last row of the Datagridview.
my Datagridview is DGVPPSlot.

 For AA = 0 To DGVPPSlot.Rows.Count - 1                       
Dim Gstrsql As String = "Insert into Qitem (ItemID,QuoteID,StdMem) values('" & QDD & "','" & QID & "','" & Trim(DGVPPSlot(0, AA).Value.ToString) & "')"
cmd.CommandText = Gstrsql
cmd.Connection = con
cmd.ExecuteNonQuery()
Next AA

Open in new window

0
 
vs1784Commented:
Try this

If not try to declare cmd inside try block.
Try
For AA = 0 To DGVPPSlot.Rows.Count - 1                       
Dim Gstrsql As String = "Insert into Qitem (ItemID,QuoteID,StdMem) values('" & QDD & "','" & QID & "','" & Trim(DGVPPSlot(0, AA).Value.ToString) & "')"
cmd.CommandText = Gstrsql
cmd.Connection = con
cmd.ExecuteNonQuery()
Next AA
Catch ee As Exception
''''Log your error here using ee.Message
End Try

Open in new window

0
 
amar31282Commented:
Try to run the following code and paste the complete error
Dim Gstrsql As String
For AA = 0 To DGVPPSlot.Rows.Count - 1                       
Gstrsql = "Insert into Qitem (ItemID,QuoteID,StdMem) values('" & QDD & "','" & QID & "','" & Trim(DGVPPSlot(0, AA).Value.ToString) & "')"
cmd.CommandText = Gstrsql
cmd.Connection = con
Try
cmd.ExecuteNonQuery()
Catch ex As Exception
errText.Text = ex.DetailedMessage (not sure check in intellisense)
Next AA
Finally
cmd.Dispose()
con.Close()

Open in new window

0
 
GuitarRichConnect With a Mentor Commented:
OneMHz is right. If you allow adding of rows in a datagridview, the last row will always be the new row with no data in it. So either change your loop to be count - 2 or check that the value is not null before adding it to the database.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.