Click here to Skip to main content
15,879,326 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i am trying to do a c# mysql
select * from table where variable =(selected tableColumn from dropdownlist)
like variable(text from text box does anyone know what the query should look like in my sql
Posted
Updated 10-Oct-12 23:54pm
v2

C#
MySqlCommand cmd = new MySqlCommand("Select * from table where variable = '@selectedValue'");
cmd.Parameters.AddWithValue("@selectedValue", value);
...
 
Share this answer
 
v2
Comments
mrDivan 11-Oct-12 10:59am    
Hi Marcus

Thank you for your response
here is my code im trying to use the selected value from a dropdown list as a variable but that seems broken

protected void btnFilter_Click(object sender, EventArgs e)
{

string filterlist = ddFillist.SelectedItem.Value;
string filterText = " % " + txtFilter.Text + "%";
MySql.Data.MySqlClient.MySqlConnection myconfill = new MySqlConnection(GetConnectionString());
if (myconfill.State != ConnectionState.Open)
try
{
myconfill.Open();
}
catch (MySqlException ex)
{
throw (ex);
}


// where ?filterList like ?filterText
MySqlCommand sqlC = new MySqlCommand("select * from T_company where ?filterlist like ?filterText ", myconfill);
MySqlDataAdapter da = new MySqlDataAdapter(sqlC);

sqlC.Parameters.AddWithValue("?filterlist", filterlist);
sqlC.Parameters.AddWithValue("?filtertext",'%'+txtFilter.Text+'%');


DataSet ds = new DataSet();
da.Fill(ds);
gdvauthors.DataSource = ds;
gdvauthors.DataBind();
myconfill.Close();
}
if i change ? filterlist with company_name it works seems like im somehow not giving my parameter the right vale
fjdiewornncalwe 11-Oct-12 11:02am    
Why the ?filterlist. The accepted way to use parameters is @filterlist.
Maciej Los 11-Oct-12 13:33pm    
Good answer, my 5!
mrDivan 12-Oct-12 2:43am    
Ho I changed the parameters back to@filterlist but it doesnt work but thanks for your help anyway
turns out you cannot use column names as a parameter in mysql so I wrote I different select statement for each column chose from the dropdown list
 
Share this answer
 
C#
select * from table where variable in (selected tableColumn from dropdownlist)
 
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