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

Recommended Answers

All 5 Replies

u can use this query
select * from customers_table where cust_id='"+txtcustbox.texxt+"'

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();
            }

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.