Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# ASP.NET
its giving me error at runtime and i cant understand error please help me ?
please advice
 

{
    protected void Page_Load(object sender, EventArgs e)
    {
 
    }
    protected void DropDownList3_SelectedIndexChanged(object sender, EventArgs e)
    {
 
    }
    protected void DropDownList4_SelectedIndexChanged(object sender, EventArgs e)
    {
        int month;
        month= DropDownList4.SelectedIndex  ;
        int year=Convert.ToInt32 (DropDownList3.Text)  ;
        SignUp obj=new SignUp();
        int p = 31;
        DropDownList5.Items.Add("0");
        switch (month)
        {
            case 0:
                obj.days( ref p);
                break;
 

 

    }
 
    public  void days(ref int day)
    {
 
        for (int i = 1; i <=day; i++)
        {
 
            DropDownList5.Items.Add(i.ToString() );
 
        }
 
    }
}
Error :
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 95: {
Line 96:
Line 97: DropDownList5.Items.Add(i.ToString() );
Line 98:
Line 99: }
Posted 25-Nov-12 20:49pm
Edited 25-Nov-12 20:59pm
v3
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

Hi Chandrakant,
 
Write your code as below.
 
protected void DropDownList4_SelectedIndexChanged(object sender, EventArgs e)
{
   int month;
   month= DropDownList4.SelectedIndex  ;
   int year=Convert.ToInt32 (DropDownList3.SelectedItem.Text)  ;
   SignUp obj=new SignUp();
   int p = 31;
   ListItem li = new ListItem("0","0");
   DropDownList5.Items.Add(li);
   switch (month)
   {
      case 0:
           obj.days( ref p);
           break;
    }
 
public  void days(ref int day)
{
   for (int i = 1; i <=day; i++)
   { 
       ListItem li = new ListItem(i.ToString(),i.ToString());
       DropDownList5.Items.Add(li); 
   } 
}
  Permalink  
v2
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

The line that is throwing the error:
DropDownList5.Items.Add(i.ToString() );
Has only two parts that use a method:
i.ToString()
And
DropDownList5.Items.Add(...);
The first is using an integer, which is not a nullable type, so it can't be that.
The second is using a collection of an object - if the object exists, then it will always have an Items Collection (because it is constructed as part of the DropDownList constructor) so the only nullable object is DropDownList5
 
Look at your other code - you need to work out how you get into the days method withput a value in DropDownList5 - where do you set it's value? What do you set it to? What are you doing when you call days?
 
Use the debugger - examine your stack trace, and look at the variables involved.
We can't solve this - you have the information we don't!
  Permalink  

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



Advertise | Privacy | Mobile
Web03 | 2.8.140709.1 | Last Updated 26 Nov 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid