Click here to Skip to main content
15,891,136 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Dim noofhalls As Integer
       Dim n As Integer
       Dim m As Integer
       Dim k As Integer
       Dim h As Integer
       Dim count As Integer

       noofhalls = Val(TextBox1.Text) / 15 'value is total number of strength and the values is 15
       If noofhalls > 1 Then
           For n = 1 To noofhalls - 1
               TabControl2.TabPages.Add("Hall" & n + 1)
           Next
       End If

       For n = 0 To noofhalls - 1
           dg = New DataGridView 'run-time datagridview is created
           dg.Size = New System.Drawing.Size(500, 500)
           dg.RowCount = 15
           dg.ColumnCount = 4
           dg.Columns(0).Name = "Seat No." 'first column labels
           dg.Columns(1).Name = "Reg, No." 'second column labels
           dg.Columns(2).Name = "Seat No." 'third column labels
           dg.Columns(3).Name = "Reg, No." 'fourth column labels

           For k = 0 To 14
               dg.Rows(k).Cells(0).Value = k + 1 'defining the seat
           Next
           h = 0

           For k = 16 To 100
               dg.Rows(h).Cells(2).Value = k + 1
               h += 1
           Next

           TabControl2.TabPages(n).Controls.Add(dg)

       Next
       count = 0
       Dim x, y As Integer
       For Each Str1 In ListBox2.Items
           Dim sql1 As String = "select count(regd_no) from class_table where class ='" + Str1 + "'"
           Dim sql As String = "select regd_no from class_table where class ='" + Str1 + "'"
           cmd1 = New SqlCommand(sql, conn)
           cmd = New SqlCommand(sql1, conn)
           count = Convert.ToInt16(cmd.ExecuteScalar)
           dr = cmd1.ExecuteReader()
           x = 1
           y = 1
           If count <= 100 Then
               While dr.Read And x <= 100

                   If x <= 1 Then
                       dg.Rows(x - 1).Cells(1).Value = dr(0).ToString
                       x += 2
                   End If
                   If x > 15 Then
                       dg.Rows(y - 1).Cells(3).Value = dr(0).ToString
                       y += 2
                   End If

               End While


           End If
           dr.Close()
       Next

   End Sub


What I have tried:

This is a Exam Hall Arrangement Project and I already set only 15 Students in my code.then, I try to increase them to some 'n' Value and it Show the error and I am trying to rectify them but i cannot solve codes...can anyone help me with this..
Posted
Updated 28-Apr-20 6:29am

1 solution

For starters you have a multitude of problems in your SQL block
Line 1 is vulnerable to SQL Injection.
Line 2 is also vulnerable
Line 3 is not needed and increases memory usage
Line 5 has a syntax error- should be count = Convert.ToInt16(cmd.ExecuteScalar())
VB
Dim sql1 As String = "select count(regd_no) from class_table where class ='" + Str1 + "'"
Dim sql As String = "select regd_no from class_table where class ='" + Str1 + "'"
cmd1 = New SqlCommand(sql, conn)
cmd = New SqlCommand(sql1, conn)
count = Convert.ToInt16(cmd.ExecuteScalar)
dr = cmd1.ExecuteReader()
NEVER EVER build an SQL query by concatenating commands with variables

This is how I would write this; using Parameters to get rid of the vulnerability, fix the syntax error, and reuse the cmd object by simply changing the CommandText and keeping the existing parameter
VB
Dim sql1 As String = "SELECT count(regd_no) FROM class_table WHERE class = @Str1"
Dim sql As String = "SELECT regd_no FROM class_table WHERE class = @Str1"

' cmd1 = New SqlCommand(sql, conn)

cmd = New SqlCommand(sql1, conn)
cmd.Parameters.AddWithValue("@Str1", Str1)

count = Convert.ToInt16(cmd.ExecuteScalar())

cmd.CommandText = sql
dr = cmd.ExecuteReader()
 
Share this answer
 
v4
Comments
Maciej Los 28-Apr-20 13:31pm    
5ed!
Nithish685 29-Apr-20 1:54am    
yes ok I try and this part has no error then,"if k=0 to 14 "this line shows some error..I also working with this project ..
MadMyche 29-Apr-20 6:22am    
A little more detail on the error would be helpful
Nithish685 30-Apr-20 7:28am    
bro that project was Exam hall seating arrangement also You know about how exam hall can be allocated...
i done all but I can't get the result of allocating two or more class..
otherwise share your mail-id i send the codes

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