Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > ASP.NET and ASP > ASP.NET 3.5 > ASP.NET 3.5 Professionals
Password Reminder
Register
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
ASP.NET 3.5 Professionals If you are an experienced ASP.NET programmer, this is the forum for your 3.5 questions. Please also see the Visual Web Developer 2008 forum.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ASP.NET 3.5 Professionals section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
 
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old August 22nd, 2009, 09:28 PM
Authorized User
 
Join Date: Jun 2009
Posts: 18
Thanks: 1
Thanked 0 Times in 0 Posts
Default Object reference not set to an instance of an object.

I try to bind the dropdownlist in the GridView,but always get the result said:Object reference not set to an instance of an object.
I set the breakpoint to follow the code,it showed I did get the data from the database,but when I follow the next code,the error showed.
My code:
Code:
cs code:
DropDownList ddlUpdateCategory = (DropDownList)this.GridView1.Rows[Convert.ToInt32(e.CommandArgument)].Cells[1].FindControl("ddlUpdateCategory");
            using (NorthwindEntities ctx = new NorthwindEntities())
            {
                ddlUpdateCategory.DataTextField = "CategoryName";
                ddlUpdateCategory.DataValueField = "CategoryID";
                ddlUpdateCategory.DataSource = ctx.Categories;
                ddlUpdateCategory.DataBind();
            }


aspx code:
<asp:TemplateField HeaderText="CategoryName">
<EditItemTemplate>
<asp:DropDownList ID="ddlUpdateCategory" runat="server" DataTextField="CategoryName" DataValueField="CategoryID">
</asp:DropDownList>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Bind("CategoryName") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>

Thanks in advance!
  #2 (permalink)  
Old August 22nd, 2009, 10:12 PM
Lee Dumond's Avatar
Wrox Author
Points: 4,942, Level: 29
Points: 4,942, Level: 29 Points: 4,942, Level: 29 Points: 4,942, Level: 29
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jan 2008
Location: Decatur, IL, USA.
Posts: 923
Thanks: 12
Thanked 166 Times in 162 Posts
Default

It's probably not finding the Dropdownlist.

First, what event is this code attached to? (I assume it is the RowDataBound event of the GridView)

And second, which cell in the row is the Dropdown list supposed to be in?
__________________
Visit my blog at http://leedumond.com
Follow me on Twitter: http://twitter.com/LeeDumond

Code:
if (this.PostHelpedYou)
{
   ClickThanksButton(); 
}
  #3 (permalink)  
Old August 22nd, 2009, 10:31 PM
Authorized User
 
Join Date: Jun 2009
Posts: 18
Thanks: 1
Thanked 0 Times in 0 Posts
Default

First,thanks for your reply in time.
Yes,you are right.
Code:
  protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName == "upload")
        {
            GridViewRow gvr = ((GridViewRow)(((Button)(e.CommandSource)).Parent.Parent));//此得出的值是表示那行被选中的索引值
            int Key = Convert.ToInt32(GridView1.DataKeys[gvr.RowIndex].Value);
            FileUpload fu = (FileUpload)this.GridView1.Rows[gvr.RowIndex].Cells[1].FindControl("fuUpload");
            int updateImagelength = fu.PostedFile.ContentLength;
            byte[] updateimage = new byte[updateImagelength];
            fu.PostedFile.InputStream.Read(updateimage, 0, updateImagelength);
            using (NorthwindEntities ctx = new NorthwindEntities())
            {
                var category = ctx.Categories.First(p => p.CategoryID == Key);
                category.Picture = updateimage;
                ctx.SaveChanges();
            }
        }
        if (e.CommandName == "Edit")
        {
            DropDownList ddlUpdateCategory = (DropDownList)this.GridView1.Rows[Convert.ToInt32(e.CommandArgument)].Cells[1].FindControl("ddlUpdateCategory");
            using (NorthwindEntities ctx = new NorthwindEntities())
            {
                ddlUpdateCategory.DataTextField = "CategoryName";
                ddlUpdateCategory.DataValueField = "CategoryID";
                ddlUpdateCategory.DataSource = ctx.Categories;
                ddlUpdateCategory.DataBind();
            }
        }
    }
And I need to click the Edit button in the Gridview,then shows the binded dropdownlist in the second column.
  #4 (permalink)  
Old August 23rd, 2009, 02:55 AM
Lee Dumond's Avatar
Wrox Author
Points: 4,942, Level: 29
Points: 4,942, Level: 29 Points: 4,942, Level: 29 Points: 4,942, Level: 29
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jan 2008
Location: Decatur, IL, USA.
Posts: 923
Thanks: 12
Thanked 166 Times in 162 Posts
Default

When a GridView RowCommand event is raised with a CommandName == "Edit", this automatically in turn raises the RowEditing event. The RowEditing event occurs before the GridView control enters edit mode.

Because the ddlUpdateCategories control is contained in the row's EditItemTemplate, it is not rendered until the GridView enters edit mode. When your event handler is invoked, the GridView has not yet entered edit mode, and therefore the ddlUpdateCategories control does not yet exist.

I suggest you find a different way to bind the ddlUpdateCategories to your list of categories. You can do that declaratively directly inside the EditItemTemplate.

If you want to do it at runtime, you can handle the RowDataBound event, which will fire for each row, then use the RowType property to determine if the current row is in edit mode. If it is, you should be able to get a reference to the ddlUpdateCategories at that time.
__________________
Visit my blog at http://leedumond.com
Follow me on Twitter: http://twitter.com/LeeDumond

Code:
if (this.PostHelpedYou)
{
   ClickThanksButton(); 
}
  #5 (permalink)  
Old August 23rd, 2009, 05:24 AM
Authorized User
 
Join Date: Jun 2009
Posts: 18
Thanks: 1
Thanked 0 Times in 0 Posts
Thumbs up Thanks a lot

Hi,
I really appreciate your help.It works like you said.Thaks,anyway!
  #6 (permalink)  
Old August 23rd, 2009, 02:07 PM
Lee Dumond's Avatar
Wrox Author
Points: 4,942, Level: 29
Points: 4,942, Level: 29 Points: 4,942, Level: 29 Points: 4,942, Level: 29
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jan 2008
Location: Decatur, IL, USA.
Posts: 923
Thanks: 12
Thanked 166 Times in 162 Posts
Default

Quote:
Originally Posted by Michael YoeH View Post
Hi,
I really appreciate your help.It works like you said.Thaks,anyway!
You can use the Thanks button at the bottom of my post to thank me.
__________________
Visit my blog at http://leedumond.com
Follow me on Twitter: http://twitter.com/LeeDumond

Code:
if (this.PostHelpedYou)
{
   ClickThanksButton(); 
}
The Following User Says Thank You to Lee Dumond For This Useful Post:
Michael YoeH (August 23rd, 2009)
 


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Object reference not set to an instance of an object. phuc2583 ASP.NET 3.5 Professionals 5 August 14th, 2009 10:56 AM
Chap 13 pg448: Object reference not set to an instance of an object tomche BOOK: Beginning ASP.NET 3.5 : in C# and VB BOOK ISBN: 978-0-470-18759-3 15 August 8th, 2009 06:14 AM
Object reference not set to an instance of object iceman90289 ASP.NET 3.5 Basics 1 March 1st, 2009 05:13 AM
Object Reference not set to an instance of object srinivas_chakka ASP.NET 1.0 and 1.1 Professional 0 February 8th, 2006 11:56 AM



All times are GMT -4. The time now is 12:49 PM.


Powered by vBulletin®
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.