Question : How do I pass multiple listbox selections in a query string?

I would like to use a listbox and allow the user to query multiple selections from the listbox.  Below are bits of my code so far.  Thank you in advance!
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
80:
81:
82:
83:
84:
85:
86:
// -------------------------

// ASP.NET (myReport.aspx):
	<asp:ListBox ID="myListBox" AutoPostBack="true" OnSelectedIndexChanged="OnSelectMyListBox_Click"
        	AppendDataBoundItems="True" runat="server" DataSourceID="MyListBoxDataSource" DataTextField=" myListBoxItem"
        	DataValueField="id" SelectionMode="Multiple" Rows="9">
        	<asp:ListItem Selected="True" Value="">All ListBox Items</asp:ListItem>
        </asp:ListBox>	


// Code Behind (myReport.aspx.cs):
	string URL;
        URL = "viewReport.aspx?page=detail" + "&lbid" + myListBox.SelectedValue;


// -------------------------

// ASP.NET (viewMyReport.aspx):
	<CR:CrystalReportViewer ID="myReport" runat="server" AutoDataBind="True"
            Height="1055px" Width="920px" EnableDatabaseLogonPrompt="False" EnableParameterPrompt="False"
            ReuseParameterValuesOnRefresh="True" />


// Code Behind (viewMyReport.aspx.cs) (loads Crystal Report):
    private ReportDocument reportDocument = new ReportDocument();
	
	protected void Page_Init(object sender, EventArgs e)
	{
		txtPageID.Text = Request.QueryString["page"];
       	 	myListboxID = Request.QueryString["lbid"];


        	ParameterField paramField = new ParameterField();
       	 	ParameterFields paramFields = new ParameterFields();
        	ParameterDiscreteValue paramDiscreteValue = new ParameterDiscreteValue();

        	//Set instances for input parameter 2 - 
        	paramField = new ParameterField();
        	paramField.Name = "@myLbId";
        	paramDiscreteValue = new ParameterDiscreteValue();
        	if (myListBoxID == "")
        	{
            		paramDiscreteValue.Value = DBNull.Value;
        	}
        	else
        	{
            		paramDiscreteValue.Value = myListBoxID;
        	}
        	paramField.CurrentValues.Add(paramDiscreteValue);

        	//Add the paramField to paramFields
        	paramFields.Add(paramField);

        	MyReport.ParameterFieldInfo = paramFields;

        	reportDocument.Load(Server.MapPath("MyReport.rpt"));

		reportDocument.SetDatabaseLogon("myUserName", "myPassword");

		MyReport.ReportSource = reportDocument;

	}

// -------------------------


// Crystal Report (MyReport.rpt)
// Calls SQL Stored Procedure and displays the data


// -------------------------

// SQL Stored Procedure (populates data on Crystal Report):
alter procedure [dbo].[GenerateMyReport]
	@myLbId int
as
	begin
		select
			*
		from
			myTable
		where
			myColumn = @myLbId
	end

//-------------------------

Answer : How do I pass multiple listbox selections in a query string?

You can write a function that will con-cat all selected Items from listbox with "," or any other separator you want.
Then you can put that in query string.

But remember that we have limitation of number of characters in query string.
If it exceeds that, then you need to use session / view-state / properties etc
Random Solutions  
 
programming4us programming4us