473,748 Members | 7,590 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Databases with DataGridView

51 New Member
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 1948
kenobewan
4,871 Recognized Expert Specialist
There are a number of problems with this code. Suggest debugging and trying to solve some first. Thanks.
Feb 18 '08 #2
javatech007
51 New Member
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 Contributor
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
javatech007
51 New Member
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 Recognized Expert Expert
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
javatech007
51 New Member
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 Recognized Expert Specialist
Do you have an insert command in your dataadapter? If so what is it?
Feb 20 '08 #8
javatech007
51 New Member
I don't think so. All my code is above. Im not familar with insert command!
Feb 20 '08 #9
Plater
7,872 Recognized Expert Expert
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

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

Similar topics

10
39511
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 some DataGridViewTextBoxCell, DataGridViewComboBoxCell etc, i want to export all that to XML.. Any help is greatly appreciated.
3
4324
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 t2 on t1.RecordID = t2.RecordID The .Net sql command object does not appear to support this kind of syntax. I need to populate one datagridview with the data from DB1.tbl1 and
3
32272
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 datatable without having to loop through each column in the datagridview? Or is there a way to retrieve the rows from the original datatable filtered by the dataview into a separate table? I only want to copy the rows from the main table that...
2
24556
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 you update after closing the grid/form, etc.? Also, can you tell me the best book to buy that fully explains the DataGridView control? Thanks.
7
12629
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 the background for my question... Before I switched my application over to the Fx 2.0, I used a DataGrid to display my data. I would store different DataGridTableStyles (each one with a custom set of columns) in the DataGrid.TableStyles property...
7
15658
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 there an equivalent property for the DataGridView? I have searched, but have not found one. I would like the user to be able to see all the columns of the table on one screen - thus eliminating the need to use the horizontal scroll bar to view...
0
2505
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 my.
3
5740
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 few celltemplate property getters and combobox/datecombo constructor calls without database access which does not take a lot of time. Debug output (below) shows lot of messages Stepping over non-user code. Running in release mode from...
0
5656
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 blank unless they are filled by the user. 2) As soon as the user enters a cell, the dtp control should appear as the editing control of that cell. If there's a value in the cell beforehand, that value is set as the value of the dtp editing control...
0
8991
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8831
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9548
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
9325
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9249
tracyyun
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6796
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6076
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
3315
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2787
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.