none
Populating a DataGrid with Stored Procedure RRS feed

  • Question

  • I'm trying to populate a DropDownList that's within a GridView using LINQ to SQL Classes but am getting an 'Object reference not set to an instance of an object' error instead.

    The GridView is populated by...

    public partial class AcctRec : System.Web.UI.Page
    {    protected System.Web.UI.WebControls.DropDownList DropDownList1;
        protected void Page_Load(object sender, EventArgs e)
        {       if (!Page.IsPostBack)
                DataBindGrid();
        }
    
    private void DataBindGrid()
    {    ActRecLiveDataContext db = new ActRecLiveDataContext();        
        var collection = from t in db.ARPayLives
                            orderby t.AR_ApplyDate descending
                            select t;
        this.GridView1.DataSource = collection;
        this.GridView1.DataBind();
    }
    


    The GridView populates fine. The DropDownList wants to be populated by a Stored Procedure which is declared in the ActRecLiveDataContext(); as a System.Data.Linq.DataContext.
     
    So the code below returns this error at line... 'this.DropDownList1.DataSource = CusCodes;'
    Object reference not set to an instance of an object.
    Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.

    ActRecLiveDataContext db = new ActRecLiveDataContext();
    var CusCodes = from c in db.uspSELECT_CustomerCodes()
                select c.Business_Settle_Code;    
    this.DropDownList1.DataSource = CusCodes;
    this.DropDownList1.DataBind();
    

    In the *.aspx file...
    <FooterTemplate>
            <edititemtemplate>        
                <asp:DropDownList ID="DropDownList1" runat="server" />          
            </edititemtemplate>
    </FooterTemplate>
    

    So obviously the same syntax to populate a GridView doesn't work for a DataList
    I'm using VS2010
    thanks
    • Edited by JimP10 Thursday, October 28, 2010 5:43 PM formatting
    Thursday, October 28, 2010 5:40 PM

Answers

  • Hello JimP10,

    I suspect you need to find the dropdownlist control in the gridview first and then bind data to it. Please take a look at this similar thread for an example: http://forums.asp.net/t/1185210.aspx

    By the way, I noticed that you defined a DropDownList control at the very beginning:

    public partial class AcctRec : System.Web.UI.Page
    {    protected System.Web.UI.WebControls.DropDownList DropDownList1;

    If you actually want to bind data to the control you defined and then add it to the gridview control, I think there are much more coding, but this is out of the scope of this forums topic.

    Best regards


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    If you have any feedback, please tell us.
    Welcome to the All-In-One Code Framework!
    • Marked as answer by liurong luo Thursday, November 4, 2010 9:29 AM
    Friday, October 29, 2010 3:15 AM

All replies

  • Hi,

    Take a look at this link:

    http://weblogs.asp.net/scottgu/archive/2007/08/16/linq-to-sql-part-6-retrieving-data-using-stored-procedures.aspx

    Hope this can help you,

    JAReyes.


    Please remember to Vote & "Mark As Answer" if this post is helpful to you.
    Por favor, recuerda Votar y "Marcar como respuesta" si la solucion de esta pregunta te ha sido útil.
    Thursday, October 28, 2010 6:04 PM
  • Hello JimP10,

    I suspect you need to find the dropdownlist control in the gridview first and then bind data to it. Please take a look at this similar thread for an example: http://forums.asp.net/t/1185210.aspx

    By the way, I noticed that you defined a DropDownList control at the very beginning:

    public partial class AcctRec : System.Web.UI.Page
    {    protected System.Web.UI.WebControls.DropDownList DropDownList1;

    If you actually want to bind data to the control you defined and then add it to the gridview control, I think there are much more coding, but this is out of the scope of this forums topic.

    Best regards


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    If you have any feedback, please tell us.
    Welcome to the All-In-One Code Framework!
    • Marked as answer by liurong luo Thursday, November 4, 2010 9:29 AM
    Friday, October 29, 2010 3:15 AM