pumbaaca
asked on
Cannot Cause change on bound field
I have a number of textboxes on my form that are all bound to a bindingsource object. About half of the textboxes correctly cause a datarow to be "updated" and update the data in the database. The other half just won't. I cannot see what difference the fields are between themselves. Both are defined essentially the same (as varchar fields). The main difference is that the ones that don't work are date fields.
From the database, I get the date fields formatted as mm/dd/yyyy (using the Convert 101 formatting). They correctly display in the textbox. I try to change the year within the textbox...which causes a change event no problem. When I do a bindingsource endedit, it does not appear to save the change to the underlying datarow that the textbox is bound to. As a result, there is no changed datarow to be updated by the data adapter.
Below is a condensed version of my source. I am calling this form from another form, passing the Data adapter, dataset, and a variable (to look up the datarow in the dataset). The update of the employeename field works fine. The reportedTestDate does not. If I change both fields together, only the employeeName field successfully updates, reportedTestDate does not.
Your thoughts?
On form:
TextBox called 'txtTestID'
TextBox called 'txtEmployeeName'
TextBox called 'txtReportedTestDate'
Button called 'btnSave'
in Code-Behind:
Private ReqTestID As Integer
Private ReqDA As SqlClient.SqlDataAdapter
Private ReqDS As DataSet
Private bsTable As BindingSource
Public Sub New(ByRef DATests As SqlClient.SqlDataAdapter, _
ByRef DSTests As DataSet, ByVal RTestID As Integer)
MyBase.New()
InitializeComponent()
ReqDA = DATests
ReqDS = DSTests
ReqTestID = RTestID
End Sub
Private Sub frmTestDetails_Load(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles MyBase.Load
bsTable = New BindingSource
bsTable.DataSource = ReqDS.Tables(0)
bsTable.Position = bsTable.Find("TestID", ReqTestID)
txtTestID.DataBindings.Add ("text", bsTable, "TestID")
txtEmployeeName.DataBindin gs.Add("te xt", bsTable, "EmployeeName")
txtReportedTestDate.DataBi ndings.Add ("text", bsTable, "ReportedTestDate")
End Sub
Private Sub btnSave_Click(ByVal sender As Object, _
ByVal e As System.EventArgs) Handles btnSave.Click
bsTable.EndEdit()
Dim dtChanges As DataTable
dtChanges = ReqDS.Tables(0).GetChanges ()
if dtChanges.rows.count > 0 then
dim intReturn as integer = ReqDA.Update(dtChanges)
If intReturn > 0 Then
ReqDS.Merge(dtChanges)
ReqDS.AcceptChanges()
Me.Close()
Else
msgbox("DB update returned an error")
end if
else
msgbox("No Updates found to be sent to DB")
end if
End Sub
From the database, I get the date fields formatted as mm/dd/yyyy (using the Convert 101 formatting). They correctly display in the textbox. I try to change the year within the textbox...which causes a change event no problem. When I do a bindingsource endedit, it does not appear to save the change to the underlying datarow that the textbox is bound to. As a result, there is no changed datarow to be updated by the data adapter.
Below is a condensed version of my source. I am calling this form from another form, passing the Data adapter, dataset, and a variable (to look up the datarow in the dataset). The update of the employeename field works fine. The reportedTestDate does not. If I change both fields together, only the employeeName field successfully updates, reportedTestDate does not.
Your thoughts?
On form:
TextBox called 'txtTestID'
TextBox called 'txtEmployeeName'
TextBox called 'txtReportedTestDate'
Button called 'btnSave'
in Code-Behind:
Private ReqTestID As Integer
Private ReqDA As SqlClient.SqlDataAdapter
Private ReqDS As DataSet
Private bsTable As BindingSource
Public Sub New(ByRef DATests As SqlClient.SqlDataAdapter, _
ByRef DSTests As DataSet, ByVal RTestID As Integer)
MyBase.New()
InitializeComponent()
ReqDA = DATests
ReqDS = DSTests
ReqTestID = RTestID
End Sub
Private Sub frmTestDetails_Load(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles MyBase.Load
bsTable = New BindingSource
bsTable.DataSource = ReqDS.Tables(0)
bsTable.Position = bsTable.Find("TestID", ReqTestID)
txtTestID.DataBindings.Add
txtEmployeeName.DataBindin
txtReportedTestDate.DataBi
End Sub
Private Sub btnSave_Click(ByVal sender As Object, _
ByVal e As System.EventArgs) Handles btnSave.Click
bsTable.EndEdit()
Dim dtChanges As DataTable
dtChanges = ReqDS.Tables(0).GetChanges
if dtChanges.rows.count > 0 then
dim intReturn as integer = ReqDA.Update(dtChanges)
If intReturn > 0 Then
ReqDS.Merge(dtChanges)
ReqDS.AcceptChanges()
Me.Close()
Else
msgbox("DB update returned an error")
end if
else
msgbox("No Updates found to be sent to DB")
end if
End Sub
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
It got me thinking, however, to put the data in its original format (smalldatetime) from the database and use the datetimepicker object on my form. It performs what I needed and seems to be acceptable to the bindingsource object...it allows an update to the databse. It saved me a little time in not having to delve into understanding the iformat constructs just yet!