Problem with update Access database using windows application front end

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

Join Date: Jul 2009
Posts: 11
Reputation: speedy gonzalos is an unknown quantity at this point 
Solved Threads: 0
speedy gonzalos speedy gonzalos is offline Offline
Newbie Poster

Problem with update Access database using windows application front end

 
0
  #1
Jul 31st, 2009
I am trying to create an Inventory/Stock Control system as a windows application using vb.net with an Access database. This is my first time trying to use vb.net and im having difficulty making the application interactive. I am unable to update records to the database from the front end.

I am creating this application in Visual Studio 2008. The first problem that i would like to resolve is trying to update records to the database. My front-end consists of a datagrid at the top of the screen, which brings back all of the records from Qry_Categories. I then have text boxes below the datagrid that replicate the data in the grid above it. So which ever row is highlighted in the datagrid, that data is then automatically filled in the text boxes below it.

I then have an update button. To update a record the user will highlight the row in the datagrid, this information will then get filled in the text boxes. The user will make changes to any of the text boxes and then click on the 'Update' button. This is where I am having the problems. Please can someone provide me with the update button code that will work, as i have tried so many and none seem to work.

This is my code so far:
  1.  
  2. Public Class Form1
  3. Dim con As New OleDb.OleDbConnection
  4. Dim adp As OleDb.OleDbDataAdapter
  5. Dim cmd As OleDb.OleDbCommand = New OleDb.OleDbCommand(adp)
  6. Dim ds As New DataSet
  7.  
  8.  
  9. Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
  10. 'TODO: This line of code loads data into the 'StockControlDataSet.LK_Categories' table. You can move, or remove it, as needed.
  11. Me.LK_CategoriesTableAdapter.Fill(Me.StockControlDataSet.LK_Categories)
  12.  
  13. con.ConnectionString = ConfigurationManager.ConnectionStrings _
  14. ("WRC_SMS_1.My.MySettings.StockControlConnectionString").ConnectionString.ToString()
  15. ' Fill the data grid viewer
  16. con.Open()
  17. cmd = New OleDb.OleDbCommand("Select * from Qry_Categories", con)
  18. adp = New OleDb.OleDbDataAdapter(cmd)
  19. adp.Fill(ds, "Qry_Categories")
  20. Me.LK_CategoriesDataGridView.DataSource = ds
  21. Me.LK_CategoriesDataGridView.DataMember = "Qry_Categories"
  22. End Sub
  23.  
  24. Private Sub btnUpdate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnUpdate.Click
  25. 'open(connection)
  26. con.Open()
  27.  
  28. cmd.Connection = con
  29. Try
  30. cmd.CommandText = "UPDATE LK_Categories SET CategoryName = @CategoryName WHERE PK_CategoryID = @CatID"
  31. '"UPDATE LK_Categories SET CategoryName = @CategoryName WHERE PK_CategoryID = 16"
  32. Dim param As New OleDb.OleDbParameter
  33. Dim param2 As New OleDb.OleDbParameter
  34.  
  35.  
  36. 'add content txtProdName as parameter
  37. param.ParameterName = "@CategoryName"
  38. param.Value = txtbxCatName.Text()
  39. cmd.Parameters.Add(param)
  40.  
  41. param2.ParameterName = "@CatID"
  42. param2.Value = txtCatID.Text
  43. cmd.Parameters.Add(param2)
  44.  
  45. cmd.ExecuteNonQuery()
  46.  
  47. MessageBox.Show("customer data successfully updated")
  48.  
  49. Catch ex As Exception
  50.  
  51. MessageBox.Show("cust data not updated because " _
  52. & ex.Message)
  53.  
  54. Finally
  55. 'close connection if it is open
  56. If (con.State = ConnectionState.Open) Then
  57. con.Close()
  58. End If
  59. End Try
  60. con.Close()
  61.  
  62. End Sub

The Qry_Categories has the following field names:
PK_CategoryID
CategoryName
Description

I would really appreciate the help as it is urgent i get this application up and running. Thanks in advance
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 2,597
Reputation: adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of 
Solved Threads: 459
Moderator
adatapost's Avatar
adatapost adatapost is online now Online
Posting Maven

Re: Problem with update Access database using windows application front end

 
0
  #2
Jul 31st, 2009
Welcome speedy gonzalos,
Do not use @ with parameter operand if database is Ms-Access.
  1. cmd.CommandText = "UPDATE LK_Categories SET CategoryName = mCategoryName WHERE PK_CategoryID =mCatID"
Or use ? (anonymous)
  1. cmd.CommandText = "UPDATE LK_Categories SET CategoryName = ? WHERE PK_CategoryID =?"
Failure is not fatal, but failure to change might be. - John Wooden
Reply With Quote Quick reply to this message  
Join Date: Jul 2009
Posts: 11
Reputation: speedy gonzalos is an unknown quantity at this point 
Solved Threads: 0
speedy gonzalos speedy gonzalos is offline Offline
Newbie Poster

Re: Problem with update Access database using windows application front end

 
0
  #3
Jul 31st, 2009
Originally Posted by speedy gonzalos View Post
I am trying to create an Inventory/Stock Control system as a windows application using vb.net with an Access database. This is my first time trying to use vb.net and im having difficulty making the application interactive. I am unable to update records to the database from the front end.

I am creating this application in Visual Studio 2008. The first problem that i would like to resolve is trying to update records to the database. My front-end consists of a datagrid at the top of the screen, which brings back all of the records from Qry_Categories. I then have text boxes below the datagrid that replicate the data in the grid above it. So which ever row is highlighted in the datagrid, that data is then automatically filled in the text boxes below it.

I then have an update button. To update a record the user will highlight the row in the datagrid, this information will then get filled in the text boxes. The user will make changes to any of the text boxes and then click on the 'Update' button. This is where I am having the problems. Please can someone provide me with the update button code that will work, as i have tried so many and none seem to work.

This is my code so far:
  1.  
  2. Public Class Form1
  3. Dim con As New OleDb.OleDbConnection
  4. Dim adp As OleDb.OleDbDataAdapter
  5. Dim cmd As OleDb.OleDbCommand = New OleDb.OleDbCommand(adp)
  6. Dim ds As New DataSet
  7.  
  8.  
  9. Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
  10. 'TODO: This line of code loads data into the 'StockControlDataSet.LK_Categories' table. You can move, or remove it, as needed.
  11. Me.LK_CategoriesTableAdapter.Fill(Me.StockControlDataSet.LK_Categories)
  12.  
  13. con.ConnectionString = ConfigurationManager.ConnectionStrings _
  14. ("WRC_SMS_1.My.MySettings.StockControlConnectionString").ConnectionString.ToString()
  15. ' Fill the data grid viewer
  16. con.Open()
  17. cmd = New OleDb.OleDbCommand("Select * from Qry_Categories", con)
  18. adp = New OleDb.OleDbDataAdapter(cmd)
  19. adp.Fill(ds, "Qry_Categories")
  20. Me.LK_CategoriesDataGridView.DataSource = ds
  21. Me.LK_CategoriesDataGridView.DataMember = "Qry_Categories"
  22. End Sub
  23.  
  24. Private Sub btnUpdate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnUpdate.Click
  25. 'open(connection)
  26. con.Open()
  27.  
  28. cmd.Connection = con
  29. Try
  30. cmd.CommandText = "UPDATE LK_Categories SET CategoryName = @CategoryName WHERE PK_CategoryID = @CatID"
  31. '"UPDATE LK_Categories SET CategoryName = @CategoryName WHERE PK_CategoryID = 16"
  32. Dim param As New OleDb.OleDbParameter
  33. Dim param2 As New OleDb.OleDbParameter
  34.  
  35.  
  36. 'add content txtProdName as parameter
  37. param.ParameterName = "@CategoryName"
  38. param.Value = txtbxCatName.Text()
  39. cmd.Parameters.Add(param)
  40.  
  41. param2.ParameterName = "@CatID"
  42. param2.Value = txtCatID.Text
  43. cmd.Parameters.Add(param2)
  44.  
  45. cmd.ExecuteNonQuery()
  46.  
  47. MessageBox.Show("customer data successfully updated")
  48.  
  49. Catch ex As Exception
  50.  
  51. MessageBox.Show("cust data not updated because " _
  52. & ex.Message)
  53.  
  54. Finally
  55. 'close connection if it is open
  56. If (con.State = ConnectionState.Open) Then
  57. con.Close()
  58. End If
  59. End Try
  60. con.Close()
  61.  
  62. End Sub

The Qry_Categories has the following field names:
PK_CategoryID
CategoryName
Description

I would really appreciate the help as it is urgent i get this application up and running. Thanks in advance
Thanks ever so much for your help that worked!

I also removed this bit of the code from the top:
  1. ' Fill the data grid viewer
  2. con.Open()
  3. cmd = New OleDb.OleDbCommand("Select * from Qry_Categories", con)
  4. adp = New OleDb.OleDbDataAdapter(cmd)
  5. adp.Fill(ds, "Qry_Categories")
  6. Me.LK_CategoriesDataGridView.DataSource = ds
  7. Me.LK_CategoriesDataGridView.DataMember = "Qry_Categories"
Reply With Quote Quick reply to this message  
Join Date: Jul 2009
Posts: 11
Reputation: speedy gonzalos is an unknown quantity at this point 
Solved Threads: 0
speedy gonzalos speedy gonzalos is offline Offline
Newbie Poster

Re: Problem with update Access database using windows application front end

 
0
  #4
Jul 31st, 2009
The only problem now is that i can update only 1 record at a time. If i want to update 2 or more records at the same time, the changes will only show on the datagrid view but not update the database.

I will have to re-run the application each time i want to update a record. Can you tell me how i can make unlimited changes to the database withoout having to close and then re open the application?
Reply With Quote Quick reply to this message  
Reply

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


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC