vb.net - 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
解决方案
注释和解释一致。
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
推荐阅读
- proof - 需要帮助开始模块化同余证明
- python - 使用 drop() 从熊猫数据框中删除星期六行的正确方法
- javascript - 使用 window.open 时如何从 postMessage 接收消息
- json - 我可以使用 TOML 变量将 TOML 表嵌套在其他 TOML 表中吗?
- typescript - Typescript:对类型化地图的动态分配
- node.js - npm init -y install 的 package.json 错误
- javascript - 如果属性为真,则从对象数组返回指定值的函数
- azure-ad-b2c - 登录重定向后 msal.js 2.0 tokenResponse null
- python - 如何操作列表中文本文件中的元素?
- javascript - 无法使用 ... 运算符或 Array.from() 或 slice(0),,Javascript/React 分隔处于状态的对象数组