Click here to Skip to main content
Rate this: bad
good
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
mrDivan893
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 at 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 at 11-Oct-12 11:02am
   
Why the ?filterlist. The accepted way to use parameters is @filterlist.
Maciej Los at 11-Oct-12 13:33pm
   
Good answer, my 5!
mrDivan at 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 2

select * from table where variable in (selected tableColumn from dropdownlist)
  Permalink  
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  

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

  Print Answers RSS
0 OriginalGriff 554
1 Gihan Liyanage 338
2 RyanDev 258
3 Sergey Alexandrovich Kryukov 248
4 ChauhanAjay 220
0 Sergey Alexandrovich Kryukov 8,936
1 OriginalGriff 7,891
2 CPallini 2,603
3 Richard MacCutchan 2,121
4 Abhinav S 1,928


Advertise | Privacy | Mobile
Web02 | 2.8.140827.1 | Last Updated 17 Oct 2012
Copyright © CodeProject, 1999-2014
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