?
Solved

check the last row of datagridview

Posted on 2009-04-23
8
Medium Priority
?
1,049 Views
Last Modified: 2013-11-26
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?
0
Comment
Question by:aarontham
8 Comments
 
LVL 11

Expert Comment

by:vs1784
ID: 24213643
put your code in try,catch and check the exception in catch block and handle it
0
 

Author Comment

by:aarontham
ID: 24213685
i put try and catch i got below error msg.

Object reference not set to an instance of an object
0
 
LVL 11

Expert Comment

by:vs1784
ID: 24213773
Can you post some code?
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 

Author Comment

by:aarontham
ID: 24213831

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
 
LVL 11

Expert Comment

by:vs1784
ID: 24213869
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
 
LVL 3

Accepted Solution

by:
OneMHz earned 1000 total points
ID: 24214331
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
 
LVL 14

Expert Comment

by:amar31282
ID: 24217380
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
 
LVL 12

Assisted Solution

by:GuitarRich
GuitarRich earned 1000 total points
ID: 24223278
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

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an anti-spam), the admin…
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …
Suggested Courses

840 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question