Click here to Skip to main content
15,886,362 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i have using this code for searching in datagridview
C#
dataGridView1.Rows.Clear();
con.Open();
SqlCommand cmd = new SqlCommand("select * from enquery where sname='" + textBox1.Text + "' or contectno='" + textBox1.Text + "' or courese='" + textBox1.Text + "'or remark='" + textBox1.Text + "' or edate='" + textBox1.Text + "' or id='" + textBox1.Text + "' or fudate='" + textBox1.Text + "' or fustatus='" + textBox1.Text + "' or cancel='" + textBox1.Text + "' ", con);
SqlDataReader dr = cmd.ExecuteReader();
while (dr.Read())
{
object[] row = { dr[0], dr[1], dr[2], dr[3], dr[4], dr[5], dr[6], dr[7], dr[8] };
dataGridView1.Rows.Add(row);
}
con.Close();

this code is working if all the field in sql are same datatype but not working if those field are different data type , example sname is varchar contectno is bigint so it show exception , can anybody help me short out this problum.i have only one textbox and user can search by any field.
Posted
Updated 6-Aug-15 21:05pm
v2
Comments
[no name] 7-Aug-15 6:10am    
If you use proper parameterized queries you will gain immediate 2 benefits. Your code will be more readable and your database will have a reduced chance of getting deleted by SQL injection attacks.
Member 10273293 8-Aug-15 3:44am    
not understand ur point ?

1 solution

u can use only nvarchar() datatype I t will accept both alphabets and numerics..


i.e convert(nvarchar(max),textbox) in your db..
 
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