I hope you will add your sql statement. The code snippet show the sql query when you press a button.
private void button1_Click_1(object sender, EventArgs e)
{
if (listBox1.SelectedItems.Count == 0)
{
MessageBox.Show("select items");
return;
}
string s = "";
for (int i = 0; i < listBox1.SelectedItems.Count; i++)
{
if(i==listBox1.SelectedItems.Count-1)
s = s + " item='" + listBox1.SelectedItems[i].ToString() + "'";
else
s=s + " item='" + listBox1.SelectedItems[i].ToString() + "' or ";
}
s = "select * from table where " + s;
MessageBox.Show(s);
}
__avd
Posting Genius (adatapost)
8,736 posts since Oct 2008
Reputation Points: 2,141
Solved Threads: 1,262
Skill Endorsements: 50
thank you for your answers.
the problem with the multiselect thing is this:
if there was only one selected item i could create a tableadapter than bind it easily to the listbox.
but i dont know how many selecteditems there will be, so i should create the query dynamically.
my problem was i don't know how to execute sql in c#.
thanks to "Diamonddrake" now i can populate my listbox.
for just info this will be my query:
string querystr = "SELECT DISTINCT sinif FROM FileTemp WHERE okuladi IN(";
for (int i = 0; i < schoolListBox.SelectedItems.Count; i++)
{
querystr = querystr + "'" + schoolListBox.SelectedItems[i] + "'";
if (i != schoolListBox.SelectedItems.Count - 1)
querystr = querystr + ",";
else
querystr = querystr + ")";
}
You can add following code to populate another listbox:
SqlDataAdapater adp=new SqlDataAdapter(querystr,"set connection string");
DataTable dt=new DataTable();
adp.Fill(dt);
anotherList.DataSource=dt;
anotherList.DisplayMember=dt.Columns[0].ColumnName;
__avd
Posting Genius (adatapost)
8,736 posts since Oct 2008
Reputation Points: 2,141
Solved Threads: 1,262
Skill Endorsements: 50
Antoher question can i populate listbox with a list of string
Create - .mdf (SQL Server Database) for your desktop or web applciation. .sdf is a SQL Server Compact DB and it is used with .NET Compact Framework.
__avd
Posting Genius (adatapost)
8,736 posts since Oct 2008
Reputation Points: 2,141
Solved Threads: 1,262
Skill Endorsements: 50