Retrieve the result of a select in a datagridview
JeanMarc
-
Anonymous user -
Anonymous user -
Hello.
I am working on a car rental management project.
I created a form to search for information on existing clients in my database (Access).
I wrote an SQL query on the TextChanged event of a textBox that returns, in a DataGridView, the information about clients whose last names start with the letter I specified in the textBox, but the query does not execute.
Here is my code:
Private Sub txtRecherche_TextChanged(sender As Object, e As EventArgs) Handles txtRecherche.TextChanged
dtgResult.Rows.Clear()
infocon.mareq = "Select MatCli, NomCli, PrenomCli from CLIENT where NomCli LIKE '" & Trim(txtRecherche.Text) & "*'"
Try
infocon.proc_connexion()
If infocon.rdr.Read Then
dtgResult.Rows.Add(infocon.rdr(0), infocon.rdr(1), infocon.rdr(2))
While infocon.rdr.Read
dtgResult.Rows.Add(infocon.rdr(0), infocon.rdr(1), infocon.rdr(2))
End While
End If
Catch ex As Exception
End Try
End Sub
I am working on a car rental management project.
I created a form to search for information on existing clients in my database (Access).
I wrote an SQL query on the TextChanged event of a textBox that returns, in a DataGridView, the information about clients whose last names start with the letter I specified in the textBox, but the query does not execute.
Here is my code:
Private Sub txtRecherche_TextChanged(sender As Object, e As EventArgs) Handles txtRecherche.TextChanged
dtgResult.Rows.Clear()
infocon.mareq = "Select MatCli, NomCli, PrenomCli from CLIENT where NomCli LIKE '" & Trim(txtRecherche.Text) & "*'"
Try
infocon.proc_connexion()
If infocon.rdr.Read Then
dtgResult.Rows.Add(infocon.rdr(0), infocon.rdr(1), infocon.rdr(2))
While infocon.rdr.Read
dtgResult.Rows.Add(infocon.rdr(0), infocon.rdr(1), infocon.rdr(2))
End While
End If
Catch ex As Exception
End Try
End Sub
Related links:
- [VB.NET] Select entire line ListView
- Retrieve the result of a SELECT (VB.net)
- The provider 'Microsoft.ACE.OLEDB.12.0' is not registered.
- Numbering RowHeader lines of a DataGridView
- Éteindre le PC avec VB.NET peut être effectué en utilisant la commande suivante : ```vb Process.Start("shutdown", "/s /t 0") ```
- [Visual Basic] only one comma in a textBox
5 answers
-
Hello
To post readable code, please use the code tags, see this little tutorial https://codes-sources.commentcamarche.net/faq/11288-les-balises-de-code
Moreover, your title mentions a datagrid (WPF control) and the body of the message refers to datagridview (Winform control), these are not the same technologies nor the same controls.
Can you confirm which one you are using?
--
When I was little, the Dead Sea was only sick.
George Burns-
Hello.
Thank you for your response.
Actually, it’s a DataGridView that I’m using in my code and the code is in vb.net.
Here is the code again:Private Sub txtRecherche_TextChanged(sender As Object, e As EventArgs) Handles txtRecherche.TextChanged dtgResult.Rows.Clear() infocon.mareq = "Select MatCli, NomCli, PrenomCli from CLIENT where NomCli LIKE '" & Trim(txtRecherche.Text) & "*'" Try infocon.proc_connexion() If infocon.rdr.Read Then dtgResult.Rows.Add(infocon.rdr(0), infocon.rdr(1), infocon.rdr(2)) While infocon.rdr.Read dtgResult.Rows.Add(infocon.rdr(0), infocon.rdr(1), infocon.rdr(2)) End While End If Catch ex As Exception End Try End Sub
Thank you in advance for your answers.
-
-
Yes, the behind code is in vb.net, but the interface technology is winform.
In .net, you can have several other interface technologies- wpf
- asp
- uiApp
- etc….
What is the type of the infocon object?
Is the datagridview empty at this moment? If not, then you should start by clearing it
--
When I was little, the Dead Sea was only sick.
George Burns-
-
The infocon object is of type InfoConnection, which is a class I created. This class allows me to manage the connection to the database and the execution of queries.
Here is its code:Public Class InfoConnexion Public cnn As SqlConnection Private macon As New OleDbConnection 'Declares a variable macon of type oledb connection and instantiated(New) Public rdr As OleDbDataReader Private strcon As String Private cmd As OleDbCommand Public mareq As String Friend meq As String 'procedure allowing to establish the connection and execute sql commands Public Sub proc_connexion() strcon = connect 'allows connection to the database macon = New OleDbConnection(strcon) macon.Open() cmd = New OleDbCommand(mareq, macon) rdr = cmd.ExecuteReader() End Sub Public Sub pro_deconnexion() rdr.Close() macon.Close() End Sub Public Function la_table() As DataTable strcon = connect 'allows connection to the db macon = New OleDbConnection(strcon) macon.Open() cmd = New OleDbCommand(mareq, macon) ' Dim dt As New DataSet Dim ds As DataTable = New DataTable Dim dat As New OleDbDataAdapter(cmd) dat.Fill(ds) 'ds = dt.Tables(0) macon.Close() Return (ds) End Function End Class
-
Ok
You didn't answer the second question, which is whether the datagridview is empty?
Moreover, if the connection is already established, I think it could be a problem.
Have you executed it step by step to see what happens?
--
When I was little, the Dead Sea was just sick.
George Burns-
Yes, the DataGridView is empty. I emptied it with the first line of code.
dtgResult.Rows.clear()
I executed it step by step. And there were no errors during execution. However, when I reached the if condition:
If infocon.rdr.Read Then dtgResult.Rows.Add(infocon.rdr(0), infocon.rdr(1), infocon.rdr(2)) While infocon.rdr.Read dtgResult.Rows.Add(infocon.rdr(0), infocon.rdr(1), infocon.rdr(2)) End While End If
The code inside the structure does not execute.
This means that the condition is not verified. I thought it might be a problem with my query, so I changed the query and it worked correctly. This is where I am stuck.
-
-
I never use a datareader, so by chance, Read is a method but you didn't put the parentheses.
--
When I was little, the Dead Sea was just sick.
George Burns -
Hello everyone! Regarding the display of a Select in a datagridview, you can also view this link: https://www.youtube.com/watch?v=H4DmXqMOY8I&feature=youtu.be
-
Hello Jacob.
No, I won’t watch your video, here https://forums.commentcamarche.net/forum/affich-36859630-cours-sur-le-c#10 why
-