Click here to Skip to main content
15,888,351 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
VB.NET
Public Sub CheckEmptyTextbox(ByVal groupbox As GroupBox)
       Dim txt As Control
       For Each txt In groupbox.Controls
       If TypeOf txt Is TextBox Then
           If txt.Text = "" Then
               MsgBox("Fill Details To Proceed",MsgBoxStyle.Exclamation)
               Exit For
           End If
           End If
       Next
   End Sub

----------------------------------------------------------------------------------
C#
CheckEmptyTextbox(GroupBox1)
        'Dim con As New SqlConnection("Data Source=ASHUTOSH-PC\SQLEXPRESS;Initial Catalog=Art Station Management System;Integrated Security=True")
        Dim com As SqlCommand
        Dim com1 As New SqlCommand
        Dim str As String
        Dim str1 As String
        Dim i = Convert.ToInt32(stuidcmb.Text)
        Dim a = Convert.ToInt32(stuagetxt.Text)
        Dim gender As String = String.Empty
        If stumalerd.Checked Then
            gender = "Male"
        ElseIf stufemalerd.Checked Then
            gender = "Female"
        End If
        ' Try
        con.Open()
        str = "Insert into StudentRegister values (@sstudentid,@sname,@smiddlename,@ssurname,@sdob,@ccoursename,@btbatchid,@sage,@sgender,@scontact,@saddress,@semailid,@sdatereg)"
        com = New SqlCommand(str, con)
        com.Parameters.AddWithValue("@sstudentid", i)
        com.Parameters.AddWithValue("@sname", stufnametxt.Text)
        com.Parameters.AddWithValue("@smiddlename", stumnametxt.Text)
        com.Parameters.AddWithValue("@ssurname", stulnametxt.Text)
        com.Parameters.AddWithValue("@sdob", DateTimePicker2.Value.Date)
        com.Parameters.AddWithValue("@ccoursename", coursenamecmb.Text)
        com.Parameters.AddWithValue("@btbatchid", batchidcmb.Text)
        com.Parameters.AddWithValue("@sage", a)
        com.Parameters.AddWithValue("@sgender", gender)
        com.Parameters.AddWithValue("@scontact", stucntcttxt.Text)
        com.Parameters.AddWithValue("@saddress", stuaddtxt.Text)
        com.Parameters.AddWithValue("@semailid", stuemailtxt.Text)
        com.Parameters.AddWithValue("@sdatereg", DateTimePicker1.Value.Date)
        com.CommandText = str
        com.ExecuteNonQuery()
        con.Close()

        con.Open()
        str1 = "Insert into AttendanceStudent values (@sstudentid,@sname,@btbatchid,@ccoursename,@atsdate,'ABSENT')"
        com1 = New SqlCommand(str1, con)
        com1.Parameters.AddWithValue("@sstudentid", i)
        com1.Parameters.AddWithValue("@sname", stufnametxt.Text)
        com1.Parameters.AddWithValue("@btbatchid", batchidcmb.Text)
        com1.Parameters.AddWithValue("@ccoursename", coursenamecmb.Text)
        com1.Parameters.AddWithValue("@atsdate", DateTimePicker1.Value.Date)
        com1.CommandText = str1
        com1.ExecuteNonQuery()
        con.Close()
        MsgBox("Record Inserted Successfully", MsgBoxStyle.Information)
        Display()
        clear()
        'Catch ex As Exception

        'End Try
Posted

1 solution

As far as I can see you check if the text boxes are empty but you don't exit from the calling method if validation fails.

In other words the validation should be something like
VB
Public Function CheckEmptyTextbox(ByVal groupbox As GroupBox) As Boolean
       Dim txt As Control
       For Each txt In groupbox.Controls
       If TypeOf txt Is TextBox Then
           If txt.Text = "" Then
               MsgBox("Fill Details To Proceed",MsgBoxStyle.Exclamation)
               CheckEmptyTextbox = False ' Set the return value to false
               Exit Function ' exit validation and return false
           End If
           End If
       Next
       CheckEmptyTextbox = True ' everything good, return true
   End Function

And when calling
VB
...
        If Not CheckEmptyTextbox(GroupBox1) Then
           Exit Sub  ' if validation failed, do not continue forward
        End If
        Dim con As New SqlConnection("Data Source=ASHUTOSH-PC\SQLEXPRESS;Initial Catalog=Art Station Management System;Integrated Security=True")
...
 
Share this answer
 
v2
Comments
Thomas Daniels 9-Jan-16 13:42pm    
+5!
Wendelius 9-Jan-16 15:07pm    
Thank you :)
Ashutosh Dutondkar 9-Jan-16 15:11pm    
Thank you so much! Now its not giving me an error! Thank you very much! +10! :);)
Wendelius 9-Jan-16 15:16pm    
You're most welcome :)
Ashutosh Dutondkar 9-Jan-16 15:33pm    
If Not CheckEmptyTextbox(GroupBox1) Then
Exit Sub
End If
'Dim con As New SqlConnection("Data Source=ASHUTOSH-PC\SQLEXPRESS;Initial Catalog=Art Station Management System;Integrated Security=True")
Dim com As SqlCommand
Dim com1 As New SqlCommand
Dim str As String
Dim str1 As String
Dim i = Convert.ToInt32(stuidcmb.Text)
Dim a = Convert.ToInt32(stuagetxt.Text)
Dim gender As String = String.Empty
If stumalerd.Checked Then
gender = "Male"
ElseIf stufemalerd.Checked Then
gender = "Female"
End If
' Try
con.Open()
str = "Insert into StudentRegister values (@sstudentid,@sname,@smiddlename,@ssurname,@sdob,@ccoursename,@btbatchid,@sage,@sgender,@scontact,@saddress,@semailid,@sdatereg)"
com = New SqlCommand(str, con)
com.Parameters.AddWithValue("@sstudentid", i)
com.Parameters.AddWithValue("@sname", stufnametxt.Text)
com.Parameters.AddWithValue("@smiddlename", stumnametxt.Text)
com.Parameters.AddWithValue("@ssurname", stulnametxt.Text)
com.Parameters.AddWithValue("@sdob", DateTimePicker2.Value.Date)
com.Parameters.AddWithValue("@ccoursename", coursenamecmb.Text)
com.Parameters.AddWithValue("@btbatchid", batchidcmb.Text)
com.Parameters.AddWithValue("@sage", a)
com.Parameters.AddWithValue("@sgender", gender)
com.Parameters.AddWithValue("@scontact", stucntcttxt.Text)
com.Parameters.AddWithValue("@saddress", stuaddtxt.Text)
com.Parameters.AddWithValue("@semailid", stuemailtxt.Text)
com.Parameters.AddWithValue("@sdatereg", DateTimePicker1.Value.Date)
com.CommandText = str
com.ExecuteNonQuery()
con.Close()
If stucntcttxt.Text.Length <> 10 Then
MsgBox("Contact Number Must Be 10 Characters Long", MsgBoxStyle.Information)
Exit Sub
End If

If EmailAddressCheck(stuemailtxt.Text) = True Then
MsgBox("Please enter your email address correctly", vbInformation)
stuemailtxt.Text = ""
Exit Sub
End If

con.Open()
str1 = "Insert into AttendanceStudent values (@sstudentid,@sname,@btbatchid,@ccoursename,@atsdate,'ABSENT')"
com1 = New SqlCommand(str1, con)
com1.Parameters.AddWithValue("@sstudentid", i)
com1.Parameters.AddWithValue("@sname", stufnametxt.Text)
com1.Parameters.AddWithValue("@btbatchid", batchidcmb.Text)
com1.Parameters.AddWithValue("@ccoursename", coursenamecmb.Text)
com1.Parameters.AddWithValue("@atsdate", DateTimePicker1.Value.Date)
com1.CommandText = str1
com1.ExecuteNonQuery()
con.Close()
MsgBox("Record Inserted Successfully", MsgBoxStyle.Information)
Display()
clear()
'Catch ex As Exception

'End Try
End Sub

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900