I can't find a tutorial anywhere on how to search a sql database using a textbox and a button in asp.net any help. I'm coding in C# Sql database has a customers table I want to display firstname and lastname by searching cust_no

No kept getting error CS1012
This got accepted without asp throwing an error but don't know if it works need to figure out how to post the results now

string sqlCmd = "Select * From customer Where cust_no = '" + CustSearch + "'";

what control u want to display data.there are various ways of displaying data using gridview,datagrid,datalist .. so on.
u need to connect to a database first.
u do that using connection string
SqlConnection conn=new sqlconnection("User ID=sa; Initial Catalog=databasename; Data Source=u r datasource");
after connecting,u need to open it

conn.Open();
declare a dataadapter to contain the data u retrieve using select statement

SqlDataAdapter da = new SqlDataAdapter("", conn);
string query = "u r query";
da.SelectCommand = new SqlCommand(query, conn);
da.SelectCommand.ExecuteScalar();
based on the data u can return the tables using icollection,dataset etc

// this is connection
con = new SqlConnection("initial Catalog=Database name; server=.; Integrated Security=true;");
//this is adapter
            SqlDataAdapter ad = new SqlDataAdapter("select * from table name where searched feild like '%" + textBox3.Text + "%'", con);
            SqlCommandBuilder cb = new SqlCommandBuilder(ad);
            ds.Clear();
            ad.Fill(ds);
            dataGridView1.DataSource = ds.Tables[0];
            if (textBox3.Text == "" || ds.Tables[0].Rows.Count == 0)
            {
                dataGridView1.Visible = false;
            }
            else
            {
                dataGridView1.Visible = true;
                con.Close();
            }

Edited 5 Years Ago by Ezzaral: Added code tags. Please use them to format any code that you post.

The last post of mine can be written in text changed event of textbox and this code will search as you type.

This article has been dead for over six months. Start a new discussion instead.