数据绑定文本框在更新DataTable后不会更新数据库 [英] Databound Textbox Wont' Update DB after Updating DataTable

查看:84
本文介绍了数据绑定文本框在更新DataTable后不会更新数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

数据绑定文本框将更新数据表,但不会将该行标记为已更新。

当我调用.update时。从来没有调用SQL进行更新。



使用其他绑定控件使用完全相同的代码。

只有文本框出现此问题。



有没有人有任何想法?







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?



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

推荐答案

在此找到解决方案(谢谢James!): http://www.pcreview.co.uk/threads/newbie -bound-textbox-not-updating-dataset.1398996 / [ ^ ]





文本框不标记数据表更新,直到你移动到下一条记录。

,因为我只使用一条记录,我必须创建一个BindingManager。

使用.EndCurrentEdit()来自Binding Manager允许的记录要标记更新。







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.



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


这篇关于数据绑定文本框在更新DataTable后不会更新数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆