Null reference

Discussion in 'ASP .Net' started by MasterChief, Feb 10, 2006.

  1. MasterChief

    MasterChief Guest

    I was wondering if somebody could look at my code below. It is deleting
    the boxes that I chose out of the database but I am getting an error at
    the line below. It says that I am referencing a null object.
    gvName.DataSource = objCommand.ExecuteReader()

    Here is my code. The gridview on my aspx page is called gridview1.


    Imports System.Data
    Imports System.Data.Sql
    Imports System.Data.SqlClient
    Imports System.Configuration

    Partial Class _Default
    Inherits System.Web.UI.Page
    Protected WithEvents gvName As System.Web.UI.WebControls.GridView

    Protected Sub Delete_Click(ByVal sender As Object, ByVal e As
    System.EventArgs) Handles Delete.Click
    Dim CheckBox As CheckBox
    Dim KeyId As Int32
    Dim strConnection As String =
    System.Configuration.ConfigurationManager.ConnectionStrings.Item("draftinglogConnectionString1").ToString()
    Dim objConnection As New SqlConnection(strConnection)
    For Each GridViewRow As GridViewRow In GridView1.Rows
    CheckBox = CType(GridViewRow.FindControl("chkDelete"),
    CheckBox)
    If CheckBox.Checked = True Then
    KeyId =
    CType(GridView1.DataKeys.Item(GridViewRow.RowIndex).Value, Int32)
    Dim strSql As String = "delete from plans WHERE
    plans.plan_id=" + KeyId.ToString()
    Dim objCommand As New SqlCommand(strSql, objConnection)
    objConnection.Open()
    gvName.DataSource = objCommand.ExecuteReader()
    gvName.DataBind()
    objConnection.Close()
    End If
    Next

    End Sub
    End Class
    MasterChief, Feb 10, 2006
    #1
    1. Advertising

  2. In 2.0, you shoudln't be declaring your controls in codebehind.

    try removeing the line:
    Protected WithEvents gvName As System.Web.UI.WebControls.GridView

    and see if it works.

    Karl
    --
    http://www.openmymind.net/
    http://www.fuelindustries.com/


    "MasterChief" <> wrote in message
    news:...
    >I was wondering if somebody could look at my code below. It is deleting
    > the boxes that I chose out of the database but I am getting an error at
    > the line below. It says that I am referencing a null object.
    > gvName.DataSource = objCommand.ExecuteReader()
    >
    > Here is my code. The gridview on my aspx page is called gridview1.
    >
    >
    > Imports System.Data
    > Imports System.Data.Sql
    > Imports System.Data.SqlClient
    > Imports System.Configuration
    >
    > Partial Class _Default
    > Inherits System.Web.UI.Page
    > Protected WithEvents gvName As System.Web.UI.WebControls.GridView
    >
    > Protected Sub Delete_Click(ByVal sender As Object, ByVal e As
    > System.EventArgs) Handles Delete.Click
    > Dim CheckBox As CheckBox
    > Dim KeyId As Int32
    > Dim strConnection As String =
    > System.Configuration.ConfigurationManager.ConnectionStrings.Item("draftinglogConnectionString1").ToString()
    > Dim objConnection As New SqlConnection(strConnection)
    > For Each GridViewRow As GridViewRow In GridView1.Rows
    > CheckBox = CType(GridViewRow.FindControl("chkDelete"),
    > CheckBox)
    > If CheckBox.Checked = True Then
    > KeyId =
    > CType(GridView1.DataKeys.Item(GridViewRow.RowIndex).Value, Int32)
    > Dim strSql As String = "delete from plans WHERE
    > plans.plan_id=" + KeyId.ToString()
    > Dim objCommand As New SqlCommand(strSql, objConnection)
    > objConnection.Open()
    > gvName.DataSource = objCommand.ExecuteReader()
    > gvName.DataBind()
    > objConnection.Close()
    > End If
    > Next
    >
    > End Sub
    > End Class
    >
    Karl Seguin [MVP], Feb 10, 2006
    #2
    1. Advertising

  3. MasterChief

    MasterChief Guest

    If I do that then gvName isn't declared. How should I be declaring it?
    Also I moved gvName.DataBind() out of the loop. Is that the correct
    thing to do?
    MasterChief, Feb 10, 2006
    #3
  4. MasterChief

    MasterChief Guest

    I decided to change gvName to gridview1 which was the name of my actual
    gridview. When I run the program now it says
    Both DataSource and DataSourceID are defined on GridView1.
    I know I am close because it is deleting the items I select. I am just
    missing one step and any help would be appreciated.
    MasterChief, Feb 10, 2006
    #4
  5. well, if you have an:

    <asp:gridview id="MyGrid" runat="Server">
    .....
    </asp:gridview>

    in asp.net 1.x, you'd declare the control as you did:

    protected MyGrid as GridView

    in 2.0, you can simply access the control by the ID you used in the aspx,
    without declaring anythingin codebehind.

    Karl

    --
    http://www.openmymind.net/



    "Karl Seguin [MVP]" <karl REMOVE @ REMOVE openmymind REMOVEMETOO . ANDME
    net> wrote in message news:%23%...
    > In 2.0, you shoudln't be declaring your controls in codebehind.
    >
    > try removeing the line:
    > Protected WithEvents gvName As System.Web.UI.WebControls.GridView
    >
    > and see if it works.
    >
    > Karl
    > --
    > http://www.openmymind.net/
    > http://www.fuelindustries.com/
    >
    >
    > "MasterChief" <> wrote in message
    > news:...
    >>I was wondering if somebody could look at my code below. It is deleting
    >> the boxes that I chose out of the database but I am getting an error at
    >> the line below. It says that I am referencing a null object.
    >> gvName.DataSource = objCommand.ExecuteReader()
    >>
    >> Here is my code. The gridview on my aspx page is called gridview1.
    >>
    >>
    >> Imports System.Data
    >> Imports System.Data.Sql
    >> Imports System.Data.SqlClient
    >> Imports System.Configuration
    >>
    >> Partial Class _Default
    >> Inherits System.Web.UI.Page
    >> Protected WithEvents gvName As System.Web.UI.WebControls.GridView
    >>
    >> Protected Sub Delete_Click(ByVal sender As Object, ByVal e As
    >> System.EventArgs) Handles Delete.Click
    >> Dim CheckBox As CheckBox
    >> Dim KeyId As Int32
    >> Dim strConnection As String =
    >> System.Configuration.ConfigurationManager.ConnectionStrings.Item("draftinglogConnectionString1").ToString()
    >> Dim objConnection As New SqlConnection(strConnection)
    >> For Each GridViewRow As GridViewRow In GridView1.Rows
    >> CheckBox = CType(GridViewRow.FindControl("chkDelete"),
    >> CheckBox)
    >> If CheckBox.Checked = True Then
    >> KeyId =
    >> CType(GridView1.DataKeys.Item(GridViewRow.RowIndex).Value, Int32)
    >> Dim strSql As String = "delete from plans WHERE
    >> plans.plan_id=" + KeyId.ToString()
    >> Dim objCommand As New SqlCommand(strSql, objConnection)
    >> objConnection.Open()
    >> gvName.DataSource = objCommand.ExecuteReader()
    >> gvName.DataBind()
    >> objConnection.Close()
    >> End If
    >> Next
    >>
    >> End Sub
    >> End Class
    >>

    >
    >
    Karl Seguin [MVP], Feb 11, 2006
    #5
  6. MasterChief

    MasterChief Guest

    I finally got it thank you. My code is below. One of the problems I was
    having was I was putting gridview1.datasouce =
    gridview1.executereader() and I changed it to
    just be objCommand.ExecuteReader()

    Protected Sub Delete_Click(ByVal sender As Object, ByVal e As
    System.EventArgs) Handles Delete.Click
    Dim CheckBox As CheckBox
    Dim KeyId As Int32
    Dim strConnection As String =
    System.Configuration.ConfigurationManager.ConnectionStrings.Item("draftinglogConnectionString1").ToString()
    Dim objConnection As New SqlConnection(strConnection)
    For Each GridViewRow As GridViewRow In GridView1.Rows
    CheckBox = CType(GridViewRow.FindControl("chkDelete"),
    CheckBox)
    If CheckBox.Checked = True Then
    KeyId =
    CType(GridView1.DataKeys.Item(GridViewRow.RowIndex).Value, Int32)
    Dim strSql As String = "delete from plans WHERE
    plans.plan_id=" + KeyId.ToString()
    Dim objCommand As New SqlCommand(strSql, objConnection)
    objConnection.Open()
    objCommand.ExecuteReader()
    objConnection.Close()
    End If
    Next
    GridView1.DataBind()
    End Sub
    MasterChief, Feb 11, 2006
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Kivak Wolf
    Replies:
    2
    Views:
    10,064
    Kivak Wolf
    Jun 28, 2005
  2. vizlab
    Replies:
    3
    Views:
    4,196
    Michael Bar-Sinai
    Oct 17, 2007
  3. Replies:
    16
    Views:
    7,317
    Mike Schilling
    Oct 12, 2005
  4. Replies:
    5
    Views:
    26,595
    Mike Schilling
    Mar 29, 2006
  5. Bo Peng
    Replies:
    13
    Views:
    13,298
    Siemel Naran
    Jul 18, 2004
Loading...

Share This Page