The code is fine but got the error below when running the application.
Server Error in '/QuestLocal' Application.
--------------------------------------------------------------------------------
Object reference not set to an instance of an object.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.
Source Error:
Line 19: {
Line 20: RadioButtonList rbl = ((RadioButtonList)(gvr.FindControl("radioButtonListID")));
Line 21: for (int j = 0; (j <= rbl.Items.Count); j++)
Line 22: {
Line 23: if ((rbl.Items[j].Value == "5"))
Source File: c:\inetpub\wwwroot\QuestLocal\Q1.aspx.cs Line: 21
Stack Trace:
Yes, GridView is bound and works fine without the Cookies condition. I am using Edit DefaultMode. Below is my full code which returns the error:
Object reference not set to an instance of an object (Line 21). Please assist.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button3_Click(object sender, EventArgs e)
{
foreach (DetailsViewRow gvr in DetailsView1.Rows)
{
if ((gvr.RowType == DataControlRowType.DataRow))
{
RadioButtonList rbl = ((RadioButtonList)(gvr.FindControl("radioButtonListI")));
for (int j = 0; (j <= rbl.Items.Count); j++)
{
if ((rbl.Items[j].Value == "5"))
{
if (Request.Cookies["Q1"] != null)
{
Response.Cookies["Q1"].Value = Label2.Text.ToString();
Response.Redirect("../QuestLocal/Q2.aspx");
}
else
{
Response.Cookies["Q1"].Value = Label2.Text.ToString();
Response.Redirect("../QuestLocal/Q3.aspx");
}
}
}
}
}
}
You got it. No more errors but the "else" part of the condition does not execute. It always executes the first part irrespective of the value. See the code below. Any more help?
AmalO.Abdull...
Contributor
3116 Points
764 Posts
Re: Condition in Cookie
Apr 25, 2012 05:50 AM|LINK
sorry iam coding in vb so as peter said
asante_za
Member
16 Points
113 Posts
Re: Condition in Cookie
Apr 25, 2012 06:11 AM|LINK
Hi
The code is fine but got the error below when running the application.
Server Error in '/QuestLocal' Application. -------------------------------------------------------------------------------- Object reference not set to an instance of an object. Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. Exception Details: System.NullReferenceException: Object reference not set to an instance of an object. Source Error: Line 19: { Line 20: RadioButtonList rbl = ((RadioButtonList)(gvr.FindControl("radioButtonListID"))); Line 21: for (int j = 0; (j <= rbl.Items.Count); j++) Line 22: { Line 23: if ((rbl.Items[j].Value == "5")) Source File: c:\inetpub\wwwroot\QuestLocal\Q1.aspx.cs Line: 21 Stack Trace:AmalO.Abdull...
Contributor
3116 Points
764 Posts
Re: Condition in Cookie
Apr 25, 2012 06:19 AM|LINK
you need to put your radiobutton list id , please you need to look into code and change it based in your id's
Peter pi - M...
Star
12871 Points
1786 Posts
Re: Condition in Cookie
Apr 25, 2012 06:22 AM|LINK
You need to ensure that the RadioButtonList radioButtonListID is in the GridView and GridView has been bound to datasource.
If you have any feedback about my replies, please contact msdnmg@microsoft.com
Microsoft One Code Framework
asante_za
Member
16 Points
113 Posts
Re: Condition in Cookie
Apr 26, 2012 12:12 PM|LINK
Yes, GridView is bound and works fine without the Cookies condition. I am using Edit DefaultMode. Below is my full code which returns the error: Object reference not set to an instance of an object (Line 21). Please assist.
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; public partial class _Default : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { } protected void Button3_Click(object sender, EventArgs e) { foreach (DetailsViewRow gvr in DetailsView1.Rows) { if ((gvr.RowType == DataControlRowType.DataRow)) { RadioButtonList rbl = ((RadioButtonList)(gvr.FindControl("radioButtonListI"))); for (int j = 0; (j <= rbl.Items.Count); j++) { if ((rbl.Items[j].Value == "5")) { if (Request.Cookies["Q1"] != null) { Response.Cookies["Q1"].Value = Label2.Text.ToString(); Response.Redirect("../QuestLocal/Q2.aspx"); } else { Response.Cookies["Q1"].Value = Label2.Text.ToString(); Response.Redirect("../QuestLocal/Q3.aspx"); } } } } } }AmalO.Abdull...
Contributor
3116 Points
764 Posts
Re: Condition in Cookie
Apr 26, 2012 12:16 PM|LINK
can i know what is you radiobutton id in aspx page? if you can post your deisgn
asante_za
Member
16 Points
113 Posts
Re: Condition in Cookie
Apr 26, 2012 12:22 PM|LINK
Please find attached.
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Q1.aspx.cs" Inherits="_Default" Debug="true"%> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title></title> <style type="text/css"> .style1 { width: 100%; } </style> </head> <body> <form id="form1" runat="server"> <div> <table class="style1"> <tr valign="top"> <td> </td> <td> <table class="style1"> <tr> <td> </td> </tr> <tr> <td> <asp:DetailsView ID="DetailsView1" runat="server" AutoGenerateRows="False" BackColor="#CCCCCC" BorderColor="#999999" BorderStyle="Solid" BorderWidth="3px" CellPadding="4" CellSpacing="2" DataKeyNames="ID" DataSourceID="AccessDataSource1" DefaultMode="Edit" ForeColor="Black" Height="50px"> <FooterStyle BackColor="#CCCCCC" /> <RowStyle BackColor="White" /> <PagerStyle BackColor="#CCCCCC" ForeColor="Black" HorizontalAlign="Left" /> <Fields> <asp:BoundField DataField="ID" HeaderText="ID" InsertVisible="False" ReadOnly="True" SortExpression="ID" /> <asp:TemplateField HeaderText="Q1. The training was effective and I feel that the objectives were covered completely" SortExpression="Quest1"> <EditItemTemplate> <asp:RadioButtonList ID="RadioButtonList1" runat="server" DataSourceID="AccessDataSource2" DataTextField="Description" DataValueField="RadioValue" SelectedValue='<%# Bind("Quest1") %>'> </asp:RadioButtonList> </EditItemTemplate> <InsertItemTemplate> <asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("Quest1") %>'></asp:TextBox> </InsertItemTemplate> <ItemTemplate> <asp:Label ID="Label1" runat="server" Text='<%# Bind("Quest1") %>'></asp:Label> </ItemTemplate> </asp:TemplateField> <asp:TemplateField ShowHeader="False"> <EditItemTemplate> <asp:Button ID="Button1" runat="server" CausesValidation="True" CommandName="Update" Text="Update" onclick="Button1_Click" /> <asp:Button ID="Button3" runat="server" Text="Continue" onclick="Button3_Click" /> <asp:Button ID="Button2" runat="server" CausesValidation="False" CommandName="Cancel" Text="Cancel" /> </EditItemTemplate> <ItemTemplate> <asp:Button ID="Button1" runat="server" CausesValidation="False" CommandName="Edit" Text="Edit" /> </ItemTemplate> </asp:TemplateField> </Fields> <HeaderStyle BackColor="Black" Font-Bold="True" ForeColor="White" /> <EditRowStyle BackColor="#000099" Font-Bold="True" ForeColor="White" /> </asp:DetailsView> </td> </tr> <tr> <td> <asp:Label ID="Label2" runat="server" Text="Label"></asp:Label> </td> </tr> <tr> <td> <asp:AccessDataSource ID="AccessDataSource1" runat="server" DataFile="E:\DocE\ConnDB/TrainingWeb.mdb" DeleteCommand="DELETE FROM [TrainingEvaluation] WHERE [ID] = ?" InsertCommand="INSERT INTO [TrainingEvaluation] ([ID], [Quest1]) VALUES (?, ?)" SelectCommand="SELECT [ID], [Quest1] FROM [TrainingEvaluation] WHERE ([UserName] = ?)" UpdateCommand="UPDATE [TrainingEvaluation] SET [Quest1] = ? WHERE [ID] = ?"> <SelectParameters> <asp:CookieParameter CookieName="UserName" Name="UserName" Type="String" /> </SelectParameters> <DeleteParameters> <asp:Parameter Name="ID" Type="Int32" /> </DeleteParameters> <UpdateParameters> <asp:Parameter Name="Quest1" Type="Double" /> <asp:Parameter Name="ID" Type="Int32" /> </UpdateParameters> <InsertParameters> <asp:Parameter Name="ID" Type="Int32" /> <asp:Parameter Name="Quest1" Type="Double" /> </InsertParameters> </asp:AccessDataSource> <asp:AccessDataSource ID="AccessDataSource2" runat="server" DataFile="E:\DocE\ConnDB/TrainingWeb.mdb" SelectCommand="SELECT [Description], [RadioValue] FROM [RadioButtons2]"> </asp:AccessDataSource> </td> </tr> </table> </td> </tr> </table> </div> </form> </body> </html>AmalO.Abdull...
Contributor
3116 Points
764 Posts
Re: Condition in Cookie
Apr 26, 2012 12:58 PM|LINK
I don't know if iam right or not but i have noticed that in your code behind you id is "radioButtonListI" and in aspx page is "radioButtonList1".
asante_za
Member
16 Points
113 Posts
Re: Condition in Cookie
Apr 26, 2012 04:02 PM|LINK
You got it. No more errors but the "else" part of the condition does not execute. It always executes the first part irrespective of the value. See the code below. Any more help?
protected void Button3_Click(object sender, EventArgs e) { foreach (DetailsViewRow gvr in DetailsView1.Rows) { if ((gvr.RowType == DataControlRowType.DataRow)) { RadioButtonList rbl = ((RadioButtonList)(gvr.FindControl("radioButtonList1"))); for (int j = 0; (j <= rbl.Items.Count); j++) { if ((rbl.Items[j].Value == "5")) { if (Request.Cookies["Q1"] != null) { Response.Cookies["Q1"].Value = Label2.Text.ToString(); Response.Redirect("../QuestLocal/Q2.aspx"); } else { Response.Cookies["Q1"].Value = Label2.Text.ToString(); Response.Redirect("../QuestLocal/Q3.aspx"); } } } } } }AmalO.Abdull...
Contributor
3116 Points
764 Posts
Re: Condition in Cookie
Apr 29, 2012 06:40 AM|LINK
you reqirement says that if selected value is 5 go to questoin2 else go to q3 but in code is not doing that!
change your code to the following
if ((rbl.Items[j].Value == "5")) { if (Request.Cookies["Q1"] != null) { Response.Cookies["Q1"].Value = Label2.Text.ToString(); Response.Redirect("../QuestLocal/Q2.aspx"); } } else { if(Request.Cookies["Q1"] != null) { Response.Cookies["Q1"].Value = Label2.Text.ToString(); Response.Redirect("../QuestLocal/Q3.aspx"); } }