Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Bound Text Box to a Date field of SQL server table

Status
Not open for further replies.

8787

Programmer
Sep 9, 2005
4
US
Hello,

I have a simple bound textbox control that bound to "Date" data type of a SQL table. I use BindingManager EndCurrentEdit to update the changes I made to the date. It works fine if I change from one date to different date, but if I remove a date and replace with nothing, I can't save the change. It keeps saving the old value. Seems this only happen for "Date" data type since the textbox bounded to varchar data type is fine. I've been debugging this for couple of days and couldn't figure out what is wrong. This is staight forward binding data. Just don't know what I missed here. Any information on this will be very appreciated!

Thanks and regards,
Lisa
 
It's probably because an empty string cannot be casted into a Date. The database doesn't know that you wish to save a null in your database--it would be like trying to type a letter into a textbox when it needed to be a numeric datatype.

I would catch the .TextChanged() event for the textbox to check if the box is empty. If it is, either explicitly set the row's value to dbnull.value, or set it to a default date.
 
I explicitly set the value to dbnull. It works fine with only one date bound control. When I have multiple, which ever is the first in the order came out correctly. But rest of them still have the old value. I add watch to both textbox text. If I remove both dates, as soon as I set first dbnull value, the second textbox's text go back to original value. Seems somehow the data get refreshed. Any suggestion will be helpful.

Thanks in advance!

**************************
If Trim(Me.txtPermits.Text) = "" Then

Me.DsPE1.Tables("ProjData").Rows
(EngBindingManager.Position).Item("Permits") =
System.DBNull.Value

End If

IfTrim(Me.txtAROW.Text) = "" Then
Me.DsPE1.Tables("ProjData").Rows
(EngBindingManager.Position).Item("AROW") =
System.DBNull.Value
End If
******************************
 
Do you have separate handlers set up for the different textboxes? Perhaps they are all running through the same code.

I extended a textbox a while ago and found out that the events were firing for all controls on the form. Try something like this and see if it helps:
Code:
Sub MyTextBox1_Click(sender As Object, e As SystemEventArgs)
    If CType(Sender, TextBox).Name <> "MyTextBox1" Then Return
    'The rest of your code goes here
End Sub

Have a great day!

j2consulting@yahoo.com
 
I found out something weird during debugging. I am lost here of the .NET behavior. I add simple window form to VB.NET project and add 3 textbox controls. Add following code to LostFocus event:

Private Sub TextBox1_LostFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles TextBox1.LostFocus
MsgBox("box 1 lost focus")
End Sub

Private Sub TextBox2_LostFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles TextBox2.LostFocus
MsgBox("box 2 lost focus")
End Sub

Private Sub TextBox3_LostFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles TextBox3.LostFocus
MsgBox("box 3 lost focus")
End Sub

When I move focus from textbox1 to textbox2, textbox2 lostfocus event triggered first then textbox1. Same as when move focus from textbox2 to textbox3, textbox3 LostFocus triggered before textbox2. Is it just me or it is the behavior of VB.NET?

Help!
 
I think leave is better.

Christiaan Baes
Belgium

I just like this --> [Wiggle] [Wiggle]
 
I think what is happening with 8787's code within the textbox is an incomplete msgbox. I copied and pasted the above code into a project. It acted really strange, generating 2 mesage boxes when the focus was lost (PLUS butting the focus back on textbox1.

Adding the same code to a leave event allowed me to move from box to box. And forming a complete msgbox, with a OKOnly button and title cause the app to function correctly.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top