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.Connecti onStrings.Item("draftinglogConnectionString1").ToS tring()
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 5 2847
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" <co*******@mix-net.net> wrote in message
news:11**********************@g47g2000cwa.googlegr oups.com... 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.Connecti onStrings.Item("draftinglogConnectionString1").ToS tring() 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
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?
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.
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:%2*****************@TK2MSFTNGP15.phx.gbl... 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" <co*******@mix-net.net> wrote in message news:11**********************@g47g2000cwa.googlegr oups.com...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.Connecti onStrings.Item("draftinglogConnectionString1").ToS tring() 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
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.Connecti onStrings.Item("draftinglogConnectionString1").ToS tring()
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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Pablo J Royo |
last post by:
Hello:
i have a function that reads a file as an argument and returns a reference
to an object that contains some information obtained from the file: FData
&ReadFile(string FilePath);
But ,...
|
by: Marcus |
last post by:
I'm running into a situation that has me adding a value of "Unknown" to
a reference table. I am being pulled between two trains of thought, and
was curious to get other's input on in. I give an...
|
by: JKop |
last post by:
Does that Standard explicitly forbid the initiation of a null reference? Is
there anything wrong with the following code?:
void Blah( std::string const &k )
{
if ( !&k ) return;
// work with...
|
by: junky_fellow |
last post by:
Can 0x0 be a valid virtual address in the address space
of an application ?
If it is valid, then the location pointed by a NULL pointer
is also valid and application should not receive "SIGSEGV"...
|
by: RMD |
last post by:
I need to be able to keep a list of object references, and null them out one
by one at a later point in time. I realize this can be dangerous, but I have
my reasons.
I can't figure out, however,...
|
by: Boniek |
last post by:
Hi
I define a public property in a new form and I can see this property in
table of Properties in Visual. How I can hide this property to see only in
code ?
Thank's Boniek
|
by: Fei Li |
last post by:
Hi,
Is null value for a class instance allowed in C#? for example:
MyClass obj = null;
If Not, how to initiate an instance with value 0? How to detect if an
instance is "newed" ? And how to...
|
by: David W |
last post by:
I'm almost tearing my hair out. A colleague claimed that a null reference can exist, like
this:
void f( int& p )
{
printf( "%d\n", p );
}
int main (int argc, char *argv)
{
|
by: MikeT |
last post by:
This may sound very elementary, but can you trap when your object is
set to null within the object?
I have created a class that registers an event from an object passed
in the constructor. When...
|
by: valentin tihomirov |
last post by:
As explained in "Using pointers vs. references"
http://groups.google.ee/group/borland.public.delphi.objectpascal/browse_thread/thread/683c30f161fc1e9c/ab294c7b02e8faca#ab294c7b02e8faca ,
the...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
| |