Click here to Skip to main content
16,004,529 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
SQL SERVER
cn.Open()
cm = New SqlCommand("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 ProductQuantity > 0 and " & cboFilter.Text & " like '" & txtSearch.Text & "'", cn)
dr = cm.ExecuteReader

What I have tried:

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 ProductQuantity > 0 and " & cboFilter.Text & " like '" & txtSearch.Text & "'
Posted
Updated 15-Jul-24 19:17pm
v2
Comments
PIEBALDconsult 13-Jul-24 23:43pm    
Do not use string concatenation to form SQL statements!

You have two problems here: the first is that as PIEBALDconsult has said, you should never concatenate strings to build a SQL command. It leaves you wide open to accidental or deliberate SQL Injection attack which can destroy your entire database. Always use Parameterized queries instead.

When you concatenate strings, you cause problems because SQL receives commands like:
SQL
SELECT * FROM MyTable WHERE StreetAddress = 'Baker's Wood'
The quote the user added terminates the string as far as SQL is concerned and you get problems. But it could be worse. If I come along and type this instead: "x';DROP TABLE MyTable;--" Then SQL receives a very different command:
SQL
SELECT * FROM MyTable WHERE StreetAddress = 'x';DROP TABLE MyTable;--'
Which SQL sees as three separate commands:
SQL
SELECT * FROM MyTable WHERE StreetAddress = 'x';
A perfectly valid SELECT
SQL
DROP TABLE MyTable;
A perfectly valid "delete the table" command
SQL
--'
And everything else is a comment.
So it does: selects any matching rows, deletes the table from the DB, and ignores anything else.

So ALWAYS use parameterized queries! Or be prepared to restore your DB from backup frequently. You do take backups regularly, don't you?

The second is that you can't parameterize the column name: the SQL preprocessor does optimisations before the parameter replacement so a query like this will return no records:
SQL
DECLARE @C NVARCHAR(MAX)
Set @C = 'Title'
SELECT * FROM VIDEOS WHERE @C LIKE '%My Movie%'
That means that to be safe you have to use CASE WHEN to select only valid column names and build the SQL query that way. Concatenating the column name and "making absolutely sure that the user can't type anything into it" is risky as some future change could replace the combo box with a textbox and put your whole DB at risk.

But very, very definitely parameterize the search string! That'll probably get rid of your problem at the same time.


Choose to search for the product using combobox

The way I;'d do it to vbe safe is to have two columns in you combobox: a visible column that describes the table to search, and a hidden column which contained a search string specific to that table:
"Computer Monitors" and "Keyboards" would be the visible rows, and the hidden column would contain "SELECT * FROM PRODUCTS WHERE Monitors LIKE '%@SEARCH%'" and "SELECT * FROM PRODUCTS WHERE Keyboards LIKE '%@SEARCH%'"
That way, the user can't enter a value for the columns name which could allow SQL Injection.

You set the BoundColumn property of the ComboBox to the hidden column, and the other one will be displayed. You can access the visible data with the Text property, and the hidden data with the Value property.

With me so far? Give it a try ...
 
Share this answer
 
v2
Comments
Raedr 14-Jul-24 4:41am    
Thank you very much
Dear Friend
I am new to the world of programming and I am following lessons and I do not know the solution?
How can I solve it or how can I write the correct code in the question?
OriginalGriff 14-Jul-24 5:02am    
What part of it can you do for yourself?
What part do you need help with?
Raedr 14-Jul-24 5:20am    
Hello my friend
The part I need help with
Choose to search for the product using combobox
And write the name of the product in the text box
Raedr 14-Jul-24 12:08pm    
this
("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 ProductQuantity > 0 like '% @cboFilter.Text %' And 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 ProductQuantity > 0 Like '% @txtSearch.Text %' ", cn)
PIEBALDconsult 15-Jul-24 8:49am    
You probably don't want spaces within the strings:
'% @cboFilter.Text %' '% @txtSearch.Text %'
First of all, I would like to thank you very much
For anyone trying to help with programming
I didn't achieve anything after many attempts
What I need from this code is:
I have a Combobox. Choose a search method by barcode, product name, or company
I also have a textbox that I write in after choosing the search method, either barcode, name, or company
The search results appear on DatagardView
This is what is required
thank you
 
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