Click here to Skip to main content
15,067,335 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
A databound Textbox will update the Datatable, but does not mark the row as updated.
when I call .update. There is never a call made to SQL to update.

Using other bound controls with this exact same code works.
It is only the textbox having this problem.

Does anyone have any ideas?



VB
Imports System
Imports System.Data
Imports System.Data.SqlClient

Public Class Customer
    Dim connString As String = "Password=TestPassword;User ID=TestUserName;" & "Initial Catalog=DB2;" & "Data Source=LocalSQLServer ; Asynchronous Processing=true"
    Dim GlobalSQLConnection As New SqlConnection
    Dim GlobalDataSet As DataSet = New DataSet
    Dim da_Customers As New SqlDataAdapter
    Dim bl As New SqlCommandBuilder

    Public Sub loadCustomer(Customerid)
        GlobalSQLConnection = New SqlConnection(connString)
        GlobalSQLConnection.Open()

        'Build Customer DataTables
        GlobalDataSet.Tables.Add("Customers")

        da_Customers = New SqlDataAdapter("Select * from customers where id=118", GlobalSQLConnection)

        bl = New SqlCommandBuilder(da_Customers)
        da_Customers.FillSchema(GlobalDataSet, SchemaType.Source, "Customers")
        da_Customers.Fill(GlobalDataSet.Tables("Customers"))

        'Textbox will not work, Datagrid works fine by replacing this one line of code.
        TextBox1.DataBindings.Add(New Binding("text", GlobalDataSet.Tables("Customers"), "CustomerName"))
    End Sub

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click

        'Prove the Dataset Did update
        For Each row In GlobalDataSet.Tables("Customers").Rows
            Debug.Print(row("CustomerName"))
        Next
        
        da_Customers.Update(GlobalDataSet, "Customers") 'There are no Rows in the DS marked for Update, So no call is ever made to the SQL server
        Debug.Print("all done go check")
    End Sub

    Private Sub Customer_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        loadCustomer(18)
    End Sub

End Class
Posted

1 solution

Solution was found Here (Thank you James!):http://www.pcreview.co.uk/threads/newbie-bound-textbox-not-updating-dataset.1398996/[^]


The Textbox Does not mark the Datatable as updated until you move to the next record.
since I am only only working with one record, I had to create a BindingManager.
Using the .EndCurrentEdit() from the Binding Manager allowed the record to be marked for update.



VB
Imports System
Imports System.Data
Imports System.Data.SqlClient
 
Public Class Customer
    Dim connString As String = "Password=TestPassword;User ID=TestUserName;" & "Initial Catalog=DB2;" & "Data Source=LocalSQLServer ; Asynchronous Processing=true"
    Dim GlobalSQLConnection As New SqlConnection
    Dim GlobalDataSet As DataSet = New DataSet
    Dim da_Customers As New SqlDataAdapter
    Dim bl As New SqlCommandBuilder
 
    Public Sub loadCustomer(Customerid)
        GlobalSQLConnection = New SqlConnection(connString)
        GlobalSQLConnection.Open()
 
        'Build Customer DataTables
        GlobalDataSet.Tables.Add("Customers")
 
        da_Customers = New SqlDataAdapter("Select * from customers where id=118", GlobalSQLConnection)
 

        bl = New SqlCommandBuilder(da_Customers)
        da_Customers.FillSchema(GlobalDataSet, SchemaType.Source, "Customers")
        da_Customers.Fill(GlobalDataSet.Tables("Customers"))
 
        'Textbox will not work, Datagrid works fine by replacing this one line of code.
        TextBox1.DataBindings.Add(New Binding("text", GlobalDataSet.Tables("Customers"), "CustomerName"))
    End Sub
 
    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
 
        'Create and Use Binding Manager HERE!!
        Dim bm as BindingManagerBase = BindingContext(GlobalDataSet, "Customers")
        bm.EndCurrentEdit()
        
        da_Customers.Update(GlobalDataSet, "Customers") 'There are no Rows in the DS marked for Update, So no call is ever made to the SQL server
        Debug.Print("all done go check")
    End Sub
 
    Private Sub Customer_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        loadCustomer(18)
    End Sub
 
End Class
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900