databounded listbox with datagrid results

N

nasirmajor

i have a databound listbox that has a multilple selection made to true.
when user select multiple values in listbox and clicks button i want
them to show the results in the datagrid on the same page using panels.
 
N

nasirmajor

the complete description of my problem is as follows:-


i have a databound listbox id "listboxcity" that has a multilple
selection made to true. when user select multiple values in listbox and
clicks button i want them to show the results in the datagrid id
"datagridname" on the same page using panels.

<script runat="server">
SqlConnection conn = new SqlConnection("user id=sa;password=;Initial
Catalog=jobsdb;Data Source=localhost;Integrated Security=SSPI;");
protected void Page_Load(object sender, EventArgs e)
{
SqlCommand sqlcity = new SqlCommand("Select * from tbl_city",
conn);

conn.Open();
SqlDataReader citydr;


citydr = sqlcity.ExecuteReader();
listboxcity.DataSource = citydr;
listboxcity.DataBind();
citydr.Close();
}

protected void Button1_Click(object sender, EventArgs e)
{
Panel1.Visible = false;
Panel2.Visible = true;
String s = "select * from tbl_job where city_code=@city_code";
SqlDataAdapter sqlda = new SqlDataAdapter(s, conn);
sqlda.SelectCommand.Parameters.Add(new SqlParameter("@city_code",
SqlDbType.Int, 4));
sqlda.SelectCommand.Parameters["@city_code"].Value =
listboxcity.SelectedValue;
DataSet ds = new DataSet();
sqlda.Fill(ds, "tbl_job");
datagridname.DataSource = ds.Tables["tbl_job"].DefaultView;
datagridname.DataBind();
}

till now it is working for single value selection and its succsseful
code is given as above. Now how to make changes in it for multiple
selection.

I have know the code that how to get multiple values from listbox by
foreach loop structure e.g

int returnselectedvalues()
{
int values = "";
foreach (ListItem li in listboxcity.Items)
{
if (li.Selected == true)
{
values += li.Value ;
}
}
return values;
}

and I think for this case SQL SELECT statement should be use with IN
key word
like e.g

USE Northwind
SELECT CompanyName, Country
FROM Suppliers
WHERE Country IN ('Japan', 'Italy','USA','UK')


With all of this case I have put it up to you dears to solveout my
problem.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,067
Latest member
HunterTere

Latest Threads

Top