Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: Visual-Studio VB.NET
This is the actual code and in this I am taking the values of the datagrid and sending it to the db through a sql stored proc, all the help appreciated. Please let me know if anything wrong in this that is causing the error
Private Sub BtnSendToGP_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnSendToGP.Click
 
        Dim VarItemNumber As String
         Dim VarItemDescription As String
         Dim VarUnitOfMeasure As String
         Dim VarItemClass As String
         Dim VarCurrentCost As String
         Dim Conn As SqlConnection
 
 
        Conn = New SqlConnection("Server = db02; Initial Catalog=SDPRD; User=xx; Password=xxxx" & "Data Source=Dynamics;Integrated Security=SSPI;")
         Try
             For cn As Integer = 0 To DtGridView.RowCount - 1
                 VarItemNumber = DtGridView(0, cn).Value.ToString
                 VarItemDescription = DtGridView(3, cn).Value.ToString
                 VarUnitOfMeasure = DtGridView(4, cn).Value.ToString
                 VarItemClass = DtGridView(5, cn).Value.ToString
                 VarCurrentCost = DtGridView(6, cn).Value.ToString
 
                Dim myCommand As SqlCommand = New SqlCommand("dbo.taUpdateCreateItemRcd", Conn)
                 If Conn.State = ConnectionState.Open Then Conn.Close()
                 Conn.Open()
                 myCommand.CommandType = Data.CommandType.StoredProcedure
                 myCommand.Parameters.AddWithValue("@I_vITEMNMBR", VarItemNumber)
                 myCommand.Parameters.AddWithValue("@I_vUseItemClass", 1)
                 myCommand.Parameters.AddWithValue("@I_vITEMDESC", VarItemDescription)
                 myCommand.Parameters.AddWithValue("@I_vUOMSCHDL", VarUnitOfMeasure)
                 myCommand.Parameters.AddWithValue("@I_vITMCLSCD", VarItemClass)
                 myCommand.Parameters.AddWithValue("@I_vCURRCOST", VarCurrentCost)
                 myCommand.Parameters.AddWithValue("@O_iErrorState", 0)
                 myCommand.Parameters.AddWithValue("@oErrString", 0)
                 myCommand.ExecuteNonQuery()
             Next
         Catch ex As Exception
             MsgBox(ex.ToString)
         Finally
             If Conn.State = ConnectionState.Open Then
                 Conn.Close()
             End If
         End Try
         MsgBox("All The Records Added To GP Successfully...", MsgBoxStyle.Information)
     End Sub
Posted 30-Apr-13 9:59am
Comments
richcb at 30-Apr-13 15:02pm
   
Well that error means you are trying to use an object that has not been instantiated. Debug the code and figure out which line it is.
ThePhantomUpvoter at 30-Apr-13 15:04pm
   
Of course something is wrong, otherwise you would not be getting an error. What you failed to do was debug your code and find the line that was throwing the error. If you have done that, you would have found the object that you was trying to use that was null. Then you would have been able to fix it. Since there are several things that could be causing the error, I will not be making any guesses.
FARMAN786 at 30-Apr-13 15:06pm
   
how do i debug a .dll, any ideas.
 
Thanks,
ThePhantomUpvoter at 30-Apr-13 16:35pm
   
The exact same way you debug anything else. Place a break point in the code and run it.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

There should be more in the stack trace.
 
Based on above code, most susceptible lines are:
                 VarItemNumber = DtGridView(0, cn).Value.ToString
                 VarItemDescription = DtGridView(3, cn).Value.ToString
                 VarUnitOfMeasure = DtGridView(4, cn).Value.ToString
                 VarItemClass = DtGridView(5, cn).Value.ToString
                 VarCurrentCost = DtGridView(6, cn).Value.ToString
 
Object reference not set to an instance of an object: This error happens when you try to use a property or call a method of an object that is null. More details: here[^]

A simple use of Visual studio DEBUGGER can tell you the object because of which it is happening. Just look at the stack trace and put a debugger on that line. Check the objects of that line and see if any one is null and you are trying to use that objects property. Handle the same.
 

BTW, why there are no brackets after ToString in any of the code lines above, like x.ToString(), Refer: MSDN: Object.ToString Method [^]
  Permalink  
v2
Comments
FARMAN786 at 30-Apr-13 17:19pm
   
Thanks, I will check that but is there any other mthod to code the above line in order to grab the data from the grid and feed it to the stored proc
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

You did not show where the exception with the message "Object reference not set to an instance of an object" is thrown.

Not to worry. This is one of the very easiest cases to detect and fix. It simply means that some member/variable of some reference type is dereferenced by using and of its instance (non-static) members, which requires this member/variable to be non-null, but in fact it appears to be null. Simply execute it under debugger, it will stop the execution where the exception is thrown. Put a break point on that line, restart the application and come to this point again. Evaluate all references involved in next line and see which one is null while it needs to be not null. After you figure this out, fix the code: either make sure the member/variable is properly initialized to a non-null reference, or check it for null and, in case of null, do something else.
 
Please see also: want to display next record on button click. but got an error in if condition of next record function "object reference not set to an instance of an object"[^].
 
Sometimes, you cannot do it under debugger, by one or another reason. One really nasty case is when the problem is only manifested if software is built when debug information is not available. In this case, you have to use the harder way. First, you need to make sure that you never block propagation of exceptions by handling them silently (this is a crime of developers against themselves, yet very usual). The you need to catch absolutely all exceptions on the very top stack frame of each thread. You can do it if you handle the exceptions of the type System.Exception. In the handler, you need to log all the exception information, especially the System.Exception.StackTrace:
http://msdn.microsoft.com/en-us/library/system.exception.aspx[^],
http://msdn.microsoft.com/en-us/library/system.exception.stacktrace.aspx[^].
 
The stack trace is just a string showing the full path of exception propagation from the throw statement to the handler. By reading it, you can always find ends. For logging, it's the best (in most cases) to use the class System.Diagnostics.EventLog:
http://msdn.microsoft.com/en-us/library/system.diagnostics.eventlog.aspx[^].
 
Good luck,
—SA
  Permalink  
Comments
FARMAN786 at 30-Apr-13 18:11pm
   
Thanks Sergey, I am new to this but in my code how and where do I check for null and throw an error, appreciate it.
 
Thanks,
Sergey Alexandrovich Kryukov at 30-Apr-13 18:26pm
   
You need to find this place used the methods I described above. Can you use the debugger?
—SA
Sergey Alexandrovich Kryukov at 30-Apr-13 19:30pm
   
Sorry, you did not answer my question about using of the debugger. The code you've show is pretty much useless as it's only a part, cannot be compiled anyway. Always add the code using "Improve question", not in comment.
—SA
FARMAN786 at 30-Apr-13 19:49pm
   
Sorry Did not read your comment, next time will put the code in Improve question
Sergey Alexandrovich Kryukov at 30-Apr-13 20:42pm
   
No problem. I hope you can locate the problem and resolve it.
Your follow-up questions are welcome.
—SA
Canny Brisk at 29-Jan-14 11:45am
   
5ed!
Sergey Alexandrovich Kryukov at 29-Jan-14 13:10pm
   
Thank you.
—SA
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

Hi Sergey,
 
We can get rid of the last column by using the syntax below.
 
DtGridView.AllowUserToAddRows = False
 
Thanks a lot for all your help.
  Permalink  

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 OriginalGriff 341
1 Marcin Kozub 225
2 Sergey Alexandrovich Kryukov 220
3 Praneet Nadkar 197
4 Shweta N Mishra 161
0 OriginalGriff 8,149
1 Sergey Alexandrovich Kryukov 7,287
2 DamithSL 5,614
3 Manas Bhardwaj 4,986
4 Maciej Los 4,910


Advertise | Privacy | Mobile
Web03 | 2.8.1411023.1 | Last Updated 30 Apr 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100