首页 > 解决方案 > Try-catch 语句 vb.net 在 Combo Box 上使用什么数据类型?

问题描述

看看下面的代码。此代码适用于除组合框外的所有文本框。我想这是因为数据类型。有没有办法解决它。请帮帮我。谢谢你!

    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

标签: vb.netcomboboxtry-catch

解决方案


注释和解释一致。

    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

推荐阅读