Click here to Skip to main content
15,898,134 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
hi. i have write this code to retrieve data from table where name like gridview value but the loop run only one time not till the all record found.
VB
<pre> Dim cmd As New OleDbCommand
        Dim dr As OleDbDataReader
        Dim i As Integer
        cmd.Connection = cn
        For i = 0 To grd.RowCount - 1
            cmd.CommandText = "SELECT status FROM exam_attendance WHERE rollno = " & Val(grd.Item(0, i).Value) & " AND subject = '" & cmbsubject.Text & "' AND ta_date = #" & CType(mskexamdate.Text, Date).ToString("MM/dd/yyyy") & "# AND class = '" & cmbclass.Text & "' AND medium = '" & cmbmedium.Text & "'"
            dr = cmd.ExecuteReader
        If dr.HasRows Then
            While dr.Read
                If dr.Item("status") = "PRESENT" Then
                    grd.Item(4, i).Value = True
                Else
                    grd.Item(4, i).Value = False
                End If
            End While
            End If
        Next



What I have tried:

i've change the code but isn't working
Posted
Updated 12-Mar-18 1:40am

VB
cmd.CommandText = "SELECT status FROM exam_attendance WHERE rollno = " & Val(grd.Item(0, i).Value) & " AND subject = '" & cmbsubject.Text & "' AND ta_date = #" & CType(mskexamdate.Text, Date).ToString("MM/dd/yyyy") & "# AND class = '" & cmbclass.Text & "' AND medium = '" & cmbmedium.Text & "'"


Not a solution to your question, but another problem you have.
Never build an SQL query by concatenating strings. Sooner or later, you will do it with user inputs, and this opens door to a vulnerability named "SQL injection", it is dangerous for your database and error prone.
A single quote in a name and your program crash. If a user input a name like "Brian O'Conner" can crash your app, it is an SQL injection vulnerability, and the crash is the least of the problems, a malicious user input and it is promoted to SQL commands with all credentials.
SQL injection - Wikipedia[^]
SQL Injection[^]
SQL Injection Attacks by Example[^]
PHP: SQL Injection - Manual[^]
SQL Injection Prevention Cheat Sheet - OWASP[^]
 
Share this answer
 
Comments
Maciej Los 12-Mar-18 7:14am    
Good advice!
Patrice T 12-Mar-18 7:32am    
Thank you
In addition to solution #2 by ppolymorphe[^]...

Quote:
i have write this code to retrieve data from table where name like gridview value but the loop run only one time not till the all record found.


You're on the wrong track! Imagine, your application is used by 1000 users. All of them start the application at the same time. What's happen with your database? A database is under DDoS attack[^]!

You have to create proper sql statement at once, which will return a proper data set. For example:
SQL
SELECT t1.*, CBool(t2.[status]="PRESENT") As IsPresent
FROM Table1 As t1 INNER JOIN Table2 as t2 ON t1.PrimaryKey = t2.ForeignKey

Then, you have to bind that data with DataGridView.

Above query statement is totally legal for MS Access database engine.

For further details, please see: Visual Representation of SQL Joins[^]
 
Share this answer
 

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