473,387 Members | 1,344 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,387 software developers and data experts.

Databases with DataGridView

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.

Expand|Select|Wrap|Line Numbers
  1.  
  2. Public Class frmCustomerDetails
  3.  
  4.     Dim objDS As New DataSet
  5.     Dim rowIndex As Integer = 0
  6.     Dim objConn As String = "Provider = Microsoft.Jet.OLEDB.4.0;" & "Data Source=F:\Database-driven banking application\Database-driven banking application\bin\CustomerDetails.mdb"
  7.  
  8.     Dim objSqlStr As String = "SELECT * FROM CustomerDetails"
  9.     Dim objDA As New OleDb.OleDbDataAdapter(objSqlStr, objConn)
  10.  
  11.  
  12.  
  13.  
  14.     Sub UpdateTextBoxes()
  15.  
  16.         txtCustomerID.Text = CStr(objDS.Tables(0).Rows(rowIndex)("CustomerID"))
  17.         txtFirstName.Text = CStr(objDS.Tables(0).Rows(rowIndex)("FirstName"))
  18.         txtLastName.Text = CStr(objDS.Tables(0).Rows(rowIndex)("LastName"))
  19.         txtAddress.Text = CStr(objDS.Tables(0).Rows(rowIndex)("Address"))
  20.         txtHomeNumber.Text = CStr(objDS.Tables(0).Rows(rowIndex)("HomeNumber"))
  21.         txtMobileNumber.Text = CStr(objDS.Tables(0).Rows(rowIndex)("MobileNumber"))
  22.         txtNotes.Text = CStr(objDS.Tables(0).Rows(rowIndex)("Notes"))
  23.  
  24.     End Sub
  25.  
  26.  
  27.     Private Sub btnNext_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnNext.Click
  28.  
  29.         If rowIndex < objDS.Tables(0).Rows.Count - 1 Then
  30.             rowIndex = rowIndex + 1
  31.             UpdateTextBoxes()
  32.  
  33.         End If
  34.  
  35.     End Sub
  36.  
  37.     Private Sub btnPrevious_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPrevious.Click
  38.  
  39.         If rowIndex > 0 Then
  40.             rowIndex = rowIndex - 1
  41.             UpdateTextBoxes()
  42.         End If
  43.  
  44.     End Sub
  45.  
  46.     Private Sub txtFind_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtFind.Click
  47.  
  48.         Dim rowcount As Integer
  49.         rowcount = objDS.Tables(0).Rows.Count - 1
  50.  
  51.         Dim ownerFound As Boolean = False
  52.         Dim lastname As String = InputBox("Enter a last name Please!", "Search")
  53.         For i As Integer = 0 To objDS.Tables(0).Rows.Count - 1
  54.             If CStr(objDS.Tables(0).Rows(i)("LastName")) = lastname Then
  55.                 ownerFound = True
  56.                 rowIndex = i
  57.                 UpdateTextBoxes()
  58.  
  59.             End If
  60.         Next
  61.  
  62.     End Sub
  63.  
  64.     Private Sub frmCustomerDetails_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
  65.  
  66.         objDA.Fill(objDS)
  67.         objDA.Dispose()
  68.  
  69.         DataGridView1.DataSource = objDS.Tables(0)
  70.         UpdateTextBoxes()
  71.  
  72.     End Sub
  73.  
  74.  
  75.     Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click
  76.  
  77.         Dim objRow As DataRow
  78.         Dim FirstName As String
  79.         Dim LastName As String
  80.  
  81.         FirstName = txtFirstName.Text
  82.         LastName = txtLastName.Text
  83.  
  84.         'Create a new DataRow object for this table
  85.         objRow = objDS.Tables("CustomerDetails").NewRow
  86.         'Edit Each Field value
  87.         objRow.Item("FirstName") = FirstName
  88.         objRow.Item("LastName") = LastName
  89.  
  90.  
  91.  
  92.         'Officially add the DataRow to our table
  93.         objDS.Tables("CustomerDetails").Rows.Add(objRow)
  94.  
  95.         objDA.Update(objDS, "CustomerDetails")
  96.  
  97.  
  98.     End Sub
  99.  
  100.     Private Sub btnFirst_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnFirst.Click
  101.  
  102.         rowIndex = 0
  103.         UpdateTextBoxes()
  104.  
  105.     End Sub
  106.  
  107.     Private Sub btnCreate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCreate.Click
  108.  
  109.  
  110.  
  111.  
  112.  
  113.  
  114.     End Sub
  115.  
  116.     Private Sub btnDelete_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDelete.Click
  117.  
  118.         If Not DataGridView1.CurrentRow.IsNewRow Then
  119.             DataGridView1.Rows.Remove(DataGridView1.CurrentRow)
  120.         End If
  121.  
  122.     End Sub
  123. End Class
  124.  
  125.  
Feb 17 '08 #1
15 1924
kenobewan
4,871 Expert 4TB
There are a number of problems with this code. Suggest debugging and trying to solve some first. Thanks.
Feb 18 '08 #2
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.'
Feb 19 '08 #3
kunal pawar
297 100+
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.
Feb 19 '08 #4
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??
Feb 19 '08 #5
Plater
7,872 Expert 4TB
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
Expand|Select|Wrap|Line Numbers
  1. if objDS.Tables("CustomerDetails") is null
  2. 'something to do if dataset is null
  3. end if
  4.  
Feb 19 '08 #6
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??
Feb 19 '08 #7
kenobewan
4,871 Expert 4TB
Do you have an insert command in your dataadapter? If so what is it?
Feb 20 '08 #8
I don't think so. All my code is above. Im not familar with insert command!
Feb 20 '08 #9
Plater
7,872 Expert 4TB
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
Feb 20 '08 #10
alright very good,

how and where would i put the code??
Feb 20 '08 #11
Plater
7,872 Expert 4TB
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
Feb 20 '08 #12
should i still be using sql statements if my information comes from an access database!??
Feb 20 '08 #13
Plater
7,872 Expert 4TB
Well considering SQL is what you use to talk to pretty much ALL databases, yes.
You are already using an SQL SELECT statement:
Expand|Select|Wrap|Line Numbers
  1. Dim objSqlStr As String = "SELECT * FROM CustomerDetails"
  2.  
You even include the Sql in the variable name...
Feb 20 '08 #14
Should this be the code so, I put it in the first few lines with the other OleDb's?

Expand|Select|Wrap|Line Numbers
  1.  
  2. Dim objDC As New OleDb.OleDbCommand(objDA)
  3.  
  4.  
Feb 20 '08 #15
Plater
7,872 Expert 4TB
Something like:
Expand|Select|Wrap|Line Numbers
  1. Dim objSqlInsertStr As String = "INSERT INTO CustomerDetails (NameOfColumn1, NameOfColumn2, etc) VALUES (@Variable1, @Variable2, etc)"
  2. Dim objDI As New OleDb.OleDbCommad(objSqlInsertStr, objConn)
  3. 'You will need to look up how to use the INSERT command 
  4. objDA.InsertCommand = objDI
  5.  
Feb 20 '08 #16

Sign in to post your reply or Sign up for a free account.

Similar topics

10
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...
3
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...
3
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...
2
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...
7
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...
7
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...
0
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...
3
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...
0
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...
0
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,...
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: 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...
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...
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
marktang
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,...
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.