Home All Groups Group Topic Archive Search About

Object Reference Error On Setting sqlCommand connection

Author
6 Jan 2006 8:48 PM
tjonsek
I've been reading most of the day on how to use datagrids and came up
with code to delete a record. I'm getting an error "Object Reference
not set to an instance of an object" for the line where I set the
command's connection. I really am confused as this same basic code
works throughout my project. I thought perhaps because the event is
bubbling up from the datagrid?

Here's my code. Any insight would help. Thanks.
(cnnGIS has been configured through the designer and works elsewhere
just fine)

Public Sub Edit_Grid(ByVal sender As Object, ByVal e As
DataGridCommandEventArgs)
        Dim sql As SqlClient.SqlCommand

        sql.Connection = cnnGIS ******This is the line that produces an
error*********************
        cnnGIS.Open()

        sql.CommandType = CommandType.Text
        If e.CommandName = "removePayout" Then
            sql.CommandText = "Delete from tbl02_employee where
emp_key='" & e.Item.Cells(0).Text & "'"
            Try
                sql.ExecuteNonQuery()
                Me.daEmp.Fill(DsEmpPay1)
                Me.DataGrid1.DataBind()


            Catch ex As Exception
                lblFail.Text = "Failed to remove employee from record.
" & Err.Description
                lblFail.Visible = True
                btnFail.Visible = True
            End Try

        End If

    End Sub

Author
6 Jan 2006 9:15 PM
Kerry Moorman
tjonsek,

Dim sql As New SqlClient.SqlCommand

Kerry Moorman


Show quoteHide quote
"tjon***@phenom-biz.com" wrote:

> I've been reading most of the day on how to use datagrids and came up
> with code to delete a record. I'm getting an error "Object Reference
> not set to an instance of an object" for the line where I set the
> command's connection. I really am confused as this same basic code
> works throughout my project. I thought perhaps because the event is
> bubbling up from the datagrid?
>
> Here's my code. Any insight would help. Thanks.
> (cnnGIS has been configured through the designer and works elsewhere
> just fine)
>
>  Public Sub Edit_Grid(ByVal sender As Object, ByVal e As
> DataGridCommandEventArgs)
>         Dim sql As SqlClient.SqlCommand
>
>         sql.Connection = cnnGIS ******This is the line that produces an
> error*********************
>         cnnGIS.Open()
>
>         sql.CommandType = CommandType.Text
>         If e.CommandName = "removePayout" Then
>             sql.CommandText = "Delete from tbl02_employee where
> emp_key='" & e.Item.Cells(0).Text & "'"
>             Try
>                 sql.ExecuteNonQuery()
>                 Me.daEmp.Fill(DsEmpPay1)
>                 Me.DataGrid1.DataBind()
>
>
>             Catch ex As Exception
>                 lblFail.Text = "Failed to remove employee from record.
> " & Err.Description
>                 lblFail.Visible = True
>                 btnFail.Visible = True
>             End Try
>
>         End If
>
>     End Sub
>
>
Author
6 Jan 2006 9:25 PM
tjonsek
Thank you Kerry.
That was a case of staring at something long enough to overlook the
obvious.