I am creating a bank customer's details application. The information is stored on a Microsoft Access file and I was able to make the information appear on my VB application through text boxes and a DataGridView but I am not able to save,edit or delete records. Does anybody have any ideas. Below is my code. -
-
Public Class frmCustomerDetails
-
-
Dim objDS As New DataSet
-
Dim rowIndex As Integer = 0
-
Dim objConn As String = "Provider = Microsoft.Jet.OLEDB.4.0;" & "Data Source=F:\Database-driven banking application\Database-driven banking application\bin\CustomerDetails.mdb"
-
-
Dim objSqlStr As String = "SELECT * FROM CustomerDetails"
-
Dim objDA As New OleDb.OleDbDataAdapter(objSqlStr, objConn)
-
-
-
-
-
Sub UpdateTextBoxes()
-
-
txtCustomerID.Text = CStr(objDS.Tables(0).Rows(rowIndex)("CustomerID"))
-
txtFirstName.Text = CStr(objDS.Tables(0).Rows(rowIndex)("FirstName"))
-
txtLastName.Text = CStr(objDS.Tables(0).Rows(rowIndex)("LastName"))
-
txtAddress.Text = CStr(objDS.Tables(0).Rows(rowIndex)("Address"))
-
txtHomeNumber.Text = CStr(objDS.Tables(0).Rows(rowIndex)("HomeNumber"))
-
txtMobileNumber.Text = CStr(objDS.Tables(0).Rows(rowIndex)("MobileNumber"))
-
txtNotes.Text = CStr(objDS.Tables(0).Rows(rowIndex)("Notes"))
-
-
End Sub
-
-
-
Private Sub btnNext_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnNext.Click
-
-
If rowIndex < objDS.Tables(0).Rows.Count - 1 Then
-
rowIndex = rowIndex + 1
-
UpdateTextBoxes()
-
-
End If
-
-
End Sub
-
-
Private Sub btnPrevious_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPrevious.Click
-
-
If rowIndex > 0 Then
-
rowIndex = rowIndex - 1
-
UpdateTextBoxes()
-
End If
-
-
End Sub
-
-
Private Sub txtFind_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtFind.Click
-
-
Dim rowcount As Integer
-
rowcount = objDS.Tables(0).Rows.Count - 1
-
-
Dim ownerFound As Boolean = False
-
Dim lastname As String = InputBox("Enter a last name Please!", "Search")
-
For i As Integer = 0 To objDS.Tables(0).Rows.Count - 1
-
If CStr(objDS.Tables(0).Rows(i)("LastName")) = lastname Then
-
ownerFound = True
-
rowIndex = i
-
UpdateTextBoxes()
-
-
End If
-
Next
-
-
End Sub
-
-
Private Sub frmCustomerDetails_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
-
-
objDA.Fill(objDS)
-
objDA.Dispose()
-
-
DataGridView1.DataSource = objDS.Tables(0)
-
UpdateTextBoxes()
-
-
End Sub
-
-
-
Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click
-
-
Dim objRow As DataRow
-
Dim FirstName As String
-
Dim LastName As String
-
-
FirstName = txtFirstName.Text
-
LastName = txtLastName.Text
-
-
'Create a new DataRow object for this table
-
objRow = objDS.Tables("CustomerDetails").NewRow
-
'Edit Each Field value
-
objRow.Item("FirstName") = FirstName
-
objRow.Item("LastName") = LastName
-
-
-
-
'Officially add the DataRow to our table
-
objDS.Tables("CustomerDetails").Rows.Add(objRow)
-
-
objDA.Update(objDS, "CustomerDetails")
-
-
-
End Sub
-
-
Private Sub btnFirst_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnFirst.Click
-
-
rowIndex = 0
-
UpdateTextBoxes()
-
-
End Sub
-
-
Private Sub btnCreate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCreate.Click
-
-
-
-
-
-
-
End Sub
-
-
Private Sub btnDelete_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDelete.Click
-
-
If Not DataGridView1.CurrentRow.IsNewRow Then
-
DataGridView1.Rows.Remove(DataGridView1.CurrentRow)
-
End If
-
-
End Sub
-
End Class
-
-
15 1924
There are a number of problems with this code. Suggest debugging and trying to solve some first. Thanks.
It actually only throws up one error in run time and that is line 84. The error is 'Object reference not set to an instance of an object.'
Problem is due to ur dataset object objDS
At time of Add, Delete or Edit event u r dataset is null. so u must store dataset in session and on each event take it from session and then do operation u want.
Problem is due to ur dataset object objDS
At time of Add, Delete or Edit event u r dataset is null. so u must store dataset in session and on each event take it from session and then do operation u want.
Hi thanks,
How would I do that if u dont mind telling me??
Hi thanks,
How would I do that if u dont mind telling me??
I see when you reqest the table from your dataset that you give the table a name. However, no where do I see you assigning that name to the table you have. My guess would be that since there is no table with that name in the dataset, it throws that error.
As for checking for null, unsure how in VB, but I think it's like -
if objDS.Tables("CustomerDetails") is null
-
'something to do if dataset is null
-
end if
-
Excellent I've got it to sabe in run time but does'nt stayed saved in the database. It adds to the DataGridView in run time but when i start the application again the new records are deleted.
When i do add a new record in run time a marker comes up in the datagrid saying ' Update requires a valid InsertCommand when passed DataRow collection with new rows.'
Anyone have any ideas??
Do you have an insert command in your dataadapter? If so what is it?
I don't think so. All my code is above. Im not familar with insert command!
I don't think so. All my code is above. Im not familar with insert command!
Well that would be why it is telling you it doesn't have the insert command, because it doesn't!
You will need to create an SQL INSERT statement for your DataAdapter
alright very good,
how and where would i put the code??
alright very good,
how and where would i put the code??
As an OleDbDataAdapter, objDA has .InsertCommand and .UpdateCommand which wnat instances of OleDbCommand, create your SQL statements in those command objects and assign them to OleDbDataAdapter
should i still be using sql statements if my information comes from an access database!??
Well considering SQL is what you use to talk to pretty much ALL databases, yes.
You are already using an SQL SELECT statement: -
Dim objSqlStr As String = "SELECT * FROM CustomerDetails"
-
You even include the Sql in the variable name...
Should this be the code so, I put it in the first few lines with the other OleDb's? -
-
Dim objDC As New OleDb.OleDbCommand(objDA)
-
-
Something like: -
Dim objSqlInsertStr As String = "INSERT INTO CustomerDetails (NameOfColumn1, NameOfColumn2, etc) VALUES (@Variable1, @Variable2, etc)"
-
Dim objDI As New OleDb.OleDbCommad(objSqlInsertStr, objConn)
-
'You will need to look up how to use the INSERT command
-
objDA.InsertCommand = objDI
-
Sign in to post your reply or Sign up for a free account.
Similar topics
by: Henok Girma |
last post by:
Hello Gurus,
I want to save the state of an unbound DataGridView on my Windows Form
application to an XML file so i can later load it back..
Basically, on my form I have a DataGridView, it's got...
|
by: Rich |
last post by:
Hello,
I need to pull data from 2 tables that reside on the same sql server but 2
different databases. In Query Analyzer I can say this
select t1.*, t2.* from tbl1 t1 join database2.dbo.tbl1...
|
by: Rich |
last post by:
Hello,
I am populating a datagridview from a datatable and filtering the number of
rows with a dataview object. Is there a way to retrieve the rows displayed
by the datagridview into a separate...
|
by: bob |
last post by:
Can anyone tell me the best way to update a dataset while it is being
edited/viewed in the DataGridView control? Is this something that
should be inserted into one of the grid's events? or should...
|
by: Mitchell S. Honnert |
last post by:
Is there an equivalent of the DataGrid's DataGridTableStyle for the
DataGridView? If not, is there an easy way to duplicate the
DataGridTableStyle's functionality for the DataGridView?
Here's...
|
by: =?Utf-8?B?TG9zdEluTUQ=?= |
last post by:
Hi All :)
I'm converting VB6 using True DBGrid Pro 8.0 to VB2005 using DataGridView.
True DBGrid has a MultipleLines property that controls whether individual
records span multiple lines. Is...
|
by: jeastman - Hotmail |
last post by:
Hello world
Excuse, not to be written English and it helps me with a translator.
I am new programming in C#.
I made a control inheriting the DataGridView to be able to add controls done
by...
|
by: Andrus |
last post by:
I have DataGridView in virtual mode containing 3500 rows.
In code below, assigning to RowCount value to 3500 takes 8 seconds.
CPU usage goes high at this time.
Stepping by F11 into user code shows...
|
by: priyamtheone |
last post by:
I'm trying to make a datagridview column to act like a datetimepicker column (C#.Net 2005). These are the behaviours that the dgv should have:
1) Initially all the cells of the dtp column should be...
|
by: taylorcarr |
last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
|
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: 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
|
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: 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...
|
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...
| |