我寫了一個簡單的驗證VB.NET代碼。此代碼正在將EMPTY數據存儲在數據庫表中。我如何避免這種情況,以及在else if
陳述後寫什麼代碼?即在電話沒有驗證之後。在數據庫中存儲空數據的驗證碼
這是下面的代碼:
Protected Sub Button1_Click(sender As Object, e As System.EventArgs) Handles Button1.Click
If Len(Trim(txtName.Text)) = 0 Then
MsgBox("Enter Name", MsgBoxStyle.Critical, "Error")
txtName.Focus()
ElseIf Len(Trim(txtAge.Text)) = 0 Then
MsgBox("Enter Age", MsgBoxStyle.Critical, "Error")
txtAge.Focus()
ElseIf Len(Trim(txtPhone.Text)) = 0 Then
MsgBox("Enter Phone", MsgBoxStyle.Critical, "Error")
txtPhone.Focus()
Else
Dim blnFlag As Boolean = False
MsgBox("Enter the details", MsgBoxStyle.Critical, "Error")
End If
Try
Dim strCommand As String
strCommand = "Insert into [Validation] ([Name],[Age],[Phone]) VALUES"
strCommand = strCommand & "('" & Trim(txtName.Text) & "','" & Trim(txtAge.Text) & "','" & Trim(txtPhone.Text) & "')"
Dim StrConnection As String
StrConnection = ConfigurationManager.ConnectionStrings("ConnectionString").ToString
Dim cnValidation As New SqlClient.SqlConnection(StrConnection)
If (cnValidation.State = ConnectionState.Closed) Then
cnValidation.Open()
End If
Dim cmdEmployee As New SqlClient.SqlCommand(strCommand, cnValidation)
cmdEmployee.ExecuteNonQuery()
cnValidation.Close()
MsgBox("Save Successful", MsgBoxStyle.Information, "Success")
Catch ex As Exception
MsgBox("Save failed " & ex.Message, MsgBoxStyle.Critical, "Failed")
End Try
不確定爲什麼你需要最後一個Else,在你的命令中使用'SqlParameter'是一個好主意 – Fred