Passing an Array into Viewstate

  • Thread starter Thread starter John Miller
  • Start date Start date
J

John Miller

I'm developing a form that captures patient information. In the form, I
need the ability to add several insurance policies. The approach I have
decided to take is to load the separate insurance policies into a
multidimensional array. So:



// I create my array

string[,] arrInsurance = new string[5,15];



// During the AddButton_Click routine, I fill out my array with the current
data in the insurance form

arrInsurance[i,0] = "0" ;

arrInsurance[i,1] = "2" ;

arrInsurance[i,2] = strCompanyName ;

arrInsurance[i,3] = strStreetNbr ;

arrInsurance[i,4] = strStreetName ;

arrInsurance[i,5] = strCity ;

arrInsurance[i,6] = strState ;

arrInsurance[i,7] = strZip ;

arrInsurance[i,8] = strGroupNbr ;

arrInsurance[i,9] = strPolicyNbr ;

arrInsurance[i,10] = strClaimOrder ;

arrInsurance[i,11] = strPolicyType ;

arrInsurance[i,12] = strPolicyHolder ;

arrInsurance[i,13] = strSSN ;

arrInsurance[i,14] = strDOB ;



// But I need to save my array before I leave the AddButton_Click routine
into Viewstate, so I don't lose the data:

ViewState["ArrInsurance"] = arrInsurance. ;



This is about as far as I can get. The form lists the added insurance
policy, and allows the user to add another if they need to do so. The form
is reloaded and I lose my data. So I need to store the data in ViewState so
I can grab it back out of the array. The program bombs with some general
error, and I just can't figure this one out. Thank you in advance for
helping me with this.



While were at it, how do I get the array back from viewstate?
 
it might be better to avoid viewstate storage.
Heres a simple example (session)

public class PolicyInput : System.Web.UI.Page {

void ShowPolicyList() {
ArrayList list = PolicyList;
if (list != null) {
foreach(Policy p in list) {
//show policy here
}
}
}

ArrayList PolicyList {
get {
ArrayList list = (ArrayList)Session["PolicyList"];
if (list == null) {
Session["PolicyList"] = new ArrayList();
}
return list;
}

private void btnAddPolicy_Click(object sender, System.EventArgs e) {
if (txtPolicyName.Text.Trim() != null) {
ArrayList list = PolicyList;
list.Add(new Policy(txtPolicyName.Text.Trim()));
//...
ShowPolicyList();
}
}

}

//Policy class
class Policy {

string name;

public Policy(string name) {
this.name = name;
}

public string Name {
get {
return name;
}
}

}

Hope this helps
 

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

Back
Top