mysql - Select query from multiple textboxes c# -



mysql - Select query from multiple textboxes c# -

i still new both c# , sql please maintain in mind. searched site couldn't find answer.

i building basic asp web form in c# connect mysql database , populates gridview results.

i have 2 text boxes on web page: phone number , user id. have button when clicked runs next code:

protected void btnsubmit_click(object sender, eventargs e) { { seek { conn.open(); //sql mysqlcommand cmd = new mysqlcommand("select * message phone_number @patientmob , user_id @userid, conn); //paramaters cmd.parameters.add(new mysqlparameter(@"patientmob", mysqldbtype.varchar)).value = patientmobile.text; cmd.parameters.add(new mysqlparameter(@"userid", mysqldbtype.varchar)).value = userid.text; mysqldataadapter adp = new mysqldataadapter(cmd); dataset ds = new dataset(); adp.fill(ds); gridview.datasource = ds;

..etc

my question is, how format query works if 1 textbox filled in? @ moment, if come in phone number not come in user id, results showing people phone number have nil entered user id field.

i adding more text boxes later title, first name, surname..etc , need solution work combination of fields filled in or not filled in.

thanks.

i don't know of great way this, unfortunately, have few options.

build string dynamically

something this:

list<string> wheres = new list<string>(); list<mysqlparameter> parameters = new list<mysqlparameter>(); if (!string.isnullorwhitespace(patientmobile.text)) { wheres.add("phone_number @patientmob"); parameters.add(new mysqlparameter(@"patientmob", mysqldbtype.varchar) { value = patientmobile.text }); } ... string query = string.format("select * messages {0}", string.join(" , ", wheres));

and on , forth...

check dynamically what's empty in query

something this:

string query = "select * message (len(@patientmob) = 0 or phone_number @patientmob) , (len(@userid) = 0 or user_id @userid)", conn); i see you're using like, , admittedly i'm not 1 mysql way off, seems wrapping parameters whatever character designates wildcard match. in t-sql, column '%' + @param + '%'. of course, won't exact match, sounds might you're looking for. in cases of empty search boxes, result in column '%%', match values.

c# mysql asp.net

Comments

Popular posts from this blog

Delphi change the assembly code of a running process -

java - Parsing XML, skip certain tags -

c# - ASP.NET MVC Sequence contains no matching element -