D Infosystems
D Infosystems

Reputation: 524

what is wrong in my this vb.net code

alt text

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Dim lab As Label = Me.GridView1.FindControl("Label1")
        If TextBox2.Text = "7" Then
            GridView1.SelectedRow.Cells(2).Text = "500"
        Else
            GridView1.SelectedRow.Cells(2).Text = "950"
        End If
    End Sub

The following error occurs : Object reference not set to an instance of an object.

Upvotes: 0

Views: 126

Answers (2)

Graham Clark
Graham Clark

Reputation: 12956

You've got this code in your Page Load event, so it will run when the page is first loaded, and on every single postback. This probably isn't what you want.

I imagine that on the very first load, there isn't a selected row in your GridView, so GridView1.SelectedRow is going to be null. If this isn't null, then Cells or Cells(2) definitely will be. Trying to access a property on null is going to throw a NullReferenceException - "Object reference not set to an instance of an object".

As this MSDN example shows, you're probably better off accessing the SelectedRow property in an event handler for the SelectedIndexChanged event of the GridView.

Upvotes: 2

adrianos
adrianos

Reputation: 1561

Dim lab As Label = Me.GridView1.FindControl("Label1")

It doesn't look like you're doing anything with this label. Put a breakpoint on that line and see if it finds it. If it doesn't and you don't even use it, take the line out.

Also, check if textbox2 is valid whilst debugging.

Upvotes: 0

Related Questions