Click here to Skip to main content
15,944,733 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

am developing winforms application,

listbox is multiselection listbox.

am filling listbox with datasource, depending on selected values from listbox,
i need to get data from database,

how to solve it.

C#
<pre lang="c#"> using (MySqlConnection con = new MySqlConnection(ConnectionString))
            {
                string query = "select distinct area_code from customer";
                con.Open();
                MySqlDataAdapter da = new MySqlDataAdapter(query,con);
                DataTable dt = new DataTable();
                da.Fill(dt);
                listBox1.DataSource = dt;
                listBox1.DisplayMember = "area_code";
            
            }



listbox is displaying fine as ex:11
33
44

when i select 11, 33

i need a query to select data from database.

am using:
C#
<pre lang="c#"> foreach (Object item in listBox1.Items)
            {
                text += item.ToString() + " ";
            }

            area = text;


and query is:
query1 = "select customer_id,name,area,mobile_number,package_name,package_type,activation_date,status,monthly_amount,lastpaid_date,lastpaid_amount,nextpay_date,totalamount_paid,totaldue_amount,total_charges from customer where area_code="+area+" ";
Posted

1 solution

First you should build your area temp string like this:
C#
string area = string.Empty;
foreach (Object item in listBox1.Items)
{
 if(area.Length > 0) area += ", ";
 area += item.ToString();
}

Then to use the area string to build your query:

C#
query1 = "select customer_id,name,area,mobile_number,package_name,package_type,activation_date,status,monthly_amount,lastpaid_date,lastpaid_amount,nextpay_date,totalamount_paid,totaldue_amount,total_charges from customer where area_code in ("+area+")";
 
Share this answer
 
v3
Comments
Member 10263519 24-Mar-14 5:06am    
when i observe by keeping breakpoint at area in above code.
am getting area value "System.Data.DataRowView"

am not getting what's the problem.
Raul Iloc 24-Mar-14 5:34am    
In my solution "area" is just a name for a temp string variable, so you have to use other name for it if this name is already used for other object in your code!
Member 10263519 24-Mar-14 6:16am    
i solved
Raul Iloc 24-Mar-14 7:38am    
OK, I hope that my solution helped you!

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