Results 1 to 5 of 5

Thread: Bound controls but no update to db

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Feb 2003
    Location
    Argentina
    Posts
    1,950

    Bound controls but no update to db

    The code below is modified from an example in a book using bound controls in an untyped dataset. two things happen that I don't understand. The first is that the table in the db doesn't get updated. No error thrown, but no update.
    The second is is that when I click the Add button, the textboxes clear ok for a new entry. I enter a value in txtQuNum and tab to txtQuestion. But when I do that, the previous value that was just cleared reappears.
    The textboxes appear to be bound ok. What's up? Thanks.
    VB Code:
    1. Private Sub CreateADOObjects()
    2.         cn.ConnectionString = mstrFullPath
    3.         Try
    4.             Dim cmdQuestion As New OleDbCommand()
    5.             cmdQuestion.Connection = cn
    6.             Dim strQuestionSelect As String
    7.             strQuestionSelect = "SELECT * FROM QS"
    8.             cmdQuestion.CommandText = strQuestionSelect
    9.             daQuestion.SelectCommand = cmdQuestion
    10.  
    11.             Dim cmdModule As New OleDbCommand()
    12.             cmdModule.Connection = cn
    13.             Dim strModuleSelect As String
    14.             strModuleSelect = "SELECT * FROM [Mod List]"
    15.             cmdModule.CommandText = strModuleSelect
    16.             daModule.SelectCommand = cmdModule
    17.  
    18.             Dim cbQuestion As New OleDbCommandBuilder()
    19.             cbQuestion.DataAdapter = daQuestion
    20.             Dim cbModule As New OleDbCommandBuilder()
    21.             cbModule.DataAdapter = daModule
    22.         Catch ex As Exception
    23.             MessageBox.Show(ex.Message, "Creating Objects")
    24.         End Try
    25.     End Sub
    26.  
    27.     Private Sub FillTables()
    28.         Try
    29.             cn.Open()
    30.             dsQuestion.Clear()
    31.             daQuestion.Fill(dsQuestion, "QS")
    32.             dsModule.Clear()
    33.             daModule.Fill(dsModule, "[Mod List]")
    34.         Catch ex As Exception
    35.             MessageBox.Show(ex.Message & Environment.NewLine _
    36.                & "Ending Progranm", "Error Retrieving Data", _
    37.               MessageBoxButtons.OK, MessageBoxIcon.Error)
    38.             Application.Exit()
    39.         Finally
    40.             If cn.State = ConnectionState.Open Then
    41.                 cn.Close()
    42.             End If
    43.         End Try
    44.     End Sub
    45.  
    46.     Private Sub BindControls()
    47.         Try
    48.             txtQuestion.DataBindings.Add("Text", dsQuestion.Tables _
    49.               ("QS"), "QText")
    50.             txtQuNum.DataBindings.Add("Text", dsQuestion.Tables _
    51.               ("QS"), "Q#")
    52.         Catch ex As Exception
    53.             MessageBox.Show(ex.Message, "Binding Controls")
    54.         End Try
    55.     End Sub
    56.  
    57.     Private Sub GetData()
    58.         Try
    59.             CreateADOObjects()
    60.             FillTables()
    61.             bmbQuestion = Me.BindingContext(dsQuestion, "QS")
    62.             bmbModule = Me.BindingContext(dsModule, "[Mod List]")
    63.             BindControls()
    64.         Catch ex As Exception
    65.             MessageBox.Show(ex.Message, "Error Getting Data")
    66.         End Try
    67.     End Sub
    68.  
    69.     Private Sub btnAdd_Click(ByVal sender As System.Object,  _
    70.         ByVal e As System.EventArgs) Handles btnAdd.Click
    71.         Try
    72.             bmbQuestion.AddNew()
    73.             txtQuNum.Clear()
    74.             txtQuestion.Clear()
    75.             txtQuNum.Focus()
    76.         Catch ex As Exception
    77.             MessageBox.Show(ex.Message, "Error Adding Record")
    78.         End Try
    79.     End Sub
    80.  
    81.     Private Sub UpdateDB()
    82.         Try
    83.             daQuestion.Update(dsQuestion.Tables("QS"))
    84.             bmbQuestion.EndCurrentEdit()
    85.             dsQuestion.Clear()
    86.             daQuestion.Fill(dsQuestion, "QS")
    87.         Catch ex As Exception
    88.             MessageBox.Show(ex.Message, "Error Updating Database")
    89.         End Try
    90.     End Sub
    91.  
    92.     Private Sub btnSave_Click(ByVal sender As System.Object, _
    93.         ByVal e As System.EventArgs) Handles btnSave.Click
    94.         UpdateDB()
    95.     End Sub

  2. #2
    Hyperactive Member
    Join Date
    Sep 2000
    Location
    Tennessee
    Posts
    279
    I am having nearly the same thing happen. I have bound text boxes on two tabs of a tab control. When I click a check box on one tab it should populate the text boxes on both tabs with pre-determined values. It does this for the text box on the tab that currently has focus, however, when I switch to the other tab it still contains the old value. Also, when I switch back to the other tab it has also reverted back to the old value. Did you (or anyone) have a solution for this?
    A cynic knows the price of everything but the value of nothing.

  3. #3

    Thread Starter
    Frenzied Member
    Join Date
    Feb 2003
    Location
    Argentina
    Posts
    1,950
    My problem was the Access field names. I had to set the QuotePrefix and QuoteSuffix properties to "[" and "]" so that Access didn't have a problem updating. Answer here if I got the right link.

  4. #4
    Hyperactive Member
    Join Date
    Sep 2000
    Location
    Tennessee
    Posts
    279
    I found the answer to my problem. What I needed to do was add:

    Me.BindingContext(MyDataset, "MyTable").EndCurrentEdit()

    after I inserted the value into the textbox.
    A cynic knows the price of everything but the value of nothing.

  5. #5
    Lively Member
    Join Date
    Jun 2004
    Location
    Philippines
    Posts
    125
    hey dman! you solved my problem too!

    Thanks a lot! been trying to solve that for weeks now...haha!

    Take care!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width