Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

What's wrong with this code?

Status
Not open for further replies.

VBRookie

Programmer
May 29, 2001
331
US
Code:
public class selectEmployee : BasePage
 {
  protected string[,] employeeArray = new string[100, 3];

  private void Page_Load(object sender, System.EventArgs e)
    {
     // GET THE RESULTS AND FEED INTO AN ARRAY
     populate_employee_array();

     if ( !Page.IsPostBack )
        {
           ViewState["employees"] = employeeArray;
        }
    }
 }


When I do this I get the error "Specified Cast is not valid".  If I comment out the line that saves the array into viewstate, the page comes up fine.

Any clues?

- VBRookie
 
I don't think you can put arrays into viewstate. There's something special you have to do with them (same with putting objects into viewstate. There's some process where you have to set the whole object to a string before passing, or something like that)

D
 
Nope, I do it on one occasion in my code.
See thread855-398063 That'l do donkey, that'l do
[bravo] Mark
If you are unsure of forum etiquette check here faq796-2540
 
Hmmm...
ok, then try this:

take what you want to do, and write it in vb.net.

Works every time.
;)

(yes, I know: this was a useless, yet somewhat amusing post)
;)

D
 
ViewState.Item["employees"] = employeeArray;

perhaps?

Just a shot in the dark.
penny1.gif
penny1.gif
 
should the square brackets be round ones [] vs ()? That'l do donkey, that'l do
[bravo] Mark
If you are unsure of forum etiquette check here faq796-2540
 
Ok some light has been shed on the issue. ViewState seems to have a problem with multidimensional arrays. I switched this to a single dimensioned array and it worked fine. What I ended up doing was creating a separate class for the data with a constructor for collecting it. I had to declare it as serializable to save it to viewstate. Then I created an array of type myClass which was single dimensional and saved that to viewstate. This is working fine. Here is the code:

Code:
Serializable()]
	public class EmployeeInformation
	{
		private string _employeeSSN;
		private string _employeeFirstName;
		private string _employeeLastName;

		public EmployeeInformation ()
		{}
		
		/// <summary>
		/// 
		/// </summary>
		/// <param name=&quot;pSSN&quot;></param>
		/// <param name=&quot;pFirstName&quot;></param>
		/// <param name=&quot;pLastName&quot;></param>
		public EmployeeInformation (string pSSN, string pFirstName, string pLastName)
		{
			_employeeSSN = pSSN;
			_employeeFirstName = pFirstName;
			_employeeLastName = pLastName;
		}

		public string employeeSSN
		{
			get {return _employeeSSN;}
			set {_employeeSSN = value;}
		}

		public string employeeFirstName
		{
			get {return _employeeFirstName;}
			set {_employeeFirstName = value;}
		}

		public string employeeLastName
		{
			get {return _employeeLastName;}
			set {_employeeLastName = value;}
		}
	}


public class selectEmployee : BasePage
	{
		protected EmployeeInformation [] empArray = new EmployeeInformation[100];
                protected int resultsCount = 0;



private void Page_Load(object sender, System.EventArgs e)
		{
						// GET THE RESULTS AND FEED INTO AN ARRAY
			populate_employee_array();

			// RETRIEVE RESULTS FROM ARRAY AND DISPLAY
			if ( !Page.IsPostBack )
			{
				// SET VALUES FOR POSTBACK
				ViewState[&quot;employees&quot;] = empArray;
			} 
		}

private void populate_employee_array()
		{
			int i;

			for (i = 0; i <= 77; i++)
			{
				empArray[i] = new EmployeeInformation ();
				empArray[i].employeeSSN = &quot;111111111&quot;;
				empArray[i].employeeLastName = &quot;Melancthon&quot;;
				empArray[i].employeeFirstName = &quot;Luther&quot;;
			}

			resultsCount = i-1;
		}

Thanks mates for all your help!

- VB Rookie
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top