Null reference

M

MasterChief

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
 
K

Karl Seguin [MVP]

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
 
M

MasterChief

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?
 
M

MasterChief

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.
 
K

Karl Seguin [MVP]

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%[email protected]...
 
M

MasterChief

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,767
Messages
2,569,572
Members
45,046
Latest member
Gavizuho

Latest Threads

Top