none
How to get error on empty TextBox (IF&Else already have conditions) RRS feed

  • Question

  • How I can get error if Serial Text box is blank. In my Code serial Cannot be repeated.

     cn.Open()
            Try
                cmd.CommandText = "Select COUNT(*) from Monitors WHERE Serial ='" & MSerialTxt.Text & "'"
                cmd.ExecuteNonQuery()
                If cmd.ExecuteScalar = 0 Then
                    cmd.CommandText = "insert into Monitors(CompanyName,Model,Serial,Size,Location,DOP,Price) values ('" & MCompanyTxt.Text & "', '" & MModelTxt.Text & "', '" & MSerialTxt.Text & "', '" & MSizeTxt.Text & "', '" & MLocationTxt.Text & "','" & MDOPDate.Text & "','" & (MPriceTxt.Text) & "')"
                    cmd.ExecuteNonQuery()
                    MsgBox("Sucessfully Entered")
                    MCompanyTxt.Text = ""
                    MModelTxt.Text = ""
                    MSerialTxt.Text = ""
                    MSizeTxt.Text = ""
                    MLocationTxt.Text = ""
                    MPriceTxt.Text = ""
                Else
                    MsgBox("Serial Number is Already entered")
                End If
            Catch ex As Exception
                MessageBox.Show("Please Provide Price in Numbers only")
            End Try
            cn.Close()
        End Sub

    Sunday, July 23, 2017 4:56 PM

All replies

  • Maybe add these lines before your code:

       If MSerialTxt.Text.Trim.Length = 0 Then

          MSerialTxt.Select()

          MsgBox("Please enter the Serial Number")

          Return

       End If

     

    By the way, I think that the first ExecuteNonQuery is not needed. And to increase the quality, you must use Parameterised Queries.

    Sunday, July 23, 2017 7:10 PM
  • Hi JimmyJawad,

    Yes, you could should to use parameterized queries in your sql code, like this.

    If MSerialTxt.Text = "" Then
                MSerialTxt.Select()
                MsgBox("Please enter the Serial Number")
                Return
            End If
    
            Dim str As String = "Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=D:\C# and VB Support\Example(VB)\Demo\Data3.mdf;Integrated Security=True"
            Dim cn As New SqlConnection(str)
            cn.Open()
            Try
                Dim sql As String = "Select COUNT(*) from Monitors WHERE Serial =@Serial"
                Dim cmd As New SqlCommand(sql, cn)
                cmd.Parameters.AddWithValue("@Serial", MSerialTxt.Text)
                If cmd.ExecuteScalar = 0 Then
                    Dim sql1 As String = "insert into Monitors(CompanyName,Model,Serial,Size,Location,DOP,Price) values (@MCompanyTxt,@MModelTxt,@MSerialTxt,@MSizeTxt,@MLocationTxt,@MDOPDate,@MPriceTxt)"
                    Dim cmd1 As New SqlCommand(sql1, cn)
                    cmd1.Parameters.AddWithValue("@MCompanyTxt", MCompanyTxt.Text)
                    cmd1.Parameters.AddWithValue("@MModelTxt", MModelTxt.Text)
                    cmd1.Parameters.AddWithValue("@MSerialTxt", MSerialTxt.Text)
                    cmd1.Parameters.AddWithValue("@MSizeTxt", MSizeTxt.Text)
                    cmd1.Parameters.AddWithValue("@MLocationTxt", MLocationTxt.Text)
                    cmd1.Parameters.AddWithValue("@MDOPDate", MDOPDate.Text)
                    cmd1.Parameters.AddWithValue(",@MPriceTxt", MPriceTxt.Text)
    
                    cmd1.ExecuteNonQuery()
                    MessageBox.Show("Sucessfully Entered")
                    MCompanyTxt.Text = ""
                    MModelTxt.Text = ""
                    MSerialTxt.Text = ""
                    MSizeTxt.Text = ""
                    MLocationTxt.Text = ""
                    MPriceTxt.Text = ""
                Else
                    MsgBox("Serial Number is Already entered")
                End If
    
                'cmd.CommandText = "Select COUNT(*) from Monitors WHERE Serial ='" & MSerialTxt.Text & "'"
                'If cmd.ExecuteScalar = 0 Then
    
                '    cmd.CommandText = "insert into Monitors(CompanyName,Model,Serial,Size,Location,DOP,Price) values ('" & MCompanyTxt.Text & "', '" & MModelTxt.Text & "', '" & MSerialTxt.Text & "', '" & MSizeTxt.Text & "', '" & MLocationTxt.Text & "','" & MDOPDate.Text & "','" & (MPriceTxt.Text) & "')"
                '    cmd.ExecuteNonQuery()
                '    MsgBox("Sucessfully Entered")
                '    MCompanyTxt.Text = ""
                '    MModelTxt.Text = ""
                '    MSerialTxt.Text = ""
                '    MSizeTxt.Text = ""
                '    MLocationTxt.Text = ""
                '    MPriceTxt.Text = ""
    
                'Else
    
                '    MsgBox("Serial Number is Already entered")
    
                'End If
            Catch ex As Exception
                MessageBox.Show("Please Provide Price in Numbers only")
            End Try
            cn.Close()

    It is veryclear and not easy to go wrong。

    Best Regards,

    Cherry


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Monday, July 24, 2017 2:46 AM
    Moderator