Click here to Skip to main content
15,897,718 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello! i create a search button for search database from ms SQL server.
C#
SqlConnection con = new SqlConnection("Data Source=.\\SQLEXPRESS;AttachDbFilename=C:\\Documents and Settings\\meekun.poon\\My Documents\\Visual Studio 2008\\Projects\\TrackLocation\\TrackLocation\\barcodePrinter.mdf;Integrated Security=True;User Instance=True");
              DataTable dt = new DataTable();
                             SqlDataAdapter SDA = new SqlDataAdapter("SELECT SerialNo,IT_Tag,PrinterID,Product,Model,Department,Location FROM [BarCodePrinter]WHERE BarCodePrinter.SerialNo= ''" +( textBox1.Text), con);
                  SDA.Fill(dt);
                  SDA.ToString();
                  dataGridView1.DataSource = dt;

**above is part of code...
But when click the search button, there are not data display.
Can someone one tell me the problem and solution?

thanks
Posted
Updated 14-Nov-12 20:36pm
v2

Because you got the SELECT statement wrong: the quotes are in the wrong place:
C#
SqlDataAdapter SDA = new SqlDataAdapter("SELECT SerialNo,IT_Tag,PrinterID,Product,Model,Department,Location FROM [BarCodePrinter]WHERE BarCodePrinter.SerialNo= ''" +( textBox1.Text), con);
Becomes
C#
SqlDataAdapter SDA = new SqlDataAdapter("SELECT SerialNo,IT_Tag,PrinterID,Product,Model,Department,Location FROM [BarCodePrinter]WHERE BarCodePrinter.SerialNo= '" + textBox1.Text + "'", con);


But don't do it like that! 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#
SqlDataAdapter SDA = new SqlDataAdapter("SELECT SerialNo,IT_Tag,PrinterID,Product,Model,Department,Location FROM [BarCodePrinter]WHERE BarCodePrinter.SerialNo=@SN", con);
SDA.SelectCommand.Parameters.AddWithValue("@SN", textBox1.Text);
 
Share this answer
 
Comments
Member 10690458 22-Mar-14 10:39am    
I want to create search button for my windows application which is show only my local disk result
OriginalGriff 22-Mar-14 10:49am    
So ask a question here:
http://www.codeproject.com/Questions/ask.aspx
And give all the details you can: we can't see your screen!
Thanks....the datagridview can display data based on the textBox...
 
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