I wrote a simple code for validation with VB.NET. This code is storing EMPTY data in the database table. How do I avoid this, and what code do I write after the else if statements? i.e after the Phone no validation.
This is the code below:
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
SqlParameterwith your command