I have several listboxes in 1 page and there is a "search" button.Whenever user select any values from the listbox's and click search button the query fires and it is showing the desired results.But I want 2 more button on this page "save Query" and "Load Query".
Requirment is:
After selecting values from listboxes before pressing "search" button user should click "Save Query" button for saving his selected values and then he should press search button.Now when the user log in next time if he clicks "Load Query" Button he can see his selected values that he had selected last time.
Actually my application runs on windows authentication.No login user id password is nesessary.So there is no database fields for that.
I want to store listbox selected item in cookies.
Right Now the value is saving in the Cookies but only the last selected value.Please see in my No:2 section for the code that I wrote.

I Tried my code like this but facing some problem:

1)After saving the values if I close the application and again come back to that particular page the values is not selected......
I am writing this code in a click event of a button.
string val=Request.Cookies["toton"].Value;
if(val != null)
{
ListBox1.SelectedValue=val;
}

It is throwing error:Object reference not set to an instance of an object.
System.NullReferenceException.

2)I have several listBoxes.But in cookies it is always saving the last listbox value.Not all the selected value.
I am using this code:

HttpCookie MyCookie = new HttpCookie("toton");
MyCookie.Value = ListBox1.SelectedItem.Text;
MyCookie.Value=ListBox2.SelectedItem.Text;
MyCookie.Expires=DateTime.MaxValue;
Response.Cookies.Add(MyCookie);

Here always ListBox2.SelectedItem.Text is saving in Cookies.

3)There are 10 ListBoxes in that page.Its not mandetory user will select all the List Boxes.
So if in any situation user is selecting 4 of them,those all 4 values should be saved in cookies and in next login those 4 should be selected.

Can anyBody help me in this regards......

All the ListBox values are storing in Cookies and fetching from there also.
But my requirment is different:
In all the ListBox's there are multi selection facility.But in this case if I select 2 values from a single ListBox only the first one is saving in the cookies.
As a result when I clicking in the "LoadQuery" instead of 2 only 1 value is selected showing.

2) There are 13 ListBox's in this page and there may be situation that out of 13 only 3 has been selected by the user.But in this case as per my code I have to select all the ListBoxes otherwise it is giving error.
What will be the if condition?

Please let me know with this multi selection facility is this approach a right one?Means storing values in Cookies?
If there any other approach? could you please help me as per my requirment?

My Code:

private void SaveQuery_Click(object sender, System.EventArgs e)
{
HttpCookie htk=new HttpCookie("toton");
HttpCookie htk1=new HttpCookie("toton1");
htk.Value=ListBox1.SelectedItem.Text;
htk1.Value=ListBox2.SelectedItem.Text;
htk.Expires=DateTime.MaxValue;
htk1.Expires=DateTime.MaxValue;
Response.Cookies.Add(htk);
Response.Cookies.Add(htk1);
}


private void LoadQuery_Click(object sender, System.EventArgs e)
{
HttpCookie cookie = Request.Cookies["toton"];
if (cookie != null)
{
ListBox1.SelectedValue = cookie.Value;
}
HttpCookie cookie1 = Request.Cookies["toton1"];
if (cookie1 != null)
{
ListBox2.SelectedValue = cookie1.Value;
}
}

In this scenarion I am taking 2 ListBoxes as an example.
Waiting for your response...............

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.