My form uses Listbox and ComboBox and records are not saved in the database

Hi, I am new to VB and I need to write an application where users will pick values from ListBox and ComboBox to fill-in the records. I can pick some options and when I press the 'save' button, I get a successful message. I can move from record to record and see my changes were 'saved' but when I exit and preview data or restart the application, the data is back to the original.

I have set the Dataset property to 'Do not Copy' (I have tried 'Copy if newer' also) but that doesn't change a thing.

There are 3 tables: toto is the main table. toto_field1 contains value for a ComboBox and toto_field3 contains value for the ListBox.

Please help, this should be simple and it's driving me crazy!!!

========================================================

Here's the form's code and at the bottom, the code from the UpdateQuery used to update the table:

Imports System

Imports System.Data

Imports System.Data.SqlClient

Imports System.Media

Public Class Form1

Private Sub TotoBindingNavigatorSaveItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TotoBindingNavigatorSaveItem.Click

SystemSounds.Exclamation.Play()

'My.Computer.Audio.Play("c:\Windows\Media\chimes.wav")

MessageBox.Show(Me.ListBox1.SelectedIndices.Count.ToString() & " items ont ete choisis")

Dim i As Integer

i = Me.ListBox1.SelectedIndices.Count

If i < 1 Or i > 3 Then

'SystemSounds.Exclamation.Play()

My.Computer.Audio.Play("c:\Windows\Media\chord.wav")

MessageBox.Show("Vous devez selectionner au moins 1 item et pas plus de 3")

Exit Sub

End If

If i > 0 Then

Dim x(i) As Double

Dim ix As Integer

For ix = 0 To i - 1

x(ix) = Me.ListBox1.SelectedIndices(ix)

MessageBox.Show(Me.ListBox1.SelectedIndices(ix).ToString())

Next

End If

'Me.SignsDataSet.EnforceConstraints = True

Try

Me.Validate()

Me.TotoBindingSource.EndEdit()

' Me.SignsDataSet.toto

Me.TotoTableAdapter.UpdateQuery()

My.Computer.Audio.Play("c:\Windows\Media\tada.wav")

MsgBox("Update successful")

Catch ex As Exception

My.Computer.Audio.Play("c:\Windows\Media\chord.wav")

MessageBox.Show("The " & ex.Source & " caused the following error: " & ex.Message)

End Try

End Sub

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

Me.SignsDataSet.EnforceConstraints = False

'TODO: This line of code loads data into the 'SignsDataSet.toto' table. You can move, or remove it, as needed.

Me.TotoTableAdapter.Fill(Me.SignsDataSet.toto)

'TODO: This line of code loads data into the 'SignsDataSet.toto_field1' table. You can move, or remove it, as needed.

Me.Toto_field1TableAdapter.Fill(Me.SignsDataSet.toto_field1)

'TODO: This line of code loads data into the 'SignsDataSet.toto_field3' table. You can move, or remove it, as needed.

Me.Toto_field3TableAdapter.Fill(Me.SignsDataSet.toto_field3)

Me.SignsDataSet.EnforceConstraints = True

End Sub

End Class

===========================================================

UpdateQuery:

UPDATE toto
SET field2 = toto_field1.field1, field3 = toto_field3.field3, field4 = toto_field3.field3
FROM toto INNER JOIN
toto_field1 ON toto.field1 = toto_field1.field1 INNER JOIN
toto_field3 ON toto.field3 = toto_field3.field3



Answer this question

My form uses Listbox and ComboBox and records are not saved in the database

  • Amit G

    I would read the following

    http://forums.microsoft.com/MSDN/ShowPost.aspx PostID=253426&SiteID=1

    This sounds like a common problem which is that the database in the problem has a property set to Copy the database to Output Directory.

    This results in every time you restart the application you original database overwriting the one in the bin directory.

    You can change this setting to Do Not Copy and it will leave the same file in the output folder.

    Let us know if that helps.


  • Modernthinker

    Thanks Spotty,

    I read the link you mentionned but it points back to the dataset property. I have tried both "copy if newer" and "do not copy" with the same result.

    Now that I found the number of record updated = 0, I realize the problem is not that they are saved and lost when I restart the application.

    The record are not saved at all... (see my update statement in my 1st post)

    Any ideas, comments will be much appreciated!

    Thanks,

    Claude.


  • Stephen Lawson

    Also I dont see any parameters in this query.

    What didnt work with the designer generated query This should have been using dataset values to update a database. And you controls would have been bound to the dataset.


  • gdp2000

    I can't use the line you proposed but I did this:

    RecUpd = Me.TotoTableAdapter.UpdateQuery()

    MessageBox.Show(RecUpd & " record(s) updated")

    And I got the following message: 0 record(s) updated

    I don't get it... but at least it match the result...


  • Etienne Sammut


    I coded the query. The one provided by the designer would not work (it would give an error).

    I had to re-write the query to reflect the choices from the ListBox (toto_field3) and ComboBox (toto_field1).

    Claude.


  • shahrul

    Thanks Spotty!

    I removed everything and re-created the form. Using the the designer generated query, I realized that no update will work if you don't define a key on the table you are tying to update.

    Claude.


  • Zackelino

    Add the following line of code after "Me.TotoTableAdapter.UpdateQuery()"

    Me.TotoTableAdapter.Update(Me.SignsDataSet) 'Returns how many rows were updated



  • N. Feldman

    Did you code that Query or did the designer

    What database are you using.


  • fred369

    Are you trying to add new data or update existing data If you're adding new then you'll need to use an INSERT query, if you're updating then you'll need a where clause in your SQL query (otherwise how does it know what to update ).

  • My form uses Listbox and ComboBox and records are not saved in the database