0

I keep getting this error

Object reference not set to an instance of an object. 

What i want to do is when the button is clicked my panel is visible with a textbox of the id number, of the row that they clicked. by id number i mean the column id, and the number given to it by the database.

Protected Sub GridView1_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs)
    Dim button As Button = CType(e.Row.FindControl("button1"), Button)
    Dim id As Label = CType(e.Row.FindControl("id"), Label)

    button.OnClientClick = _
    Panel1.Visible = True
    Label2.Visible = True
    Label2.Text = id.Text

    UpdatePanel1.Update()



End Sub
5
  • Which line generates the error? Commented Mar 8, 2011 at 16:45
  • 1
    Add a break point on button.OnClientClick and see which control(s) are null Commented Mar 8, 2011 at 16:46
  • button.OnClientClick = line causes the error Commented Mar 8, 2011 at 16:50
  • @wraithnath good idea, and the sad thing is they are both nothing Commented Mar 8, 2011 at 16:52
  • Check the row type is a DataRow, if you try and find the control in the header im guessing you dont have the button in it. I think its the (e.Row.RowType) property you need to check Commented Mar 9, 2011 at 9:01

4 Answers 4

3

Try to add the following condition to ensure whether the DataRow (rather than the HeaderRow) is being processes at this moment:

If e.Row.RowType = DataControlRowType.DataRow Then
    ...
End If
Sign up to request clarification or add additional context in comments.

2 Comments

thanks, but for some reason when i click the button it doesnt change the label text
@MyHeadHurts: The OnClientClick is intendant for working on the client side. To be able change the server-side controls’ properties, it is necessary to use the server-side button’s Click event.
1

Its more than likely one of the controls you are searching for is not being found.

Try the following, the error will tell you which one is not being found correctly.

Dim button As Button = CType(e.Row.FindControl("button1"), Button)
if(button == null) throw new ApplicationException("button1 was not found");

Dim id As Label = CType(e.Row.FindControl("id"), Label)
if(id== null) throw new ApplicationException("id was not found");

NB* All the information you need to find where the error is occurring is contained inside the exception details. Look at the stack trace for the line number. If there is one skill every programmer needs is the ability to read exceptions correctly and pay attention to them.

Comments

1

I would suspect your issue may be coming from

Dim id As Label = CType(e.Row.FindControl("id"), Label)

this line returning Nothing (as it may not have found the control) and then you are trying to access one of its properties here

Label2.Text = id.Text

I have no idea what this line is trying to

button.OnClientClick = _

but if it failed to find the control this would also cause the same error.

To solve the problem you are having I would recommend not doing this in the manner you are. But setting an OnClick in button definition of the Row for a method like MyButtonClicked, handle this server side where you will be be able to determine the row sending the command and then have you're logic to hide/show the panel/text boxes etc

1 Comment

on the button.onclientclick i am trying to set the commands that the button will run
0

One of your two controls isn't being found in the row. Is the ID for the Label control really "id"?

1 Comment

there both nothing in debug but both are there

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.