Click here to Skip to main content
16,018,442 members
Please Sign up or sign in to vote.
3.00/5 (2 votes)
See more:
I have 6 text boxes.
on clicking save button it should check the following
if the value entered in textbox 4 is beginning with 400 then save.
else if value in textbox 4 is not starting with 400 then check with database whether the value exists in database.
if exists save entry
else check for textbox 5. if value in textbox 5 is 29 or 31 then save
else clear field or dont save..
please help me.
i am new to vb.net...
please
i need it very immediately....


I have used goto statement here. Is there any other way to do this.. can u please help me ... or is this fine to proceed... find the below code

Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click
        Try
            If (IsNumeric(txtAccNo.Text) = False Or txtAccNo.Text.Trim.Length < 12) Then
                ShowMessage("Enter Account Number")
 'some validation done here for first three textboxes..
If Mid(ctrl.txtBank_Code.Text, 1, 3) = "400" Then
                    GoTo repeat
                End If
                
                If Mid(ctrl.txtBank_Code.Text, 1, 3) <> "400" Then
                    Dim Conn1 As New SqlConnection(CONNSTR)
                    Dim cmd1 As New SqlCommand("", Conn1)
                    cmd1.CommandText = "Select MICR_CODE from New_Speed1 where MICR_CODE='" & _
                                      ctrl.txtBank_Code.Text & "'"
                    Try
                        Conn1.Open()
                        Dim drReader As SqlDataReader = cmd1.ExecuteReader
                        Dim isthr As Boolean = False
                        If drReader.HasRows Then
                            isthr = True
                            'MsgBox("TEST")
                            Conn1.Close()
                            cmd1.Dispose()
                            Conn1.Dispose()
                            drReader.Read()
                            ctrl.txtBank_Code.Text = drReader.Item("MICR_CODE")
                            drReader.Close()
                            Conn1.Close()
                            cmd1.Dispose()
                            Conn1.Dispose()
                        
                        ElseIf isthr <> True And ((ctrl.txtTran_Code.Text = "29") Or (ctrl.txtTran_Code.Text = "31")) Then
                            GoTo repeat
                        ElseIf isthr <> True And (ctrl.txtTran_Code.Text <> "29" Or ctrl.txtTran_Code.Text <> "31") Then
                            MsgBox("OUT STATION !!!")
                            Exit Sub
                        End If
                    Catch ex As Exception
                        MsgBox("Could not Find Record", MsgBoxStyle.Critical)
                    End Try
                End If

the goto statement comes here. once i click save button it should validate as mentioned above and save in database

repeat:
Dim cmd As New SqlCommand("Insert_Scanned_Cheques_New01", conn)
               cmd.CommandType = CommandType.StoredProcedure
               cmd.Parameters.Add("Account_No", Data.SqlDbType.VarChar, 12).Value = _
                                  Me.txtAccNo.Text
Posted
Updated 22-Jun-11 18:47pm
v2
Comments
Prerak Patel 23-Jun-11 0:56am    
It can be avoided most of the time with restructuring function.

1 solution

Just to give you an idea:

I haven't seen a single goto for many ears. I saw the bare traces of goto until middle of 1980x, mostly in FORTRAN and almost never later. Even in FORTRAN it was considered obsolete; it's just because people used to work in earlier versions of the compiler.

I'm very surprised someone remembers this operator and use it. It's relatively hard to fix your code because the normal programmer's brain is not used to see any need in goto; the use if it is not even considered.

I would try to throw out the code and write it normally.

By the way, you misuse try-catch. After showing message box propagation of the exception is blocked. The exception should be caught in main UI thread cycle. The class Application of both Forms and WPF have the member to catch the exception on the very top of the cycle.

You also mix-up data manipulations and UI, this is very bad and not supportable. There are different architectural patterns helping to separate concerns: MVC, MVP, MVA and MVVM; you can find them in Wikipedia, please see the list in my past answer:
how to control Controlls of a user interface form through Functions (methods)[^].


—SA
 
Share this answer
 

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