Click here to Skip to main content
15,305,969 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
How do I search code in C #?
I plan to search for the name of a button and a textbox called txtsearch and txtsearch. I have been searching for about a Datagrid I am using.

User information entered in the textbox and when I got the information data in the database show the code that I wrote it in Datagrid cumin to see me what I should write


C#
SqlConnection conn = new SqlConnection();
        conn.ConnectionString = @"Data Source=PARIS\SQLEXPRESS;Integrated Security=True";
        conn.Open();
        SqlCommand cmd = new SqlCommand();
        cmd.Connection = conn;
      cmd = new SqlCommand ("select * from samira where name like '%"+txtsearch.Text+"%'");
      DataTable dt = new DataTable();
      SqlDataAdapter adapter = new SqlDataAdapter();
      adapter.Fill(dt);
      dataGridView1.DataSource = dt;
      dataGridView1.Visible = true;
      conn.Close();



This will stop running adapter.fill (dt);
Where does the fault code?
Posted
Updated 19-Jan-22 3:24am
v2

In this line:

C#
cmd = new SqlCommand ("select * from samira where namelike N'"+txtsearch.Text+"%'");


You need to put a space between "name" and "like".
   
SqlConnection conn = new SqlConnection();
            conn.ConnectionString = @"Data Source=PARIS\SQLEXPRESS;Integrated Security=True";
            conn.Open();
            SqlDataAdapter adapter = new SqlDataAdapter("select * from samira where name like '%"+txtsearch.Text+"%'",conn);
          DataTable dt = new DataTable();
          adapter.Fill(dt);
          dataGridView1.DataSource = dt;
          dataGridView1.Visible = true;
          conn.Close();
   
string StrSqlCon = "Data Source=LOLLAESERVER\\SqlExpress;Initial Catalog=Students;Persist Security Info=True;User Id=sa;*********=Tadstadiwa";
string StrSearch = "Select * From Students where StudentName like
'%"+txtStuName.Text+"%'",conn);
SqlConnection SqlCon = new SqlConnection(StrSqlCon);
SqlCommand cmd = new SqlCommand(StrSearch, SqlCon);
SqlCon.Open();
SqlDataReader r = cmd.ExecuteReader();
Student s = new Student();
   
Comments
Richard Deeming 19-Jan-22 11:41am
   
An unformatted, unexplained code-dump, which barely differs from the existing solutions, and which has not addressed the SQL Injection[^] vulnerability from the question and other solutions, does not add anything to the discussion on this 11-year-old question.

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