Welcome to Dream.In.Code
Getting C# Help is Easy!

Join 136,552 C# Programmers for FREE! Get instant access to thousands of C# experts, tutorials, code snippets, and more! There are 1,900 people online right now. Registration is fast and FREE... Join Now!




object reference not set an instance of object

 
Reply to this topicStart new topic

object reference not set an instance of object, edit column on grid view

Goober
17 Aug, 2008 - 07:26 PM
Post #1

New D.I.C Head
*

Joined: 9 Jul, 2008
Posts: 7

I'm trying to make a grid view which allow user to key in data on the column, once they click the edit button inside grid view. But now when i debug, once i click edit button , the system kept run on the GridView1_RowDataBound() function after finish run the edit command function.

CODE


        protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            Admin admin = new Admin();
            admin.AdminID = Convert.ToInt32((string)Session["adminID"]);
            admin.AdminName = (string)Session["username"];

            Button btn = e.Row.FindControl("btnEdit") as Button;
          
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                btn.Attributes.Add("onClick", "return confirm('Are you sure you wish to edit this item?');");
                TextBox txtBox1 = (TextBox)e.Row.FindControl("txtlinkdetail");
                txtBox1.Enabled = false;
            }

CODE

protected void GridView1_RowEditing(object sender, GridViewCommandEventArgs e)
        {
            try
            {
              if (e.CommandName == "Edit")
                {
                   // GridView1.EditIndex = e.NewEditIndex;
                    GridViewRow row = GridView1.Rows[Convert.ToInt32(e.CommandArgument)];
                    GridView1.DataBind();    
                }
}



Then it return an error as below:IPB Image

Can tell me how to solve the problem? Thx

User is offlineProfile CardPM
+Quote Post

kikz
RE: Object Reference Not Set An Instance Of Object
17 Aug, 2008 - 07:34 PM
Post #2

New D.I.C Head
*

Joined: 21 Jul, 2008
Posts: 32



Thanked: 1 times
My Contributions

When you ues "x as Type", C# tries to cast x into an instance of type Type. If the cast fails (throws an exception) x is null.

Your problem is that btnEdit cannot be found.
CODE

Button btn = e.Row.FindControl("btnEdit") as Button;

The above fails, so btn == null.

Perform a check for null before trying to access btn.
User is offlineProfile CardPM
+Quote Post

Goober
RE: Object Reference Not Set An Instance Of Object
17 Aug, 2008 - 07:56 PM
Post #3

New D.I.C Head
*

Joined: 9 Jul, 2008
Posts: 7

Can provide any example? Because other aspx.page for the project also got use the similar code. But there are no error find.
Thanks
User is offlineProfile CardPM
+Quote Post

nonggiatu
RE: Object Reference Not Set An Instance Of Object
18 Aug, 2008 - 05:54 PM
Post #4

New D.I.C Head
*

Joined: 7 Jun, 2008
Posts: 3


My Contributions
QUOTE(Goober @ 17 Aug, 2008 - 08:56 PM) *

Can provide any example? Because other aspx.page for the project also got use the similar code. But there are no error find.
Thanks

After this :
[code]Button btn = e.Row.FindControl("btnEdit") as Button;[code]
if there is a button named "btnEdit" on Row, the btn is a instance of type Button, so you can use its methods and properties
But if the Datagrid Row don't have a button named "btnEdit", it can't find that button to set value for variable btn. So, the value of btn is null.

User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 12/2/08 11:14PM

Live C# Help!

C# Tutorials

Reference Sheets

C# Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month