Click here to Skip to main content
15,895,813 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
i am making a search page in where i am putting a textbox and submit button. when the user enters a text then on button click what should happen is that the keyword should be binded with sql query and it should look for keywords matching that query in database.

i tried this query in sql server management studio and it was working:

SELECT * from project WHERE (pcode LIKE 'a%') OR (fundingagency LIKE 'r%')

what i want is to bind the textbox data in both the columns like what i want is

string query = "select pcode,fundingagency from project where=" ('pcode' like TextBox1.Text) or ('fundingagency' like TextBox1.Text) ;
SqlDataAdapter adp = new SqlDataAdapter(query,con);

can somebody format this query for me. casue i m getting errors saying "the multi-part identifiesr "textbox.text"could not be found.
Posted
Updated 18-Jan-13 22:22pm
v3

Try
string query = "select pcode,fundingagency from project where (pcode like '" + TextBox1.Text) + "' or (fundingagency like '" + TextBox1.Text + "')" ;
.
 
Share this answer
 
Try:
C#
string query = "select pcode,fundingagency from project where (pcode like '" + TextBox1.Text+ "') or (fundingagency like '" + TextBox1.Text + "')";
SqlDataAdapter adp = new SqlDataAdapter(query,con);

But seriously, you shouldn't do it like that at all! Do not 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. Use Parametrized queries instead:
C#
string query = "select pcode,fundingagency from project where (pcode like @SS) or (fundingagency like @SS)";
SqlDataAdapter adp = new SqlDataAdapter(query,con);
adp.SelectCommand.Parameters.AddWithValue("@SS", TextBox1.Text);
Both of these assume that the user has added the '%' character to his text box.
 
Share this answer
 
Comments
Abhinav S 19-Jan-13 4:26am    
5 of course. The additional suggestion was a good one.
OriginalGriff 19-Jan-13 4:31am    
Yeah - a web site search box that feeds directly into an SQL query? Brrrr! It's enough to make me feel very nervous indeed...
BTW: one of the brackets is in the wrong place in your solution.
a2ulthakur 19-Jan-13 4:29am    
but its at the front end of the form. so should i ask my users to enter % before or after keyword cause that will be messy
OriginalGriff 19-Jan-13 4:37am    
No - you could add them to the query:
(pcode like '%' + @SS + '%')
for example.
a2ulthakur 19-Jan-13 4:30am    
plus in the second query that u have mentioned we are not taking the user entered text in the textbox how will the query fetch that value
protected void Button1_Click(object sender, EventArgs e)
    {
        string query = "select pcode, pname, pi, copi, fundingagency, total from project where (pcode like'%'+ @SS +'%')  or (fundingagency like '%' + @SS + '%')";
        SqlDataAdapter adp = new SqlDataAdapter(query, con);
        adp.SelectCommand.Parameters.AddWithValue("@SS", TextBox1.Text);
        
       DataTable dt = new DataTable();
        adp.Fill(dt);
        GridView1.DataSource = dt;
        GridView1.DataBind();
    }


this worked for me
 
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