locked
Object reference not set to an instance of an object RRS feed

  • Question

  • I have tried to fill dropdwon control using Array list but get Object reference not set to an instance of an object

    my code is 

    private ArrayList GetDummyData()
        {
            ArrayList arr = new ArrayList();
            arr.Add(new ListItem("Item1", "1"));
            arr.Add(new ListItem("Item2", "2"));
            arr.Add(new ListItem("Item3", "3"));
            arr.Add(new ListItem("Item4", "4"));
            arr.Add(new ListItem("Item5", "5"));
            return arr;


        }

        private void FillDropDownList(DropDownList ddl)
        {
           
                 ArrayList arr = GetDummyData();
            foreach (ListItem item in arr)
            {
                ddl.Items.Add(item);
            }
        }

    BUT  ddl.Items.Add(item); get above mentioned error. for more clarification I have attached printScreen.

    My aim is  click  on button in gridvierw footer add another row in gridview but row should be editable and also some column as dropdwon list .

    first time shoule be look one row in gridview to client


    I having refer

    "http://www.aspsnippets.com/Articles/Adding-Dynamic-Rows-in-GridView-with-DropDownLists-in-ASP.Net.aspx"

    article. But I have some doubt because all value set for dropdwon list didn't come from database table .

    anyone have another idea in that get prefer database value for fill in dropdwon list give me


    Monday, February 23, 2015 12:06 PM

Answers

  • You should be able to see in your debugger what line (or line number in the exception call stack) the exception is thrown on. Looking at your code and the error you are getting the exception is probably thrown because your DropDownList variable ddl is null in your method FillDropDownList. 

    That being said the better place to ask this question is on the ASP.NET Forums, please repost your question there if you still have the problem.


    -Igor

    • Proposed as answer by Kristin Xie Friday, March 6, 2015 3:40 AM
    • Marked as answer by Just Karl Thursday, April 9, 2015 9:28 PM
    Monday, February 23, 2015 12:13 PM

All replies

  • You should be able to see in your debugger what line (or line number in the exception call stack) the exception is thrown on. Looking at your code and the error you are getting the exception is probably thrown because your DropDownList variable ddl is null in your method FillDropDownList. 

    That being said the better place to ask this question is on the ASP.NET Forums, please repost your question there if you still have the problem.


    -Igor

    • Proposed as answer by Kristin Xie Friday, March 6, 2015 3:40 AM
    • Marked as answer by Just Karl Thursday, April 9, 2015 9:28 PM
    Monday, February 23, 2015 12:13 PM
  • DropDownList ddl is not instantiated.

    Where do you call FillDropDownList method and make sure that the ddl paramater you pass is instantiated.

    Monday, February 23, 2015 12:14 PM
  • Hi,

    Typically you passed a null value as a parameter to your "FillDropDownList" method. It looks like you searched specific dropdownlists using the "FindControl" method but it returned null, because you made an error in the name or something like this.

    Can we see how you called "FillDropDownList" please?


    Philippe

    Monday, February 23, 2015 12:17 PM
  • I am beginner in c#. I have idea about sql but I am trying to make application in that application I want  show gridview  default with one row to client after increases row on button click event .

    I have refer below Mentioned Article and trying to two  combine article because At time one row should be contain editable box or selection box (dropdwon list).  first article run good but after combination of two article get error .

    plz help me.. What have gone wrong 

    "http://www.aspsnippets.com/Articles/Add-new-Row-to-GridView-on-Button-Click-in-ASPNet.aspx"

    "http://www.aspsnippets.com/Articles/Adding-Dynamic-Rows-in-GridView-with-DropDownLists-in-ASP.Net.aspx"

    Monday, February 23, 2015 12:39 PM
  • Problem is happening here:

     private void FillDropDownList(DropDownList ddl)
        {
           
                 ArrayList arr = GetDummyData();
            foreach (ListItem item in arr)
            {
                ddl.Items.Add(item);
            }
        }

    Hover over your mouse on that method and you will see references. There you can see where do you call this method. 

    Go to the point where you call this method and you will there see a uninstantiated DropDownList and instantiate it. 

    If you want you can try it as this but I am not sure if it works .

    private void FillDropDownList(DropDownList ddl)
        {
             ddl = new DropDownList();
           
                 ArrayList arr = GetDummyData();
            foreach (ListItem item in arr)
            {
                ddl.Items.Add(item);
            }
        }


    • Edited by Val10 Monday, February 23, 2015 12:47 PM
    Monday, February 23, 2015 12:46 PM
  • I have tried above code but get error to 'dll' show error 'The name dll doesn't exist in current context'

    and I tried to go pointer to method  but show message 'specifie a return value type for method that does not return value'

    I have below paste my code 

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Data;
    using System.Data.SqlClient;
    using System.Configuration;
    using System.Web.Configuration;
    using System.Collections;

               

    protected void Page_Load(object sender, EventArgs e)
        {

            if (!Page.IsPostBack)
            {
                SetInitialRow();
            }


        }

     private ArrayList GetDummyData()
        {
            ArrayList arr = new ArrayList();
            arr.Add(new ListItem("Item1", "1"));
            arr.Add(new ListItem("Item2", "2"));
            arr.Add(new ListItem("Item3", "3"));
            arr.Add(new ListItem("Item4", "4"));
            arr.Add(new ListItem("Item5", "5"));
            return arr;


        }
        private void FillDropDownList(DropDownList ddl)
        {
           // dll = new DropDownList();

            ArrayList arr = GetDummyData();
            foreach (ListItem item in arr)
            {
                ddl.Items.Add(item);
            }
        }


        private void AddNewRowToGrid()
        {
            int rowIndex = 0;

            if (ViewState["CurrentTable"] != null)
            {
                DataTable dtCurrentTable = (DataTable)ViewState["CurrentTable"];
                DataRow drCurrentRow = null;
                if (dtCurrentTable.Rows.Count > 0)
                {
                    for (int i = 1; i <= dtCurrentTable.Rows.Count; i++)
                    {
                        //extract the TextBox values
                        TextBox box1 = (TextBox)grd_Vehical.Rows[rowIndex].Cells[1].FindControl("TextBox1");
                        DropDownList ddl2 = (DropDownList)grd_Vehical.Rows[i].Cells[2].FindControl("DropDownList2");
                        //TextBox box2 = (TextBox)grd_Vehical.Rows[rowIndex].Cells[2].FindControl("TextBox2");
                        //TextBox box3 = (TextBox)grd_Vehical.Rows[rowIndex].Cells[3].FindControl("TextBox3");

                        drCurrentRow = dtCurrentTable.NewRow();
                        drCurrentRow["RowNumber"] = i + 1;

                        dtCurrentTable.Rows[i - 1]["Column1"] = box1.Text;
                        dtCurrentTable.Rows[i]["Column2"] = ddl2.SelectedItem.Text;
                        //dtCurrentTable.Rows[i - 1]["Column3"] = box3.Text;
                   rowIndex++;
                 }
                    dtCurrentTable.Rows.Add(drCurrentRow);
                    ViewState["CurrentTable"] = dtCurrentTable;

                    grd_Vehical.DataSource = dtCurrentTable;
                    grd_Vehical.DataBind();
                }
            }
            else
            {
                Response.Write("ViewState is null");
            }

            //Set Previous Data on Postbacks
            SetPreviousData();
        }

     private void SetPreviousData()
        {
            int rowIndex = 0;
            if (ViewState["CurrentTable"] != null)
            {
                DataTable dt = (DataTable)ViewState["CurrentTable"];
                if (dt.Rows.Count > 0)
                {
                    for (int i = 0; i < dt.Rows.Count; i++)
                    {
                        TextBox box1 = (TextBox)grd_Vehical.Rows[rowIndex].Cells[1].FindControl("TextBox1");
                        DropDownList ddl2 = (DropDownList)grd_Vehical.Rows[rowIndex].Cells[2].FindControl("DropDownList2");

                        //TextBox box3 = (TextBox)Gridview1.Rows[rowIndex].Cells[3].FindControl("TextBox3");

                        box1.Text = dt.Rows[i]["Column1"].ToString();
                        FillDropDownList(ddl2);
                        //box3.Text = dt.Rows[i]["Column3"].ToString();
                        if (i < dt.Rows.Count - 1)
                        {
                            ddl2.ClearSelection();
                            ddl2.Items.FindByText(dt.Rows[i]["Column2"].ToString()).Selected = true;
                        }



                        rowIndex++;

                    }
                }
            }
        }

     protected void GridView1_OnRowCommand1(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName.Equals("EmptyInsert"))

            {
                AddNewRowToGrid();
            
            }
        }

    Monday, February 23, 2015 1:03 PM
  • if (ViewState["CurrentTable"] != null) { DataTable dt = (DataTable)ViewState["CurrentTable"]; if (dt.Rows.Count > 0) {

    DropDownList ddl2; for (int i = 0; i < dt.Rows.Count; i++) {

    ddl2 = new DropDownList(); // add this line TextBox box1 = (TextBox)grd_Vehical.Rows[rowIndex].Cells[1].FindControl("TextBox1"); ddl2 = (DropDownList)grd_Vehical.Rows[rowIndex].Cells[2].FindControl("DropDownList2"); //TextBox box3 = (TextBox)Gridview1.Rows[rowIndex].Cells[3].FindControl("TextBox3"); box1.Text = dt.Rows[i]["Column1"].ToString(); FillDropDownList(ddl2); //box3.Text = dt.Rows[i]["Column3"].ToString(); if (i < dt.Rows.Count - 1) { ddl2.ClearSelection(); ddl2.Items.FindByText(dt.Rows[i]["Column2"].ToString()).Selected = true; } rowIndex++; } } }

    You need to make some alteration in the code above by instantiating the DropDownList.

    But the point is this line is  returning null:

    (DropDownList)grd_Vehical.Rows[rowIndex].Cells[2].FindControl("DropDownList2")

    Monday, February 23, 2015 1:11 PM
  • An arraylist contains objects (therefore was in version 2005 from that the generic list created)

    This can be your code.

    ArrayList arr = GetDummyData(); foreach (Object item in arr) { ddl.Items.Add((ListItem)item);

    }



    Success
    Cor

    Monday, February 23, 2015 1:29 PM
  • hi,

    you should do like this

    private ArrayList GetDummyData()
        {
            ArrayList arr = new ArrayList();
            arr.Add(new ListItem("Item1", "1"));
            arr.Add(new ListItem("Item2", "2"));
            arr.Add(new ListItem("Item3", "3"));
            arr.Add(new ListItem("Item4", "4"));
            arr.Add(new ListItem("Item5", "5"));
            return arr;
    
    
        }
    
        private void FillDropDownList(DropDownList ddl)
        {
           
            arr = GetDummyData();
            foreach (ListItem item in arr)
            {
                ddl.Items.Add(item);
            }
        }

    Monday, February 23, 2015 2:13 PM
  • or also

    var makes = new List<string> {
        "Audi",
        "BMW",
        "Ford",
        "Vauxhall",
        "Volkswagen"
    };
    makes.Sort(); 
    
    DropDownList1.DataSource = makes;
    DropDownList1.DataBind();

    Monday, February 23, 2015 2:15 PM
  • Please post questions related to ASP.NET in the ASP.NET forums (http://forums.asp.net).
    Monday, February 23, 2015 3:24 PM