473,394 Members | 1,802 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

Unable to delete row

Hi. I'm new to VB.net and am used to working with recordsets (much
easier). I have a datagrid that, when the user double clicks on a row,
I am giving them a chance to delete it. I thought this would be simple
but it's taking me forever. I am getting an error when I try to delete
the record. I can't figure out what I'm doing wrong.
The error is "Object reference not set to an instance of an object".
None of the incredibly overpriced books I have seem to be of any help.
Someone's knowledge would be greatly appreciated.
Private Sub dgProjItems_DoubleClick(ByVal sender As Object, ByVal e As
System.EventArgs) Handles dgProjItems.DoubleClick
Dim CurItemID As String
CurItemID = dgProjItems.Item(dgProjItems.CurrentRowIndex(), 0)

Dim strConnectionString As String = _
"Provider=Microsoft.Jet.OLEDB.4.0;" & _
"Data
Source=C:\Databases\NewEstimate.mdb;"
Dim cn As New OleDbConnection(strConnectionString)
Dim strSQLl As String = "Select * from ProjectsItems Where
ItemID = '" & CurItemID & "'"
Dim objCommand1 As New OleDbCommand(strSQLl, cn)
Dim da As New OleDbDataAdapter(objCommand1)
Dim ds As New DataSet

Try
cn.Open()

Dim cb As New OleDb.OleDbCommandBuilder(da)

ds.Tables("ProjectItems").Rows(0).Delete() 'error here
da.Update(ds, "ProjectItems")

Me.dgProjItems.DataSource = ds.Tables("ProjectItems")

Catch OleDbExceptionErr As OleDb.OleDbException
MessageBox.Show(OleDbExceptionErr.Message, "Access SQL")
End Try
End Sub

Nov 21 '05 #1
7 3665
tr********@yahoo.com wrote:
Hi. I'm new to VB.net and am used to working with recordsets (much
easier). I have a datagrid that, when the user double clicks on a row,
I am giving them a chance to delete it. I thought this would be simple
but it's taking me forever. I am getting an error when I try to delete
the record. I can't figure out what I'm doing wrong.
The error is "Object reference not set to an instance of an object".
None of the incredibly overpriced books I have seem to be of any help.
Someone's knowledge would be greatly appreciated.
Private Sub dgProjItems_DoubleClick(ByVal sender As Object, ByVal e As
System.EventArgs) Handles dgProjItems.DoubleClick
Dim CurItemID As String
CurItemID = dgProjItems.Item(dgProjItems.CurrentRowIndex(), 0)

Dim strConnectionString As String = _
"Provider=Microsoft.Jet.OLEDB.4.0;" & _
"Data
Source=C:\Databases\NewEstimate.mdb;"
Dim cn As New OleDbConnection(strConnectionString)
Dim strSQLl As String = "Select * from ProjectsItems Where
ItemID = '" & CurItemID & "'"
Dim objCommand1 As New OleDbCommand(strSQLl, cn)
Dim da As New OleDbDataAdapter(objCommand1)
Dim ds As New DataSet

Try
cn.Open()

Dim cb As New OleDb.OleDbCommandBuilder(da)

ds.Tables("ProjectItems").Rows(0).Delete() 'error here
da.Update(ds, "ProjectItems")

Me.dgProjItems.DataSource = ds.Tables("ProjectItems")

Catch OleDbExceptionErr As OleDb.OleDbException
MessageBox.Show(OleDbExceptionErr.Message, "Access SQL")
End Try
End Sub


"Object reference not set to an instance of an object"

This error means that an object you are trying to do an operation
against does not exist (is nothing). so figure out which object is empty.

dim DT as datatable = ds.Tables("ProjectItems")
dim DR as datarow = DT.Rows(0)
DR.Delete()

My guess is that your datatable isn't named ProjectItems so that is
returning nothing.

Chris
Nov 21 '05 #2
Your code isn't going to work because on one line you create a new empty
dataset:

Dim ds As New DataSet

And then on another line, you are attempting to delete from this empty
dataset:

ds.Tables("ProjectItems").Rows(0).Delete() 'error here

You're never calling Fill on your OleDbDataAdapter to fill this dataset so
that you can delete the row.
"tr********@yahoo.com" wrote:
Hi. I'm new to VB.net and am used to working with recordsets (much
easier). I have a datagrid that, when the user double clicks on a row,
I am giving them a chance to delete it. I thought this would be simple
but it's taking me forever. I am getting an error when I try to delete
the record. I can't figure out what I'm doing wrong.
The error is "Object reference not set to an instance of an object".
None of the incredibly overpriced books I have seem to be of any help.
Someone's knowledge would be greatly appreciated.
Private Sub dgProjItems_DoubleClick(ByVal sender As Object, ByVal e As
System.EventArgs) Handles dgProjItems.DoubleClick
Dim CurItemID As String
CurItemID = dgProjItems.Item(dgProjItems.CurrentRowIndex(), 0)

Dim strConnectionString As String = _
"Provider=Microsoft.Jet.OLEDB.4.0;" & _
"Data
Source=C:\Databases\NewEstimate.mdb;"
Dim cn As New OleDbConnection(strConnectionString)
Dim strSQLl As String = "Select * from ProjectsItems Where
ItemID = '" & CurItemID & "'"
Dim objCommand1 As New OleDbCommand(strSQLl, cn)
Dim da As New OleDbDataAdapter(objCommand1)
Dim ds As New DataSet

Try
cn.Open()

Dim cb As New OleDb.OleDbCommandBuilder(da)

ds.Tables("ProjectItems").Rows(0).Delete() 'error here
da.Update(ds, "ProjectItems")

Me.dgProjItems.DataSource = ds.Tables("ProjectItems")

Catch OleDbExceptionErr As OleDb.OleDbException
MessageBox.Show(OleDbExceptionErr.Message, "Access SQL")
End Try
End Sub

Nov 21 '05 #3
Thank you both for your quick reply.
Scott........I added the fill method and now get the error "An
unhandled exception of type 'System.Data.OleDb.OleDbException' occurred
in system.data.dll"

Here's the revised code:

Dim strSQLl As String = "Select * from ProjectItems Where ItemID = '" &
CurItemID & "'"
Dim objCommand1 As New OleDbCommand(strSQLl, cn)
Dim da As New OleDbDataAdapter(objCommand1)
Dim ds As New DataSet
da.Fill(ds, "ProjectItems")
Try
cn.Open()

Dim cb As New OleDb.OleDbCommandBuilder(da)

ds.Tables("ProjectItems").Rows(0).Delete()

da.Update(ds, "ProjectItems")

Me.dgProjItems.DataSource = ds.Tables("ProjectItems")

Catch OleDbExceptionErr As OleDb.OleDbException
MessageBox.Show(OleDbExceptionErr.Message, "Access SQL")
End Try

Chris........The table name is correct. Thanks.

Nov 21 '05 #4
tr********@yahoo.com wrote:
Thank you both for your quick reply.
Scott........I added the fill method and now get the error "An
unhandled exception of type 'System.Data.OleDb.OleDbException' occurred
in system.data.dll"

Here's the revised code:

Dim strSQLl As String = "Select * from ProjectItems Where ItemID = '" &
CurItemID & "'"
Dim objCommand1 As New OleDbCommand(strSQLl, cn)
Dim da As New OleDbDataAdapter(objCommand1)
Dim ds As New DataSet
da.Fill(ds, "ProjectItems")
Try
cn.Open()

Dim cb As New OleDb.OleDbCommandBuilder(da)

ds.Tables("ProjectItems").Rows(0).Delete()

da.Update(ds, "ProjectItems")

Me.dgProjItems.DataSource = ds.Tables("ProjectItems")

Catch OleDbExceptionErr As OleDb.OleDbException
MessageBox.Show(OleDbExceptionErr.Message, "Access SQL")
End Try

Chris........The table name is correct. Thanks.


what does oledbexceptionerr.message say? It will give more detail about
what the error is.

Chris
Nov 21 '05 #5
I ran through all the code. That's the only error I get.

Nov 21 '05 #6
Travlintom,

At least do you consequently have to close something when you open
something. Closing and opening is in fact not needed for a dataadapter
(however better when you use more tables), because the dataadapter does that
generic for you.

Your block could be this both for the Fill and the Update
Try
open connection
Try
fill or update
Catch error
'do action for the Try or Update
End Try
Catch error
'do action for the Open
Finally
Close connection
End Try

As I wrote, you can not use the open. In Net 2.0 you can than use as well
"using".

I hope this helps,

Cor
Nov 21 '05 #7
Thanks all. I found the problem. It was actually the SQL statement was
looking for a string when It should have been an integer. I made the
change and it works fine. Thanks for all your help.

Nov 21 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Peter Gomis | last post by:
I have encountered a situation where I am unable to remove a .NET assembly from the GAC. The message I receive is "Assembly 'assemblyname' could not be uninstalled because it is required by other...
2
by: Barbara | last post by:
Hi, I have an sql database that has the primary key set to three fields, but has not been set as unique(I didn't create the table). I have 1 record that has 2 duplicates and I am unable to delete...
9
by: MAF | last post by:
Does anyone know why in 2005 I might be getting this error everytime I try and recompile? Error 226 Unable to copy file "obj\Debug\myfile.dll" to "bin\Debug\myfile.dll". The process cannot...
0
by: bazzer | last post by:
hey, im trying to access a microsoft access database from an ASP.NET web application in visual basic 2003.NET. i get the following error when i try running it: Server Error in...
4
by: Sailor1877 via AccessMonster.com | last post by:
I've been reading all the threads about problems with LDB files but my specific problem doesn't seem to be addressed. I have a back end database on a server that I'm unable to run Compact and...
4
by: Wannabe | last post by:
I am using ASP.Net 2.0 and have a gridview on my page. I have everything working except the delete command. The page reloads except the row I am trying to delete is still there. I believe it is...
0
by: Buddy Home | last post by:
Hello, I'm trying to upload a file programatically and occasionally I get the following error message. Unable to write data to the transport connection: An established connection was aborted...
3
by: Buddy Home | last post by:
Hello, I'm trying to upload a file programatically and occasionally I get the following error message. Unable to write data to the transport connection: An established connection was aborted...
1
by: Markw | last post by:
Hi folks I think I've got a variable problem but not 100% sure. Background: I took the CMS example from chapter 6 in "Build your Own Database Driven Website Using PHP&MySQL" and have attempted to...
5
by: maheswaran | last post by:
Hi, i have issue in FTP , i have tried with .net and also c# Note the following scenario: 1. Upload the file to FTP >than 60 MB. 2. At the time of Uploading, unplug the LAN cable (or)...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
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 using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.