Quantcast
Channel: VBForums - Visual Basic .NET
Viewing all articles
Browse latest Browse all 27518

VS 2010 Database sometimes saving data

$
0
0
Hi,

I use this code to save data from a win form to a database

vb.net Code:
  1. Public Class frmAddCust
  2.     Private alibraryDS As New LibraryDataSet
  3.     Private aCustomersTA As LibraryDataSetTableAdapters.CustomersTableAdapter
  4.     Private customersBS As New BindingSource
  5.  
  6.     Private Sub frmAddCust_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
  7.         Try
  8.             'Set up the dataset
  9.             aCustomersTA = New LibraryDataSetTableAdapters.CustomersTableAdapter
  10.             aCustomersTA.Fill(alibraryDS.Customers)
  11.  
  12.             'Set up binding source
  13.             customersBS.DataSource = alibraryDS
  14.             customersBS.DataMember = "Customers"
  15.  
  16.             'Bind the controls
  17.             txtID.DataBindings.Add("text", customersBS, "CustomerID")
  18.             txtFName.DataBindings.Add("text", customersBS, "FirstName")
  19.             txtLName.DataBindings.Add("text", customersBS, "LastName")
  20.  
  21.             tpDate.DataBindings.Add("text", customersBS, "DateJoined")
  22.             label1.Text = "CustomerID"
  23.         Catch ex As Exception
  24.             MessageBox.Show(ex.Message)
  25.         End Try
  26.     End Sub
  27.  
  28.     Private Sub cmdRegister_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdRegister.Click
  29.         If cmdRegister.Text = "Register" Then
  30.             customersBS.EndEdit()
  31.             customersBS.AddNew()
  32.             txtID.Focus()
  33.             cmdRegister.Text = "Save"
  34.         Else
  35.             Try
  36.                 'Save Button clicked
  37.                 customersBS.EndEdit()
  38.                 aCustomersTA.Update(alibraryDS.Customers)
  39.                 cmdRegister.Text = "Register"
  40.                 MessageBox.Show("Record Saved", "Save", MessageBoxButtons.OK, MessageBoxIcon.Information)
  41.             Catch ex As Exception
  42.                 'Catch deplicate record and constraint violations
  43.                 MessageBox.Show(ex.Message)
  44.             End Try
  45.         End If
  46.     End Sub
  47. End Class

Edit:

The connection string.

Code:

    <connectionStrings>
        <add name="WindowsApplication2.My.MySettings.LibraryConnectionString"
            connectionString="Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\Library.mdf;Integrated Security=True;User Instance=True"
            providerName="System.Data.SqlClient" />
    </connectionStrings>

However, while it works without fault in the AddCust form in the AddProd the same code saving the data half the time. Also, Sometimes if I reload the application after using form AddProd to add data to the database (verifying by checking the data in the Server Explorer) the data disappears. What could cause this?

Thanks,


Nightwalker

Viewing all articles
Browse latest Browse all 27518

Latest Images

Trending Articles



Latest Images

<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>