Click here to Skip to main content
16,022,309 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
VB
Sub LoadProduct()
    If cboFilter.Text = String.Empty Then Return
    If txtSearch.Text = String.Empty Then Return
    Dim i As Integer = 0
    DataGridView2.Rows.Clear()
    cn.Open()
    cm = New MySqlCommand("SELECT * FROM tblProduct AS p INNER JOIN tblGeneric AS g ON p.ProductGeneric = GenericID INNER JOIN tblBrand AS b ON p.ProductBrand = BrandID INNER JOIN tblFromulation AS f ON p.ProductFromulation = FromulationID INNER JOIN tblClassification AS c ON p.ProductClassification = ClassificationID INNER JOIN tblType AS t ON p.ProductType = TypeID where " & cboFilter.Text & " like '" & txtSearch.Text & "%'", cn)
    dr = cm.ExecuteReader
    While dr.Read
        i += 1
        DataGridView2.Rows.Add(i, dr.Item("ProductID").ToString, dr.Item("GenericName").ToString & Space(2) & dr.Item("BrandName").ToString & Space(2) & dr.Item("FromulationName").ToString & Space(2) & dr.Item("ClassificationName").ToString & Space(2) & dr.Item("TypeName").ToString)
    End While
    dr.Close()
    cn.Close()
End Sub


What I have tried:

SQL
SELECT * FROM tblProduct AS p INNER JOIN tblGeneric AS g ON p.ProductGeneric = GenericID INNER JOIN tblBrand AS b ON p.ProductBrand = BrandID INNER JOIN tblFromulation AS f ON p.ProductFromulation = FromulationID INNER JOIN tblClassification AS c ON p.ProductClassification = ClassificationID INNER JOIN tblType AS t ON p.ProductType = TypeID
Posted
Updated 3-Jul-24 17:00pm
v5

As said in the first solution parameterisation is the main issue but it looks like there are other issues as well like error handling, disposing of objects and so on.

Even though you have used MariaDB you could go through Properly executing database operations[^]. The examples are for SQL Server but the idea with MariaDB is exactly the same.
 
Share this answer
 
Comments
Raedr 3-Jul-24 23:57pm    
Hello my friend, thank you for your reply
I did not face any actual problem in entering and displaying on DataCreditView, but the problem appears to me when I include the search engine. When I write in the file, the message appears and I need a solution.
To be honest, I say that I follow the explanation and do what it does, but it applies to the English language and I apply to the Arabic language. Do you think that the reason for the message appearing is from the Arabic language?
Wendelius 4-Jul-24 12:08pm    
Most likely the lack of parameterisation is causing your problem since the text is concatenated to the string. When you use bind variables, such problems will disappear.
There is so much going on in this question demonstrating how not to write a SQL query. The biggest, and most obvious issue is that your query is directly adding raw information directly from the interface. This leaves your code open to SQL Injection arracks[^].

Please parameterise your query. This will also take care of the problem with your query, because you aren't treating the search part of the text as a SQL string.
 
Share this answer
 

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

  Print Answers RSS
Top Experts
Last 24hrsThis month


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