Sign In| Join
Get Help:Ask a Question in our Forums|Report a Bug|More Help Resources
Last post May 25, 2012 11:44 AM by hoopslife
Member
631 Points
629 Posts
May 24, 2012 11:39 AM|LINK
I have a default ASP.NET 4.0 Login Form which is auto-generated with an empty ASP.NET 4 application.
On the Login Form I have added two DropDown Controls and I pass the selected item text and values in session variables.
When the Login button is clicked, these session variables are created and page is supposed to redirected to another page.
Below is the code of the Login Button Click event:
protected void LoginButton_Click(object sender, EventArgs e) { reuse reuseableMethodsClass = new reuse(); VerifyUser verifyUser = new VerifyUser(); DropDownList ddlFy = LoginUser.FindControl("ddlFyYear") as DropDownList; DropDownList ddlOffice = LoginUser.FindControl("ddlOffice") as DropDownList; verifyUser.EnteredUserName = LoginUser.UserName; verifyUser.EnteredPassword = LoginUser.Password; bool verified = verifyUser.GetLoginStatus(); if (verified) { Session.Add("ASP.NET_SessionId", Session.SessionID.ToString()); Session.Add("FinancialYear", ddlFy.SelectedItem.Text); Session.Add("LoggedOfficeName", ddlOffice.SelectedItem.Text); Session.Add("LoggedOfficeID", ddlOffice.SelectedItem.Value.ToString()); Response.Redirect("~/Views/ListView.aspx"); } else reuseableMethodsClass.CustomClientMessage("Failed", this.Page); }
The problem is when I click the Login button, an error is displayed in the lines where I add DropDown values to the Session.
Error is: Object reference not set to an instance of an object.
Error is displayed in the three lines:
Session.Add("FinancialYear", ddlFy.SelectedItem.Text); Session.Add("LoggedOfficeName", ddlOffice.SelectedItem.Text); Session.Add("LoggedOfficeID", ddlOffice.SelectedItem.Value.ToString());
I think when I select option from the DropDown, the new value is not retained after postback.
Contributor
2477 Points
483 Posts
May 24, 2012 11:53 AM|LINK
Do you have viewstate turned on for the controls?
May 24, 2012 11:55 AM|LINK
They were ON by default. I turned them OFF but the problem is still there.
I never cared about turning them OFF before.
May 24, 2012 01:03 PM|LINK
Well, you deffinetly need them on to have the values postback. Have you tried stepping through your code to see which specific control isn't sending the value over? Or is it all 3?
May 25, 2012 10:57 AM|LINK
All three. I set the breakpoint.
All-Star
20155 Points
3328 Posts
May 25, 2012 11:04 AM|LINK
Hi,
Before adding the DropDownList value to Session, you need to check whether the DropDownList is null or not!
if(ddlFy != null) { Session.Add("FinancialYear", ddlFy.SelectedItem.Text); }
Hope it helps u...
May 25, 2012 11:44 AM|LINK
Can you post the html? Make sure you have runat="server" for each controller.
rpk2006
Member
631 Points
629 Posts
"Object reference not set" error when Login button clicked
May 24, 2012 11:39 AM|LINK
I have a default ASP.NET 4.0 Login Form which is auto-generated with an empty ASP.NET 4 application.
On the Login Form I have added two DropDown Controls and I pass the selected item text and values in session variables.
When the Login button is clicked, these session variables are created and page is supposed to redirected to another page.
Below is the code of the Login Button Click event:
protected void LoginButton_Click(object sender, EventArgs e) { reuse reuseableMethodsClass = new reuse(); VerifyUser verifyUser = new VerifyUser(); DropDownList ddlFy = LoginUser.FindControl("ddlFyYear") as DropDownList; DropDownList ddlOffice = LoginUser.FindControl("ddlOffice") as DropDownList; verifyUser.EnteredUserName = LoginUser.UserName; verifyUser.EnteredPassword = LoginUser.Password; bool verified = verifyUser.GetLoginStatus(); if (verified) { Session.Add("ASP.NET_SessionId", Session.SessionID.ToString()); Session.Add("FinancialYear", ddlFy.SelectedItem.Text); Session.Add("LoggedOfficeName", ddlOffice.SelectedItem.Text); Session.Add("LoggedOfficeID", ddlOffice.SelectedItem.Value.ToString()); Response.Redirect("~/Views/ListView.aspx"); } else reuseableMethodsClass.CustomClientMessage("Failed", this.Page); }The problem is when I click the Login button, an error is displayed in the lines where I add DropDown values to the Session.
Error is: Object reference not set to an instance of an object.
Error is displayed in the three lines:
Session.Add("FinancialYear", ddlFy.SelectedItem.Text);Session.Add("LoggedOfficeName", ddlOffice.SelectedItem.Text);
Session.Add("LoggedOfficeID", ddlOffice.SelectedItem.Value.ToString());
I think when I select option from the DropDown, the new value is not retained after postback.
hoopslife
Contributor
2477 Points
483 Posts
Re: "Object reference not set" error when Login button clicked
May 24, 2012 11:53 AM|LINK
Do you have viewstate turned on for the controls?
Blog
rpk2006
Member
631 Points
629 Posts
Re: "Object reference not set" error when Login button clicked
May 24, 2012 11:55 AM|LINK
They were ON by default. I turned them OFF but the problem is still there.
I never cared about turning them OFF before.
hoopslife
Contributor
2477 Points
483 Posts
Re: "Object reference not set" error when Login button clicked
May 24, 2012 01:03 PM|LINK
Well, you deffinetly need them on to have the values postback. Have you tried stepping through your code to see which specific control isn't sending the value over? Or is it all 3?
Blog
rpk2006
Member
631 Points
629 Posts
Re: "Object reference not set" error when Login button clicked
May 25, 2012 10:57 AM|LINK
All three. I set the breakpoint.
roopeshreddy
All-Star
20155 Points
3328 Posts
Re: "Object reference not set" error when Login button clicked
May 25, 2012 11:04 AM|LINK
Hi,
Before adding the DropDownList value to Session, you need to check whether the DropDownList is null or not!
if(ddlFy != null) { Session.Add("FinancialYear", ddlFy.SelectedItem.Text); }Hope it helps u...
Roopesh Reddy C
Roopesh's Space
hoopslife
Contributor
2477 Points
483 Posts
Re: "Object reference not set" error when Login button clicked
May 25, 2012 11:44 AM|LINK
Can you post the html? Make sure you have runat="server" for each controller.
Blog