Click here to Skip to main content
15,883,623 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
XML
I have a stored procedure "Get_Emp_No" which retrieves all records from database. I have a code that call the stored procedure mentioned above. I want to display only few fields in datagrid. But I am not getting it. Below is the code of stored procedure and code in which the procedure is called.
Also I am getting Error-"Object Reference not set to an instance of object"
------------------------------------------------------------------
stored procedure
-----------------------------------------------------------------
<pre lang="SQL"><pre lang="sql">CREATE PROCEDURE Get_Emp_No

AS
    SELECT * from emp_master
RETURN 0</pre>

------------------------------------------------------------------
vb code
------------------------------------------------------------------
VB
Try
            con.Open()
            com.Connection = con
            com = New SqlCommand("Get_Emp_No", con)
            ' com.CommandText = "Get_Emp_No"
            com.CommandType = CommandType.StoredProcedure
            dr = com.ExecuteReader
            While dr.Read
                i = DGV_empno.Rows.Add(+1)
                DGV_empno.Rows(i).Cells(0).Value = dr("empid")
                DGV_empno.Rows(i).Cells(1).Value = dr("empname")
                DGV_empno.Rows(i).Cells(2).Value = dr("dob")
                DGV_empno.Rows(i).Cells(3).Value = dr("doj")
                DGV_empno.Rows(i).Cells(4).Value = dr("dept")
                DGV_empno.Rows(i).Cells(5).Value = dr("desig")
                DGV_empno.Rows(i).Cells(6).Value = dr("exp")
                DGV_empno.Rows(i).Cells(7).Value = dr("salary")
            End While
        Catch ex As Exception
            MsgBox(ex.Message)
        Finally
            con.Close()
        End Try
    End Sub
Posted
Updated 16-Mar-15 0:31am
v2
Comments
[no name] 16-Mar-15 6:33am    
Do you know at which line the error occurs?
A94 16-Mar-15 6:35am    
Yes It is occurring at "com.Connection=con"
[no name] 16-Mar-15 6:47am    
Oh right, I could have spotted that - Solution below :-)

VB
Try
          con.Open()
          com = New SqlCommand("Get_Emp_No", con)
        
          com.CommandType = CommandType.StoredProcedure
          dr = com.ExecuteReader
          While dr.Read
              i = DGV_empno.Rows.Add(+1)
              DGV_empno.Rows(i).Cells(0).Value = dr("empid")
              DGV_empno.Rows(i).Cells(1).Value = dr("empname")
              DGV_empno.Rows(i).Cells(2).Value = dr("dob")
              DGV_empno.Rows(i).Cells(3).Value = dr("doj")
              DGV_empno.Rows(i).Cells(4).Value = dr("dept")
              DGV_empno.Rows(i).Cells(5).Value = dr("desig")
              DGV_empno.Rows(i).Cells(6).Value = dr("exp")
              DGV_empno.Rows(i).Cells(7).Value = dr("salary")
          End While
      Catch ex As Exception
          MsgBox(ex.Message)
      Finally
          con.Close()
      End Try
  End Sub
 
Share this answer
 
Comments
Maciej Los 16-Mar-15 7:15am    
+5
You use "com" before you have created it:
VB
com.Connection = con
com = New SqlCommand("Get_Emp_No", con)

If you wouldn't pass the SqlConnection as an argument to the SqlCommand-Constructor, you would simply have to reverse these two lines. But since you do, you can just remove the first of these two lines.
It should work then - if there are no other bugs ;)

I would suggest to use a different identifier for the SqlCommand: cmd - that way it's easier to distinguish from con.
 
Share this answer
 
Comments
Maciej Los 16-Mar-15 7:13am    
Hawk eye! +5!
[no name] 16-Mar-15 7:19am    
I would like to keep the impression of a hawk eye up but in this case I know because I asked him for the error-line in a comment to his question. .....Maybe I just shouldn't have told you *g*
But I take the +5, thank you! :-)

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900