Making an txtbox.text blank freezes program

peterc

Member
Joined
Jan 5, 2016
Messages
16
Programming Experience
1-3
Hi all, again...

I have an interesting one (well I think it is interesting).

I have a form with a binding source on it.

If I change one of the bound fields which is an integer - to have a value of blank, through the use of a textbox then the program stops working.

If I change a text field it works fine.

The sql database definition for this field is Integer, and allows NULL.

vbproblem.JPG

The fields in question are vehicle year, vehicle tare, vehicle gvm, vehicle gcm, vehicle mtm, vehicle, axles, and vehicle wheelbase.

If I change the value of these fields to be blank, and click on Save, absolutely nothing happens, and I have to stop the program via Visual Studio....My File--> Exit button does not work either.

Below is the code for doing the update

VB.NET:
Private Sub cmdVehSave_Click(sender As Object, e As EventArgs) Handles cmdVehSave.Click
        'Variables for handling string to integer values in the database

        Dim intVehJobNo As Integer?
        Dim intVehYear As Integer?
        Dim intVehtare As Integer?
        Dim intvehgvm As Integer?
        Dim intvehgcm As Integer?
        Dim intvehmtm As Integer?
        Dim intvehaxles As Integer?
        Dim intvehwheelbase As Integer?

        intVehJobNo = txtjdJobNo.Text

        If txtVehYear.Text = "" Then intVehYear = Nothing Else intVehYear = txtVehYear.Text
        If txtVehTare.Text = "" Then intVehtare = Nothing Else intVehtare = txtVehTare.Text
        If txtVehGvm.Text = "" Then intvehgvm = Nothing Else intvehgvm = txtVehGvm.Text
        If txtVehGcm.Text = "" Then intvehgcm = Nothing Else intvehgcm = txtVehGcm.Text
        If txtVehMtm.Text = "" Then intvehmtm = Nothing Else intvehmtm = txtVehMtm.Text
        If txtVehAxles.Text = "" Then intvehaxles = Nothing Else intvehaxles = txtVehAxles.Text
        If txtVehWheelbase.Text = "" Then intvehwheelbase = Nothing Else intvehwheelbase = txtVehWheelbase.Text

        Dim strVehUpdate As String = "UPDATE VEHICLE " &
               "SET veh_job_no = " & intVehJobNo & ", Vehicle_Year = " & intVehYear & ", Vehicle_Make = '" & txtVehMake.Text & "', Vehicle_Model = '" & txtVehModel.Text & "', Vehicle_Rego = '" & txtVehRego.Text & "', Vehicle_vin = '" & txtVehVin.Text & "', " &
               " vehicle_chassis = '" & txtVehChassis.Text & "', vehicle_type = '" & txtVehType.Text & "', " &
               " vehicle_tare = " & intVehtare & ", vehicle_gvm = " & intvehgvm & ", vehicle_gcm = " & intvehgcm & ", vehicle_mtm = " & intvehmtm & ", vehicle_axles = " & intvehaxles & ", " &
               " vehicle_wheelbase = " & intvehwheelbase & ", vehicle_bodytype = '" & txtVehBodyType.Text & "', vehicle_condition = '" & txtVehCondition.Text & "', vehicle_class = '" & txtVehClass.Text & "', vehicle_notes = '" & txtVehNotes.Text & "' " &
               " WHERE veh_job_no = '" & intVehJobNo & "'"


        If sql.DataUpdate(strVehUpdate) = 0 Then
            MsgBox("Job not found")
        Else
            MsgBox("Job Updated")
        End If

    End Sub

Any ideas would be appreaciated
 
This is a perfect example of why you should NEVER use string concatenation to insert values into SQL code. You should ALWAYS use parameters. Before you make that call to DataUpdate, look at the value of strVehUpdate and the issue should be obvious. I have to wonder why you haven't already done that without prompting. If your code doesn't work when using particular data, the very first thing you should do is be sure that that data is what it's supposed to be.
 
Okay thanks for that.

However the fundamental issue is that if the Vehcile Year field is blank, absolutely nothing happens when I press the Save button on the form. The program basically stops responding.
 
The program basically stops responding.

Then you should debug and see where exactly that happens. Set a breakpoint at the top of the event handler (F9) and then step through the code line by line (F10). I'm guessing that, while the root cause is in the code you posted, the symptom shows itself in that DataUpdate method, so you'll probably want to step into that (F11).
 
And I have done this so many times I have lost count. When I click on Save absolutely nothing happens, if the field Vehicle Year is changed from a value to blank
 
And I have done this so many times I have lost count. When I click on Save absolutely nothing happens, if the field Vehicle Year is changed from a value to blank

So, are you saying that, if you place a breakpoint on the first line of that method and click the Button that the breakpoint never gets hit?
 
Yep......

If I change the value in vehicle year to blank and click save, the breakpoint does not get hit...the program actually stops responding, and I have to stop it via the VS Console

Yet there is nothing reporting in the debugging screen, that indicates there is an error
 
Are you handling any other events of that TextBox, e.g. Validating or Leave? If so then the issue is probably in one of those handlers. If not then something is corrupted somewhere and probably no changes you make to your code will help. You may have to blow away that form and recreate it.
 
Okay have sorted it, but not what I thought, more leaning towards where you were heading I think.

Seems to be a problem that has not been fixed .net - Data-bound TextBox: can't exit - Stack Overflow
So what I have done is changed the advanced binding data source update mode on the numeric textboxes to be Never instead of onValidation, and that has fixed the problem.

Have also taken on board you suggestion, about parametising my sql calls as well, which has now made it easier to set dbnull.value on the blank numeric values

Thanks
 
Back
Top