Thursday, March 29, 2012

Object reference

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 40: dtagrdReviewDisplay.DataBind();Line 41: //dropdownlistLine 42: drpdwnRevRating.Items[0].Selected=true;Line 43: drpdwnProRating.Items[0].Selected=true;Line 44:

Is there anything loaded into the dropDown?

You may want to make your code like this to prevent the error:

if(drpdwnRevRating.Items.Count > 0)

drpdwnRevRating.Items[0].Selected = true;

if(drpdwnProRating.Items.Count > 0)

drpdwnProRating.Items[0].Selected = true;


the rating dropdown has ratings 1 to 10,Autopostback property is set to true.

so when the user selects the rating,the new rating is shown in the label and when the forms reloads i want to make Select one as the item to be diplayed.


How about trying the code I sent just to get the page to load. Then you can see if there is actually anything in your dropdownlist. I tried to recreate your error and the only way I can is if I don't put anything into the dropdownlist.

Let me know what happens when you try those 'if' statements.


i have datagrid ,in datagrid i have one drop down,this dropdown has 1 to 10 as listitems

i want to catch the rating what the user has selected so

.aspx

<asp:DropDownList id="drpdwnRevRating" AutoPostBack="True" Runat="server">
<asp:ListItem Selected="True" Value="Select One">Select One</asp:ListItem>
<asp:ListItem Value="1">1</asp:ListItem>
<asp:ListItem Value="2">2</asp:ListItem>
<asp:ListItem Value="3">3</asp:ListItem>
<asp:ListItem Value="4">4</asp:ListItem>
<asp:ListItem Value="5">5</asp:ListItem>
<asp:ListItem Value="6">6</asp:ListItem>
<asp:ListItem Value="7">7</asp:ListItem>
<asp:ListItem Value="8">8</asp:ListItem>
<asp:ListItem Value="9">9</asp:ListItem>
<asp:ListItem Value="10">10</asp:ListItem>
</asp:DropDownList>
<br>
Rate the Product:
<asp:DropDownList>
<asp:ListItem Value="1">1</asp:ListItem>
</asp:DropDownList>
</ItemTemplate>
</asp:TemplateColumn>
</Columns>

.cs

privatevoid dtagrdReviewDisplay_SelectedIndexChanged(object sender, System.EventArgs e)

{

string strRevRating;

strRevRating=Request["drpdwnRevRating"];

Response.Write(strRevRating);

}

Output notthing is coming in strRevRating and i used break point here,the comilation is not coming into this eventhandler itself.


is their is any problem in this event handler of dropdownlist control what i wrote

protected System.Web.UI.WebControls.DropDownList drpdwnRevRating;

this.drpdwnRevRating.SelectedIndexChanged += new System.EventHandler(this.drpdwnRevRating_SelectedIndexChanged);


private void drpdwnRevRating_SelectedIndexChanged(object sender, System.EventArgs e)
{
string strRevRating;
strRevRating=Request["drpdwnRevRating"];
Response.Write(strRevRating);

}

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 65: { Line 66: this.ImageButton1.Click += new System.Web.UI.ImageClickEventHandler(this.ImageButton1_Click);Line 67: this.drpdwnRevRating.SelectedIndexChanged += new System.EventHandler(this.drpdwnRevRating_SelectedIndexChanged);Line 68: this.Load += new System.EventHandler(this.Page_Load);Line 69:


I think the issues you are having is because the DropDownList does not belong to the page, it belongs to the datagrid. The null exception is because there is no "this.drpdwnRevRating".

To work with the dropdownlists, you must find the control on the line of the datagrid and cast it to a DropDownList. I assume you are using ASP.Net 1.1 which I don't have installed on this computer or I would type up a quick sample, but you can refer to this article.http://www.codeproject.com/aspnet/DataGridDropDownList.asp

Pay attention to how they are doing:

DropDownList dl = (DropDownList)(dgi.Cells[2].Controls[1]);

You can also do:

DropDownList dl = (DropDownList)(dgi.Rows[e.SelectedIndex].FindControls("drpdwnProRating");

//I think that is the right syntax but I don't have the luxury of intellisense right now.

Then you can work with 'dl' instead of 'drpdwnProRating'

I hope that helps to move your forward a bit.

0 comments:

Post a Comment