SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 What is wrong with this select query
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

Abid
Posting Yak Master

Pakistan
105 Posts

Posted - 04/03/2013 :  12:11:24  Show Profile  Reply with Quote
HI. I have a helping form, which appears on the leave event of the textbox from main form (SaleInfo). When this form is loaded so the Prod_Name from ProdInfo is loaded into Combobox (where combobox is set to AutoComplete).

On the form there is a button on which i have the following Query:

Dim selProd As String = "select * from ProdInfo where Prod_Name like '%" & ComboBox1.Text & "%'"
I want to load only that product into a DataGridView on the same helping form, which is select in Combobox. But when i press the button so it displays the entire table values. Please tell me that what is wrong with this query.

chadmat
The Chadinator

USA
1974 Posts

Posted - 04/03/2013 :  12:17:42  Show Profile  Visit chadmat's Homepage  Reply with Quote
Set a breakpoint on that line and make sure ComboBox1.Text is what you expect it to be.

-Chad
Go to Top of Page

Abid
Posting Yak Master

Pakistan
105 Posts

Posted - 04/03/2013 :  21:34:21  Show Profile  Reply with Quote
yes i put a breakpoint on my query. When the break point occurs, so its equal to nothing. But then i put the breakpoint on the next line, right after the query, i.e.

Dim selProd As String = "select * from ProdInfo where Prod_Name like '%" & ComboBox1.Text & "%'"
                Dim cmdSql As New SqlCommand(selProd, cnSql)
                cmdSql.CommandType = CommandType.Text
                daSql.SelectCommand = cmdSql

                daSql.Fill(dsSql, "ProdInfo")

                dgvSalesPicker.DataSource = dsSql
                dgvSalesPicker.DataMember = "ProdInfo"


This is what it appears in breakpoint.
selProd = "select * from ProdInfo where Prod_Name like '%Keyboard%'"
Go to Top of Page

LoztInSpace
Aged Yak Warrior

940 Posts

Posted - 04/04/2013 :  00:48:22  Show Profile  Reply with Quote
quote:
Originally posted by Abid

Please tell me that what is wrong with this query.


Apart from the SQL injection and performance issue you mean?
Go to Top of Page

Abid
Posting Yak Master

Pakistan
105 Posts

Posted - 04/04/2013 :  05:41:21  Show Profile  Reply with Quote
Apart from SQL Injection.
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

India
52317 Posts

Posted - 04/04/2013 :  06:07:28  Show Profile  Reply with Quote
are you looking at pattern serach or exact search? ie do you want records having word Keyboard in their name or do you want only keyboard record's detail?

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/
https://www.facebook.com/VmBlogs
Go to Top of Page

Abid
Posting Yak Master

Pakistan
105 Posts

Posted - 04/04/2013 :  08:28:25  Show Profile  Reply with Quote
This time i need only exact search.

But it would extremely applausable if you guide me for both.
Go to Top of Page

chadmat
The Chadinator

USA
1974 Posts

Posted - 04/04/2013 :  12:51:30  Show Profile  Visit chadmat's Homepage  Reply with Quote
The way it is now, you should be getting anything that contains the word "Keyboard". If you want exact match, change

LIKE '%Keyboard%'
to
='Keyboard'

-Chad
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

India
52317 Posts

Posted - 04/04/2013 :  14:29:10  Show Profile  Reply with Quote
quote:
Originally posted by Abid

This time i need only exact search.

But it would extremely applausable if you guide me for both.



if you want exact search the statement should be


Dim selProd As String = "select * from ProdInfo where Prod_Name = '" & ComboBox1.Text & "'"


------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/
https://www.facebook.com/VmBlogs
Go to Top of Page

Abid
Posting Yak Master

Pakistan
105 Posts

Posted - 04/06/2013 :  05:37:21  Show Profile  Reply with Quote
I think the problem is with, not clearing the DataSet. I'm just clearing the dataSet so the problem is solved.
Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.06 seconds. Powered By: Snitz Forums 2000