Problem while Inserting and Updating data into MS access

Please support our VB.NET advertiser: Intel Parallel Studio Home
Reply

Join Date: Mar 2009
Posts: 2
Reputation: jallan2009 is an unknown quantity at this point 
Solved Threads: 0
jallan2009 jallan2009 is offline Offline
Newbie Poster

Problem while Inserting and Updating data into MS access

 
0
  #1
Mar 16th, 2009
Sir,
I am using front end as a VB.net 2005 and I am a beginner in this language. I am creating Desktop application and using controls TAB controls 1st tab control is Contact in contact I am using one text box, two button and one datagrid while I am using the oledb database i am inserting the data through text box and correspondingly that data will display on datagrid and there is my following coding to add contact.

In the contactdb file i have created contact table and the fields are ContactID and ContactName


Private Sub AddContact()
Try
Dim myDataSet As DataSet = New DataSet()
Dim cmd As OleDbCommand
Dim con As OleDbConnection
Dim str As String
'Dim da As New OleDbDataAdapter



Dim connectionString As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=D:\Contactdb.mdb"

con = New OleDbConnection(connectionString)
cmd = New OleDbCommand("insert into Contact(ContactName) values(ContactName)", con)
With cmd.Parameters
.Add("@ContactName", OleDbType.VarChar).Value = connametxtbox.Text
End With
con.Open()
cmd.ExecuteNonQuery()
con.Close()
' Close and Clean up objects

DataGridView1.DataSource = myDataSet.Tables("Contact").DefaultView
con.Dispose()
cmd.Dispose()

Catch ex As OleDbException
MsgBox(ex.Message.ToString)
insertOk = False
End Try
If insertOk Then
MsgBox("Contact Submitted Successfully!")
Else : MsgBox("There was an error saving your data!")
End If
End Sub


1) The problem is that while inserting data through text box it will insert but it will not display on my MS access file i.e. Contactdb what is the reason behind that please guide me for that please.

2) That the data in datagrid i.e. contact name by selecting the contact name it will display in the text box by datagrid cell click event by using this i want to update the contact name only. How I can update the Contact Name please guide me.

where is my mistake is going on.

Thanks in advance
Reply With Quote Quick reply to this message  
Join Date: May 2008
Posts: 51
Reputation: martonx is an unknown quantity at this point 
Solved Threads: 8
martonx martonx is offline Offline
Junior Poster in Training

Re: Problem while Inserting and Updating data into MS access

 
0
  #2
Mar 16th, 2009
You are using MS access database, so why are you writing ugly code? The framework can do all your job.
There are tons of video tutorials, for example here:

http://msdn.microsoft.com/en-us/vstudio/aa700732.aspx
Reply With Quote Quick reply to this message  
Join Date: Mar 2009
Posts: 66
Reputation: c0deFr3aK is an unknown quantity at this point 
Solved Threads: 10
c0deFr3aK's Avatar
c0deFr3aK c0deFr3aK is offline Offline
Junior Poster in Training

Re: Problem while Inserting and Updating data into MS access

 
0
  #3
Mar 17th, 2009
I will show you another arrangement of coding which is pretty simple to read and understand

First Create a Module where you store your Connection to your Database

Module:
  1. Option Explicit On
  2. Imports System
  3. Imports System.Data
  4. Imports System.Data.OLEDB
  5.  
  6. Module DBCONN 'DBCONN is the name of your module
  7. Public conn As New System.Data.OLEDB.Connection 'call this connection anywhere anytime in your application
  8. Sub DBLOAD()
  9. Dim connstring as String = "Provider = Microsoft.Jet.OLEDB.4.0;Data Source = C:\DATA\MyDatabase.MDB; Persist Security Info = False;"
  10.  
  11. If conn.State = ConnectionState.Open Then conn.Close
  12. With conn
  13. Try
  14. .ConnectionString = connString
  15. .Open
  16. Catch ex As Exception
  17. Messagebox.Show(ex.Message,"Error Connection")
  18. End Try
  19. End Sub
  20.  
  21. End Module

Note: change the path of the Data Source to your own
So we will no insert some data into the database
  1. Private Sub btnSave_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSave.Click
  2. Dim sql As String ="SELECT * FROM ContactDB WHERE ContactName = '" & Me.TextboxName.Text &"'" 'assuming that a Primary key is Autonumber
  3. Dim da As New OLEDB.OledbAdapter(sql, conn) 'conn is called from the module
  4. Dim ds As New DataSet
  5. Dim cb As New OLEDB.OledbCommandBuilder(da)
  6. Dim dt As DataTable, row As DataRow
  7.  
  8. da.Fill(ds)
  9. dt = ds.Tables(0)
  10. row = dt.NewRow
  11.  
  12. row("CondtactName") = Me.TextboxName.Text
  13. 'put your other table fields here
  14.  
  15. 'we will check if the Contact Name exists or not in your DB
  16. If ds.Tables(0).Rows.Count = 0 Then
  17. dt.Rows.Add(row)
  18. da.Update(ds)
  19. MsgBox ("Insert Successful")
  20. ContactShow() ' we will show our newly inserted data into our datagrid
  21. Exit Sub
  22. Else
  23. MsgBox (""& Me.TextboxName.Text &"" & " Already in your Database")
  24. End If
  25.  
  26. End Sub

So we will Display our inserted data in the Datagrid

We will make a custom datagrid so that it will look professional
Assuming that the Name of our Datagrid is ContactDG

We will make a sub
  1. Private sub ContactShow()
  2. Me.ContactDG.TableStyle.Clear
  3. Dim sql As String ="SELECT * FROM ContactDB ORDER BY ContactName"
  4. Dim da As New OLEDB.OledbAdapter(sql, conn)
  5. Dim ds As New DataSet
  6. Dim dt As DataTable
  7.  
  8. da.Fill(ds)
  9. dt = ds.Tables(0)
  10.  
  11. 'We will customize our Datagrid Tablestyle
  12. Dim ts As New DatagridTableStle
  13. ts.MappingName = dt.TableName
  14. ts.RowHeaderVisible = False 'we will not display datagrid row header
  15. 'you can customize gridline color etc.
  16.  
  17. Dim col0 As New DataGridTextboxColumn
  18. col0.Alignment = HorizontalAlignment.Left
  19. col0.HeadeText = "Contact Name"
  20. col0.MappingName ="ContactName" 'this is our database field
  21. col0.Width = 250
  22. ts.GridColumnStyle.Add(col0)
  23.  
  24. With ContactDG
  25. .TableStyles.Add(ts)
  26. .DataSource = dt
  27. .CaptionVissible = False
  28. .ReadOnly = True
  29. End With
  30. End Sub

On mouseclick event as you said you want to display back your data in the textbox for editing.

Here is a Simple code
This also Select the entire row of your Datagrid as you click your mouse

  1. Private Sub ContactDG_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles ContactDG.MouseUp
  2.  
  3. Dim pt As New Point(e.X,e.Y)
  4. Dim hit As DataGrid.HitTestInfo = Me.ContactDG.HitTest(pt)
  5. If hit.Type = DataGrid.HitTestType.Cell Then
  6. Me.ContactDG.CurrentCell = New DataGridCell(hit.Row, hit.Column)
  7. Me.ContactDG.Select(hit.Row)
  8. 'we will throw back Contact Name to the TextboxName
  9. Me.TextboxName.Text = Me.ContactDG.Items(ContactDG.CurrentRowIndex, 0)
  10. End If
  11.  
  12. End Sub

Now we will Update our TextboxName assumin there were error in the Spelling

  1. Private Sub btnUpdate_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnUpdate.Click
  2.  
  3. Dim sql As String ="SELECT * FROM ContactDB WHERE ContactName = '" & Me.TextboxName.Text & "'"
  4. Dim da As New OLEDB.OledbAdapter(sql, conn)
  5. Dim ds As New DataSet
  6. Dim cb As New OLEDB.OledbCommandBuilder(da)
  7. da.Fill(ds)
  8. ds.Tables(0).Rows(0).Item("ContactName") = Me.TextboxName.Text
  9. da.Update(ds)
  10. MsgBox ("Update Successful")
  11.  
  12. End Sub
So i guess you have now an idea of what will you add to this codes
I hope it helps
Last edited by c0deFr3aK; Mar 17th, 2009 at 10:55 pm.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Other Threads in the VB.NET Forum
Thread Tools Search this Thread



Tag cloud for VB.NET
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC