Solved

Saving record in VB2010

Posted on 2011-03-18
5
886 Views
Last Modified: 2012-08-13
Hello,

I have a backend database as my bindingsource, and am attempting to use a Save command button to reflect the changes on my form into my database.  Please advise.

Thank you.

Option Explicit On

Imports System
Imports System.Data
Imports System.Data.OleDb
Imports System.Data.SqlClient
Imports System.Windows.Forms.DataGridView

Public Class frmInput
    Dim sqlString As String = "SELECT * FROM tblInsuranceClaim"
    Dim ConnString As String = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Documents and Settings\Administrator\Desktop\MyDBBackend.mdb;User ID=admin"
    Dim OleDBConn1 As System.Data.OleDb.OleDbConnection = New System.Data.OleDb.OleDbConnection(ConnString)
    Dim OleDbDataAdapter1 As System.Data.OleDb.OleDbDataAdapter = New System.Data.OleDb.OleDbDataAdapter(sqlString, OleDBConn1)
    Dim dbGen As New OleDbCommandBuilder(OleDbDataAdapter1)

    Private Sub frmOrder_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        'TODO: This line of code loads data into the 'MyDBBackendDataSet3.tblInsuranceClaim' table. You can move, or remove it, as needed.
        Me.TblInsuranceClaimTableAdapter.Fill(Me.MyDBBackendDataSet.tblInsuranceClaim)
        txtPDrug.Text = frmDrugs.txtDrugHidden.Text

        Try
            OleDbDataAdapter1.AcceptChangesDuringUpdate = True
        Catch ex As Exception
            MsgBox("An exception was thrown because the back-end database is currently not allowing updates. Please try again.")
        End Try

        Try
            OleDBConn1.Open()
        Catch ex As Exception
            MsgBox("An exception was thrown because a connection to the database or database tables cannot be established")
        End Try

        TblInsuranceClaimBindingSource.AddNew()
    End Sub

    Private Sub cmdSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdSave.Click
        Try
            Me.Validate()
            Me.TblInsuranceClaimBindingSource.EndEdit()
            OleDbDataAdapter1.Update(Me.MyDBBackendDataSet.tblInsuranceClaim)
            MyDBBackendDataSet.Tables("tblInsuranceClaim").AcceptChanges()
            OleDbDataAdapter1.Update(MyDBBackendDataSet, "tblInsuranceClaim")
            OleDBConn1.Close()
            MsgBox("Update Successful")
        Catch ex As Exception
            MsgBox("Update Failed")
        End Try
    End Sub

Open in new window

0
Comment
Question by:--TripWire--
  • 3
  • 2
5 Comments
 
LVL 7

Assisted Solution

by:Alex_W
Alex_W earned 200 total points
Comment Utility
Consider My.Settings to save settings between runtime sessions.  You'll find it in the project properties under the Settings page.

To save you just need something like:

My.Settings.CommandButton = txtCommand.Text

To load you you use the forms load event with:

txtCommand.Text = My.Settings.CommandButton.
0
 

Author Comment

by:--TripWire--
Comment Utility
Thanks for the reply - but I think you misunderstood my question.
My form has a number of different controls on it, mostly textboxes.  And I want the information that the user types in to be saved into the database when the user clicks the Save command button on the form.
0
 
LVL 7

Accepted Solution

by:
Alex_W earned 200 total points
Comment Utility
Ah my mistake, to update changes in your binding source controls you'll want to use the data adapters update method.  I see your using an Access DB, here is some specifics:

http://msdn.microsoft.com/en-us/library/ms233819(v=VS.80).aspx
0
 

Author Comment

by:--TripWire--
Comment Utility
hmm...Well, I already have the adapter.update method in my code above
0
 

Author Closing Comment

by:--TripWire--
Comment Utility
It was helpful, but I wasn't able to find a full solution this way.  But thank you.
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

A while ago, I was working on a Windows Forms application and I needed a special label control with reflection (glass) effect to show some titles in a stylish way. I've always enjoyed working with graphics, but it's never too clever to re-invent …
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now