5 Replies - 1781 Views - Last Post: 27 September 2007 - 04:53 AM Rate Topic: -----

#1 hiphop_13  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 37
  • Joined: 04-June 07

object reference error

Posted 24 September 2007 - 02:14 AM

Hi, how can i create an array of session and pass it by a method??
What i have is

TextBox1
TextBox2
Button
AspTable

I want a row to be added contatining Textbox1.Text each time i click the button, for this i need array of session so that i can store the value of Textbox1.Text in every click
Here is my code but it is resulting an object reference error plz check it out.

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml;
using System.Collections;
using System.IO;

public partial class _Default : System.Web.UI.Page
{
	string[] a;
	public void AddNewRow(int count)
	{
		TableRow r;
		TableCell c1,c2,c3,c4,c5;
		for (int i = 0; i <= count; i++)
		{
			 r = new TableRow();
			c1 = new TableCell();
			c1.Controls.Add(new LiteralControl(Session[a[i]].ToString()));//to take each time the TextBox1.Text value
			r.Cells.Add(c1);
		  
			Table1.Rows.Add(r);
		 
		}
	}

	protected void Page_Load(object sender, EventArgs e)
	{
		if (!Page.IsPostBack)
		{
			Session["RowsAdded"] = -1;
			 a = new string[5];//to store the values of TextBox1.Text by giving it to a session
			
	 

		}
	}



	protected void TextBox1_TextChanged(object sender, EventArgs e)
	{
	 
	}
	protected void Button3_Click(object sender, EventArgs e)
	{
	  int s=(int)Session["RowsAdded"]+1;
	  Session["RowsAdded"] = s;
	  a[s] = DropDownList1.Text;//results object reference is not set to an instance of an object
	  Session[a[s]] = a[s];

	  for (int counter = 0; counter <= (int)Session["RowsAdded"]; counter++)
		  AddNewRow(counter);
}

}



Thank you

Is This A Good Question/Topic? 0
  • +

Replies To: object reference error

#2 PsychoCoder  Icon User is offline

  • Google.Sucks.Init(true);
  • member icon

Reputation: 1642
  • View blog
  • Posts: 19,853
  • Joined: 26-July 07

Re: object reference error

Posted 24 September 2007 - 06:21 AM

We need to know where the NullReferenceException is happening
Was This Post Helpful? 0
  • +
  • -

#3 hiphop_13  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 37
  • Joined: 04-June 07

Re: object reference error

Posted 25 September 2007 - 12:46 AM

I have stated where is the error in a comment
start reading the code from the end the first comment u see is the error(object reference not set to an instance of an object)
Thanks
Was This Post Helpful? 0
  • +
  • -

#4 PsychoCoder  Icon User is offline

  • Google.Sucks.Init(true);
  • member icon

Reputation: 1642
  • View blog
  • Posts: 19,853
  • Joined: 26-July 07

Re: object reference error

Posted 26 September 2007 - 10:17 PM

Are you trying:

1) Create an array of session objects
2) Create session values that are arrays?

For a session to hold an array value, then retrieve those values back out, try something like this:

string[] myArrayData = { "One", "Two", "Three" };

Session.Add("myArrayData", myArrayData);

string[] myArrayDataFromSession = (string[])Session["myArrayData"];

string secondItem = myArrayDataFromSession[1];


Was This Post Helpful? 0
  • +
  • -

#5 hiphop_13  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 37
  • Joined: 04-June 07

Re: object reference error

Posted 27 September 2007 - 12:17 AM

Thanks a lot really:)
Thank you
Was This Post Helpful? 0
  • +
  • -

#6 PsychoCoder  Icon User is offline

  • Google.Sucks.Init(true);
  • member icon

Reputation: 1642
  • View blog
  • Posts: 19,853
  • Joined: 26-July 07

Re: object reference error

Posted 27 September 2007 - 04:53 AM

No problem, thats what we're here for :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1