Click here to Skip to main content
12,290,988 members (54,529 online)
Rate this:
 
Please Sign up or sign in to vote.
See more: C# MySQL
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 10-Oct-12 23:00pm
mrDivan1.2K
Edited 10-Oct-12 23:54pm
v2
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 3

MySqlCommand cmd = new MySqlCommand("Select * from table where variable = '@selectedValue'");
cmd.Parameters.AddWithValue("@selectedValue", value);
...
  Permalink  
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
Marcus Kramer 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
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 4

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
  Permalink  
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 2

select * from table where variable in (selected tableColumn from dropdownlist)
  Permalink  

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Mobile
Web01 | 2.8.160518.1 | Last Updated 17 Oct 2012
Copyright © CodeProject, 1999-2016
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100