ok, i have the following class in my code behind file. it’s a very simple page, just takes the TestIssue object, loads the entry into a text box for editing the entry, and then when save is clicked it calls the function which updates the TestIssue in the database.
here’s the code.
public partial class Issues_Edit : System.Web.UI.Page
{
protected string filter_test_director;
protected TestIssue myIssue;
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
string id = Request.QueryString["id"];
id = "1358";
myIssue = new TestIssue(Convert.ToInt32(id));
issue_date.Text = myIssue.Entry_Date.ToString();
issue_author.Text = myIssue.Author.Full_Name_RFL;
issue_text.Text = myIssue.Entry.Replace("<br>", "\n");
issue_text.Height = 150;
issue_text.Width = 400;
}
}
protected void SaveButton_Click(object sender, EventArgs e)
{
myIssue.Entry = issue_text.Text;
int update = TestIssueDB.UpdateIssue(myIssue);
if (update == 1)
{
//Response.Redirect("program.aspx?p=" + myIssue.Program_ID);
}
else
{
top_message.Text = "Error Updating Issue Text";
}
}
}
Right now the problem is that when the Save function is called, it errors out with a nullreferenceexception on the myIssue object. I’m not really sure why this is, because it’s the exact same way i am handling other objects on some other pages, with the object declared above the page_load, initialized within the page_load, and then modified in other functions.
any idea why the myIssue object does not exist in that function?
You never initialize that variable when the button is clicked!
Each request to the page creates a new instance of the page. In general, nothing you did during a previous request has any effect on the current request. In particular, you set the
myIssuefield on the first request, but on the second request,IsPostBackis true, so your initialization code is not executed.