简体   繁体   中英

What data type to use on Combo Box in Try-catch statement vb.net?

Have a look at the code below. This code works on all the textboxes except on the combobox. I guess it is because of the data type. Is there a way to fix it. Please do help me. Thank You!

    Dim int As Integer
    Dim str As String
    Try
        int = CInt(txtsid.Text) & CInt(txtsph.Text)
        str = CStr(txtsfn.Text) & CStr(txtsln.Text) & CStr(txtint.Text) & CStr(txtsem.Text) & CStr(cbogen.Text)
    Catch ex As Exception
        MessageBox.Show("Please Type Informations Properly")
        Return
    End Try
    Dim result As Integer = MessageBox.Show("Are you sure you want to proceed?", "Proceed", MessageBoxButtons.YesNo, MessageBoxIcon.Question)
    If result = DialogResult.Yes Then
        UserHomepage.Show()
        Me.Hide()
        cmdInsert.CommandText = "Insert into student Values(" + txtsid.Text + ",'" + txtint.Text + "','" + txtsfn.Text + "','" + txtsln.Text + "', '" + cbogen.Text + "', " + txtsph.Text + ", '" + txtsem.Text + "');"
        cmdInsert.CommandType = CommandType.Text
        cmdInsert.Connection = cnnOLEDB
        cmdInsert.ExecuteNonQuery()
    ElseIf result = DialogResult.No Then
        Me.Show()
        UserHomepage.Hide()
    End If

Comment and explanations in line.

    Private Sub UpdateDatabase()
        'This entire are of code down to the End Try does nothing
        'Any .Text property is already a String and does not need CStr
        'In the int = line you have 2 Strings that you convert to Integers, Then they must
        'be changed back to Strings in order to concatenate them, Then the new string is again changed to an
        'integer!! Argh!
        'Dim int As Integer
        'Dim str As String
        'Try
        '    int = CInt(txtsid.Text) & CInt(txtsph.Text)
        '    str = CStr(txtsfn.Text) & CStr(txtsln.Text) & CStr(txtint.Text) & CStr(txtsem.Text) & CStr(cbogen.Text)
        'Catch ex As Exception
        '    MessageBox.Show("Please Type Informations Properly")
        '    Return
        'End Try

        'Changed Integer to DialogResult
        Dim result As DialogResult = MessageBox.Show("Are you sure you want to proceed?", "Proceed", MessageBoxButtons.YesNo, MessageBoxIcon.Question)
        If result = DialogResult.Yes Then
            UserHomepage.Show()
            Me.Hide()
            Try 'This is the place for Try...End Try. Networks and database connections can have
                'all sorts of unexpected errors.
                'The Using blocks ensure that your objects are closed and disposed even if there is an error.
                'Keep your connections local
                Using cnnOLEDB As New OleDbConnection("Your connection string")
                    'Pass your command text and the connection to the constructor of the command
                    Using cmdInsert As New OleDbCommand("Insert into student Values(?,?,?,?,?, ?,?);", cnnOLEDB)
                        cmdInsert.CommandType = CommandType.Text
                        'USE PARAMETERS to avoid SQL injection
                        'If this first parameter is an autonumber field, it should be removed
                        'from the Insert statement. Also remove a "?" You may have to list the
                        'fields in the first part of the Insert to match the question marks.
                        cmdInsert.Parameters.Add("@sid", OleDbType.Integer).Value = txtsid.Text
                        cmdInsert.Parameters.Add("@int", OleDbType.VarChar).Value = txtint.Text
                        cmdInsert.Parameters.Add("@sfn", OleDbType.VarChar).Value = txtsfn.Text
                        cmdInsert.Parameters.Add("@sln", OleDbType.VarChar).Value = txtsln.Text
                        cmdInsert.Parameters.Add("@gen", OleDbType.VarChar).Value = cbogen.Text
                        cmdInsert.Parameters.Add("@sph", OleDbType.Integer).Value = txtsph.Text
                        cmdInsert.Parameters.Add("@sem", OleDbType.VarChar).Value = txtsem.Text
                        cnnOLEDB.Open()
                        cmdInsert.ExecuteNonQuery()
                    End Using
                End Using
            Catch ex As Exception
                MessageBox.Show(ex.Message)
            End Try
            'The following ElseIf is useless
            'Me is already visible and UserHomepage is not
            'ElseIf result = DialogResult.No Then
            '    Me.Show()
            '    UserHomepage.Hide()
        End If
    End Sub
    'Do your validation here
    Private Sub TextBox1_Validating(sender As Object, e As System.ComponentModel.CancelEventArgs) Handles TextBox1.Validating
        'For a string field
        If TextBox1.Text = "" Then
            MessageBox.Show("Required field")
            e.Cancel = True
            TextBox1.Select(0, TextBox1.Text.Length)
        End If
        'Or
        'For a number field
        Dim myInt As Integer
        If Not Int32.TryParse(TextBox1.Text, myInt) Then
            MessageBox.Show("Requires number")
            e.Cancel = True
            TextBox1.Select(0, TextBox1.Text.Length)
        End If
    End Sub

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM