I have enclosed the sample code that I created. I want to read in employee
data, and modify a few fields. I have tried to globally declare the objects
that I need but I still am having problems. I want to update in a seperate
subroutine and seem to have problems. HELP please.
-------------------------------------------
Public Class Form1
Inherits System.Windows.Forms.Form
Public cn As OleDb.OleDbConnection
Public ds As DataSet
Public da As OleDb.OleDbDataAdapter
Public rowEmployee As DataRow
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
Try
Dim strConn As String
strConn = "Provider=Microsoft.Jet.OLEDB.4.0;Data
Source=C:\adonetsbs\SampleDBs\nwind.mdb;"
Dim cn As New OleDb.OleDbConnection(strConn)
Dim strSQL As String
strSQL = "SELECT EmployeeID, FirstName, LastName, Address, City,
Region, " & _
"PostalCode from Employees ORDER BY LastName, FirstName"
Dim da = New OleDb.OleDbDataAdapter(strSQL, strConn)
Dim ds As New DataSet
da.Fill(ds, "Employees")
Dim tbl As DataTable = ds.Tables(0)
'rowEmployee = New DataRow
rowEmployee = tbl.Rows(0)
txtFirstName.Text = rowEmployee("FirstName")
txtLastName.Text = rowEmployee("LastName")
txtAddress.Text = rowEmployee("Address")
Catch ex As Exception
MessageBox.Show(ex.Message & " :: " & ex.Source)
Finally
End Try
End Sub
Private Sub btnUpdate_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles btnUpdate.Click
Try
rowEmployee("LastName") = txtLastName.Text
da.Update(ds)
Catch ex As Exception
MessageBox.Show(ex.Message & " :: " & ex.Source)
End Try
End Sub
End Class
---------------------------------------
When it runs I get the following error:
"Object reference not set to an instance of an object."
on the da.Update(ds) line in the btnUpdate_Click routine.
HELP.
Thanks,
Gary 12 1136
Gary,
In Form1_Load you are creating local variables such as da, ds, etc.
Instead, you need to use the "global" or form level variables that you have
creted in the form's declarations section.
In Form1_Load, you just need to do something like:
ds = New DataSet
instead of dimensioning the variable again.
Kerry Moorman
"Gary Paris" wrote: I have enclosed the sample code that I created. I want to read in employee data, and modify a few fields. I have tried to globally declare the objects that I need but I still am having problems. I want to update in a seperate subroutine and seem to have problems. HELP please.
------------------------------------------- Public Class Form1
Inherits System.Windows.Forms.Form Public cn As OleDb.OleDbConnection Public ds As DataSet Public da As OleDb.OleDbDataAdapter Public rowEmployee As DataRow
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Try Dim strConn As String strConn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\adonetsbs\SampleDBs\nwind.mdb;"
Dim cn As New OleDb.OleDbConnection(strConn)
Dim strSQL As String strSQL = "SELECT EmployeeID, FirstName, LastName, Address, City, Region, " & _ "PostalCode from Employees ORDER BY LastName, FirstName"
Dim da = New OleDb.OleDbDataAdapter(strSQL, strConn) Dim ds As New DataSet
da.Fill(ds, "Employees")
Dim tbl As DataTable = ds.Tables(0)
'rowEmployee = New DataRow rowEmployee = tbl.Rows(0) txtFirstName.Text = rowEmployee("FirstName") txtLastName.Text = rowEmployee("LastName") txtAddress.Text = rowEmployee("Address")
Catch ex As Exception MessageBox.Show(ex.Message & " :: " & ex.Source) Finally End Try End Sub
Private Sub btnUpdate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnUpdate.Click Try rowEmployee("LastName") = txtLastName.Text da.Update(ds)
Catch ex As Exception
MessageBox.Show(ex.Message & " :: " & ex.Source)
End Try
End Sub End Class ---------------------------------------
When it runs I get the following error:
"Object reference not set to an instance of an object."
on the da.Update(ds) line in the btnUpdate_Click routine.
HELP.
Thanks,
Gary
Gary,
There is a lot still not right, however let us first take this problem.
Your error message is strange for me, that I don't directly see.
However you needs an update command in the dataadapter.
That is easy to do for a simple select statement as you have.
See the code I have pasted inline (one row)
And try than again. I now don't understand the error you get, however that
is at least needed.
When you have it running have than a look at
Databinding
\\\
cma = DirectCast(BindingContext(dataset1.Tables(0)), CurrencyManager)
textbox1.DataBindings.Add(New Binding("Text", dataset1.Tables(0),
"LastName"))
///
The cma.position gives you than the row that is used and you can affect that
by using buttons on your form.
I hope this helps,
Cor Public Class Form1
Inherits System.Windows.Forms.Form Public cn As OleDb.OleDbConnection Public ds As DataSet Public da As OleDb.OleDbDataAdapter Public rowEmployee As DataRow
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Try Dim strConn As String strConn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\adonetsbs\SampleDBs\nwind.mdb;"
Dim cn As New OleDb.OleDbConnection(strConn)
Dim strSQL As String strSQL = "SELECT EmployeeID, FirstName, LastName, Address, City, Region, " & _ "PostalCode from Employees ORDER BY LastName, FirstName"
Dim da = New OleDb.OleDbDataAdapter(strSQL, strConn) Dim ds As New DataSet
da.Fill(ds, "Employees")
dim cmb as new OleDb.OleDbCommandbuilder(da)
Dim tbl As DataTable = ds.Tables(0)
'rowEmployee = New DataRow rowEmployee = tbl.Rows(0) txtFirstName.Text = rowEmployee("FirstName") txtLastName.Text = rowEmployee("LastName") txtAddress.Text = rowEmployee("Address")
Catch ex As Exception MessageBox.Show(ex.Message & " :: " & ex.Source) Finally End Try End Sub
Private Sub btnUpdate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnUpdate.Click Try rowEmployee("LastName") = txtLastName.Text da.Update(ds)
Catch ex As Exception
MessageBox.Show(ex.Message & " :: " & ex.Source)
End Try
End Sub End Class ---------------------------------------
When it runs I get the following error:
"Object reference not set to an instance of an object."
on the da.Update(ds) line in the btnUpdate_Click routine.
HELP.
Thanks,
Gary
Kerry,
That is it, however Gary needs my answer as well for his next problem.
:-)
Cor
Okay, I'm going to try for a bit of a rewrite.
--------------------------------------------------------
Dim da As System.Data.OleDb.OleDbDataAdapter
Dim cn As System.Data.OleDb.OleDbConnection
cn = New System.Data.OleDb.OleDbConnection ( _
"provider=Microsoft.Jet.OLEDB.4.0; & _
data source=C:\adonetsbs\SampleDBs\nwind.mdb;"
da = New System.Data.OleDb.OleDbDataAdapter( _
"SELECT EmployeeID, FirstName, LastName, Address, City, Region, PostalCode
FROM Employees ORDER BY LastName, FirstName"
Dim ds As System.Data.DataSet
ds = New System.Data.DataSet
cn.Fill(ds)
---------------------------------------
That will get your data adapter and dataset going.... the rest I'm having a
bit of trouble reading.......
"Gary Paris" wrote: I have enclosed the sample code that I created. I want to read in employee data, and modify a few fields. I have tried to globally declare the objects that I need but I still am having problems. I want to update in a seperate subroutine and seem to have problems. HELP please.
------------------------------------------- Public Class Form1
Inherits System.Windows.Forms.Form Public cn As OleDb.OleDbConnection Public ds As DataSet Public da As OleDb.OleDbDataAdapter Public rowEmployee As DataRow
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Try Dim strConn As String strConn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\adonetsbs\SampleDBs\nwind.mdb;"
Dim cn As New OleDb.OleDbConnection(strConn)
Dim strSQL As String strSQL = "SELECT EmployeeID, FirstName, LastName, Address, City, Region, " & _ "PostalCode from Employees ORDER BY LastName, FirstName"
Dim da = New OleDb.OleDbDataAdapter(strSQL, strConn) Dim ds As New DataSet
da.Fill(ds, "Employees")
Dim tbl As DataTable = ds.Tables(0)
'rowEmployee = New DataRow rowEmployee = tbl.Rows(0) txtFirstName.Text = rowEmployee("FirstName") txtLastName.Text = rowEmployee("LastName") txtAddress.Text = rowEmployee("Address")
Catch ex As Exception MessageBox.Show(ex.Message & " :: " & ex.Source) Finally End Try End Sub
Private Sub btnUpdate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnUpdate.Click Try rowEmployee("LastName") = txtLastName.Text da.Update(ds)
Catch ex As Exception
MessageBox.Show(ex.Message & " :: " & ex.Source)
End Try
End Sub End Class ---------------------------------------
When it runs I get the following error:
"Object reference not set to an instance of an object."
on the da.Update(ds) line in the btnUpdate_Click routine.
HELP.
Thanks,
Gary
Cor,
I put the
dim cmb as new OleDb.OleDbCommandbuilder(da)
line in as you suggested. Still got the same error. Can you explain the
other command you described and where it goes? I don't understand.
Thanks,
Gary
"Cor Ligthert" <no************@planet.nl> wrote in message
news:OW****************@TK2MSFTNGP09.phx.gbl... Gary,
There is a lot still not right, however let us first take this problem. Your error message is strange for me, that I don't directly see. However you needs an update command in the dataadapter. That is easy to do for a simple select statement as you have. See the code I have pasted inline (one row) And try than again. I now don't understand the error you get, however that is at least needed.
When you have it running have than a look at Databinding \\\ cma = DirectCast(BindingContext(dataset1.Tables(0)), CurrencyManager) textbox1.DataBindings.Add(New Binding("Text", dataset1.Tables(0), "LastName")) /// The cma.position gives you than the row that is used and you can affect that by using buttons on your form.
I hope this helps,
Cor
Public Class Form1
Inherits System.Windows.Forms.Form Public cn As OleDb.OleDbConnection Public ds As DataSet Public da As OleDb.OleDbDataAdapter Public rowEmployee As DataRow
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Try Dim strConn As String strConn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\adonetsbs\SampleDBs\nwind.mdb;"
Dim cn As New OleDb.OleDbConnection(strConn)
Dim strSQL As String strSQL = "SELECT EmployeeID, FirstName, LastName, Address, City, Region, " & _ "PostalCode from Employees ORDER BY LastName, FirstName"
Dim da = New OleDb.OleDbDataAdapter(strSQL, strConn) Dim ds As New DataSet
da.Fill(ds, "Employees")
dim cmb as new OleDb.OleDbCommandbuilder(da)
Dim tbl As DataTable = ds.Tables(0)
'rowEmployee = New DataRow rowEmployee = tbl.Rows(0) txtFirstName.Text = rowEmployee("FirstName") txtLastName.Text = rowEmployee("LastName") txtAddress.Text = rowEmployee("Address")
Catch ex As Exception MessageBox.Show(ex.Message & " :: " & ex.Source) Finally End Try End Sub
Private Sub btnUpdate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnUpdate.Click Try rowEmployee("LastName") = txtLastName.Text da.Update(ds)
Catch ex As Exception
MessageBox.Show(ex.Message & " :: " & ex.Source)
End Try
End Sub End Class ---------------------------------------
When it runs I get the following error:
"Object reference not set to an instance of an object."
on the da.Update(ds) line in the btnUpdate_Click routine.
HELP.
Thanks,
Gary
This is OK, but how can I do an update from another subroutine when the
connection and dataadapter are not global? I want to click on a button to
do the update.
Thanks
Gary
"brix_zx2" <br*****@discussions.microsoft.com> wrote in message
news:67**********************************@microsof t.com... Okay, I'm going to try for a bit of a rewrite.
-------------------------------------------------------- Dim da As System.Data.OleDb.OleDbDataAdapter Dim cn As System.Data.OleDb.OleDbConnection
cn = New System.Data.OleDb.OleDbConnection ( _ "provider=Microsoft.Jet.OLEDB.4.0; & _ data source=C:\adonetsbs\SampleDBs\nwind.mdb;"
da = New System.Data.OleDb.OleDbDataAdapter( _ "SELECT EmployeeID, FirstName, LastName, Address, City, Region, PostalCode FROM Employees ORDER BY LastName, FirstName"
Dim ds As System.Data.DataSet ds = New System.Data.DataSet
cn.Fill(ds)
--------------------------------------- That will get your data adapter and dataset going.... the rest I'm having a bit of trouble reading....... "Gary Paris" wrote:
I have enclosed the sample code that I created. I want to read in employee data, and modify a few fields. I have tried to globally declare the objects that I need but I still am having problems. I want to update in a seperate subroutine and seem to have problems. HELP please.
------------------------------------------- Public Class Form1
Inherits System.Windows.Forms.Form Public cn As OleDb.OleDbConnection Public ds As DataSet Public da As OleDb.OleDbDataAdapter Public rowEmployee As DataRow
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Try Dim strConn As String strConn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\adonetsbs\SampleDBs\nwind.mdb;"
Dim cn As New OleDb.OleDbConnection(strConn)
Dim strSQL As String strSQL = "SELECT EmployeeID, FirstName, LastName, Address, City, Region, " & _ "PostalCode from Employees ORDER BY LastName, FirstName"
Dim da = New OleDb.OleDbDataAdapter(strSQL, strConn) Dim ds As New DataSet
da.Fill(ds, "Employees")
Dim tbl As DataTable = ds.Tables(0)
'rowEmployee = New DataRow rowEmployee = tbl.Rows(0) txtFirstName.Text = rowEmployee("FirstName") txtLastName.Text = rowEmployee("LastName") txtAddress.Text = rowEmployee("Address")
Catch ex As Exception MessageBox.Show(ex.Message & " :: " & ex.Source) Finally End Try End Sub
Private Sub btnUpdate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnUpdate.Click Try rowEmployee("LastName") = txtLastName.Text da.Update(ds)
Catch ex As Exception
MessageBox.Show(ex.Message & " :: " & ex.Source)
End Try
End Sub End Class ---------------------------------------
When it runs I get the following error:
"Object reference not set to an instance of an object."
on the da.Update(ds) line in the btnUpdate_Click routine.
HELP.
Thanks,
Gary
Gary,
See the answer from Kerry what means
ds = New DataSet
da.Fill(ds, "Employees")
dim cmb as new OleDb.OleDbCommandbuilder(da)
Cor
Cor,
I made the change and I still get the object not referenced error.
Gary
"Cor Ligthert" <no************@planet.nl> wrote in message
news:%2****************@TK2MSFTNGP09.phx.gbl... Gary,
See the answer from Kerry what means
ds = New DataSet da.Fill(ds, "Employees") dim cmb as new OleDb.OleDbCommandbuilder(da)
Cor
Gary
This ones too
cn = New OleDb.OleDbConnection(strConn)
da = New OleDb.OleDbDataAdapter(strSQL, strConn)
Cor
Cor,
I made the changes you suggested but I get an error message when the update
is called: Update unable to fine TableMapping ['Table'] or DataTable
'Table'
Here is the code as it looks now:
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
Try
Dim strConn As String
strConn = "Provider=Microsoft.Jet.OLEDB.4.0;Data
Source=C:\adonetsbs\SampleDBs\nwind.mdb;"
cn = New OleDb.OleDbConnection(strConn)
Dim strSQL As String
strSQL = "SELECT EmployeeID, FirstName, LastName, Address, City,
Region, " & _
"PostalCode from Employees ORDER BY LastName, FirstName"
da = New OleDb.OleDbDataAdapter(strSQL, strConn)
ds = New DataSet
da.Fill(ds, "Employees")
Dim cmb As New OleDb.OleDbCommandBuilder(da)
Dim tbl As DataTable = ds.Tables(0)
'rowEmployee = New DataRow
rowEmployee = tbl.Rows(3)
txtFirstName.Text = rowEmployee("FirstName")
txtLastName.Text = rowEmployee("LastName")
txtAddress.Text = rowEmployee("Address")
Catch ex As Exception
MessageBox.Show(ex.Message & " :: " & ex.Source)
Finally
End Try
End Sub
"Cor Ligthert" <no************@planet.nl> wrote in message
news:eK**************@TK2MSFTNGP14.phx.gbl... Gary
This ones too
cn = New OleDb.OleDbConnection(strConn)
da = New OleDb.OleDbDataAdapter(strSQL, strConn)
Cor
Gary,
Sorry
da.Update(ds, "Employees")
Cor
Thanks Cor for your help in this. I will keep studying and hopefully I'll
be a real programmer one day!
Gary
"Cor Ligthert" <no************@planet.nl> wrote in message
news:OJ**************@tk2msftngp13.phx.gbl... Gary,
Sorry
da.Update(ds, "Employees")
Cor
This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Warren Wright |
last post by:
Hi All,
First, where can I get some questions of this sort answered?
Preferably, are there good books or online guides that I can consult for
these types of answers when necessary?
1. How do...
|
by: Sarah Tanembaum |
last post by:
Beside its an opensource and supported by community, what's the fundamental
differences between PostgreSQL and those high-price commercial database (and
some are bloated such as Oracle) from...
|
by: serge |
last post by:
/*
This is a long post. You can paste the whole message
in the SQL Query Analyzer.
I have a scenario where there are records
with values pointing to wrong records and I need to fix them
using an...
|
by: Jim Cobban |
last post by:
I have a set of web pages that are organized in pairs. One of each pair
contains a graphic and the other is an extended description of the graphic.
I am trying to set it up that selecting a single...
|
by: typingcat |
last post by:
First of all, I'm an Asian and I need to input Japanese, Korean and so
on. I've tried many PHP IDEs today, but almost non of them supported
Unicode (UTF-8) file.
I've found that the only Unicode...
|
by: Aaron Ackerman |
last post by:
I cannot a row to this bound DataGrid to SAVE MY LIFE! I have tried
everything and I am at a loss. The using goes into add mode with the add
button adds his data then updates with the update...
|
by: Brian Hampson |
last post by:
I am trying to determine all the groups which the current user has
permissions to add a member.
Here's my code:
foreach (System.DirectoryServices.SearchResult ADSearchres in...
|
by: SLIMSHIM |
last post by:
Hi,
I"m new to c# and .net. I wrote a small program to add rows to an access
table.
the program goes thru the motions but the data never gets there.
here is my code. I am intentionaly not using...
|
by: raylopez99 |
last post by:
I am running out of printing paper trying to debug this...it has to be
trivial, but I cannot figure it out--can you? Why am I not printing
text, but just the initial string "howdy"?
On 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:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
|
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: 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...
|
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: 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,...
|
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...
| |