Click here to Skip to main content
15,886,769 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I'm trying to display data in a Gridview without using the page load because I am also using a tabview of which each tab will display new data in a new grid.

When I run the following code, I'm getting an error "Object not set to a reference of an object".

When I step through the code, it is reading the data from the text boxes... just will not display the grid or the data.

The Stored procedure parameter have also been tested.

VB
Public Sub GetContractInfo()

        Dim connStr As String
        connStr = _
     ConfigurationManager.ConnectionStrings("HawkConnectionString").ConnectionString
        Dim cmd = New SqlCommand("bdis_ContractSearch")
        cmd.CommandType = CommandType.StoredProcedure
        cmd.Parameters.AddWithValue("@Contract", txtSCRCHcontractnum.Text)
        cmd.Parameters.AddWithValue("@ContractDesc", txtSRCHContractDescr.Text)
        cmd.Parameters.AddWithValue("@ProjMgrName", txtSRCHProjectMgr.Text)
        cmd.Parameters.AddWithValue("@Job", txtSRCHjobnum.Text)

        Try
            cmd.Connection.Open()
            grdContractSearch.DataSource = cmd.ExecuteReader()
            grdContractSearch.DataBind()

            cmd.Connection.Close()
            cmd.Connection.Dispose()
        Catch ex As Exception
            lblStatus.Text = ex.Message

        End Try


    End Sub


[Modified: added some tags for the question and fixed your code...you had to pre tags which were messing up the formatting.]
Posted
Updated 30-Jun-10 5:53am
v2

1 solution

you have to set the connection string for the sqlcommand:

VB
cmd.Connection = New SqlConnection(connStr)


This is another good reason to only put a single line within a Try/Catch block. When you have multiple lines of code, if you don't look at the StackTrace, you can't tell where the error came from sometimes.

So, I would put the cmd.Connection.Open in a Try/Cath and the cmd.ExecuteReader in a Try/Catch.

Close will only throw an error if Open does.

Dispose won't throw an error. Neither will GridView.DataBind.
 
Share this answer
 
Comments
technette 30-Jun-10 12:19pm    
Reason for my vote of 5
Worked as soon as I made the corrections. 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