13 Replies - 1992 Views - Last Post: 18 October 2012 - 07:41 AM Rate Topic: -----

#1 mohammed saleh  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 9
  • Joined: 18-October 12

Error Object reference not set to an instance of an object.

Posted 18 October 2012 - 01:04 AM

I build Textbox in Gridview
The event error is :
 Protected Sub GVperson_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GVperson.RowDataBound

        If e.Row.RowType = DataControlRowType.DataRow Then
            Dim txtID As TextBox = CType(e.Row.FindControl("Pers_PersonId"), TextBox)
            Dim txtphone As TextBox = CType(e.Row.FindControl("Pers_PhoneNumber"), TextBox)
            Dim txtfname As TextBox = CType(e.Row.FindControl("Pers_FirstName"), TextBox)
            Dim txtlname As TextBox = CType(e.Row.FindControl("Pers_LastName"), TextBox)

            Dim x As New Person

            If x.idPRO.ToString() = Nothing Then
                txtID.Text = 0
            End If
            If x.phonePRO.Trim() = Nothing Then
                txtphone.Text = 0 [b]-> error object refernce here
            End If
            If x.LNAMEPRO.Trim() = Nothing Then
                txtlname.Text = 0 -> error object refernce here

            End If
            If x.FNAMEPRO.Trim() = Nothing Then
                txtfname.Text = 0 -> error object refernce here

            End If
            Dim dt As New DataTable
            dt = x.find()
            GVperson.DataSource = dt
            GVperson.DataBind()
        End If




    End Sub



Is This A Good Question/Topic? 0
  • +

Replies To: Error Object reference not set to an instance of an object.

#2 lucky3  Icon User is offline

  • Friend lucky3 As IHelpable
  • member icon

Reputation: 231
  • View blog
  • Posts: 765
  • Joined: 19-October 11

Re: Error Object reference not set to an instance of an object.

Posted 18 October 2012 - 01:23 AM

Try to define your TextBox objects with New, so you'll have live instances of TextBox class.
Was This Post Helpful? 0
  • +
  • -

#3 mohammed saleh  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 9
  • Joined: 18-October 12

Re: Error Object reference not set to an instance of an object.

Posted 18 October 2012 - 01:33 AM

I DID
Dim txtphone As NEW TextBox
txtphone= CType(e.Row.FindControl("Pers_PhoneNumber"), TextBox)

SHOW THE SAME ERROR
Was This Post Helpful? 0
  • +
  • -

#4 mohammed saleh  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 9
  • Joined: 18-October 12

Re: Error Object reference not set to an instance of an object.

Posted 18 October 2012 - 02:35 AM

Please help me
I want the answer today
quickly please

nice day
Was This Post Helpful? 0
  • +
  • -

#5 lucky3  Icon User is offline

  • Friend lucky3 As IHelpable
  • member icon

Reputation: 231
  • View blog
  • Posts: 765
  • Joined: 19-October 11

Re: Error Object reference not set to an instance of an object.

Posted 18 October 2012 - 02:50 AM

Perhaps moving this topic to ASP.NET would be better.
Was This Post Helpful? 1
  • +
  • -

#6 mohammed saleh  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 9
  • Joined: 18-October 12

Re: Error Object reference not set to an instance of an object.

Posted 18 October 2012 - 02:52 AM

ok now asp.net
Was This Post Helpful? 0
  • +
  • -

#7 mohammed saleh  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 9
  • Joined: 18-October 12

Re: Error Object reference not set to an instance of an object.

Posted 18 October 2012 - 03:08 AM

Protected Sub GVperson_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GVperson.RowDataBound

If e.Row.RowType = DataControlRowType.DataRow Then
Dim txtID As New TextBox
txtID = CType(e.Row.FindControl("Pers_PersonId"), TextBox)
Dim txtphone As New TextBox
txtphone = CType(e.Row.FindControl("Pers_PhoneNumber"), TextBox)
Dim txtfname As New TextBox
txtfname = CType(e.Row.FindControl("Pers_FirstName"), TextBox)
Dim txtlname As New TextBox
txtlname = CType(e.Row.FindControl("Pers_LastName"), TextBox)

Dim x As New Person

If x.idPRO.ToString() = Nothing Then
txtID.Text = 0
End If
If x.phonePRO.Trim() = Nothing Then
txtphone.Text = 0
End If
If x.LNAMEPRO.Trim() = Nothing Then
txtlname.Text = 0
End If
If x.FNAMEPRO.Trim() = Nothing Then
txtfname.Text = 0
End If
Dim dt As New DataTable
dt = x.find()
GVperson.DataSource = dt
GVperson.DataBind()
End If


End Sub

View Postmohammed saleh, on 18 October 2012 - 03:05 AM, said:

Protected Sub GVperson_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GVperson.RowDataBound

        If e.Row.RowType = DataControlRowType.DataRow Then
            Dim txtID As New TextBox
            txtID = CType(e.Row.FindControl("Pers_PersonId"), TextBox)
            Dim txtphone As New TextBox
            txtphone = CType(e.Row.FindControl("Pers_PhoneNumber"), TextBox)
            Dim txtfname As New TextBox
            txtfname = CType(e.Row.FindControl("Pers_FirstName"), TextBox)
            Dim txtlname As New TextBox
            txtlname = CType(e.Row.FindControl("Pers_LastName"), TextBox)

            Dim x As New Person

            If x.idPRO.ToString() = Nothing Then
                txtID.Text = 0
            End If
            If x.phonePRO.Trim() = Nothing Then
                txtphone.Text = 0
            End If
            If x.LNAMEPRO.Trim() = Nothing Then
                txtlname.Text = 0
            End If
            If x.FNAMEPRO.Trim() = Nothing Then
                txtfname.Text = 0
            End If
            Dim dt As New DataTable
            dt = x.find()
            GVperson.DataSource = dt
            GVperson.DataBind()
        End If


    End Sub



help me for asp.net code
Was This Post Helpful? 0
  • +
  • -

#8 mohammed saleh  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 9
  • Joined: 18-October 12

Re: Error Object reference not set to an instance of an object.

Posted 18 October 2012 - 03:11 AM

I Create a new topic for asp.net
please read it
Was This Post Helpful? 0
  • +
  • -

#9 Atli  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3636
  • View blog
  • Posts: 5,759
  • Joined: 08-June 10

Re: Error Object reference not set to an instance of an object.

Posted 18 October 2012 - 05:10 AM

I've merged your two threads. Please do not double post your questions. If you believe that your thread belongs in another forum, use the "Report" button below your post and ask that it be moved.

As it is, I've moved this thread to the ASP.NET forum.
Was This Post Helpful? 0
  • +
  • -

#10 mohammed saleh  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 9
  • Joined: 18-October 12

Re: Error Object reference not set to an instance of an object.

Posted 18 October 2012 - 05:30 AM

how move this thread to asp.net ?
Was This Post Helpful? 0
  • +
  • -

#11 Atli  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3636
  • View blog
  • Posts: 5,759
  • Joined: 08-June 10

Re: Error Object reference not set to an instance of an object.

Posted 18 October 2012 - 06:03 AM

I've already moved it.

As to your errors. The e.Row.FindControl("Pers_PhoneNumber") calls would return null if the controls aren't found. That would no doubt lead to an exception like that if you then tried to set attributes on the returned null reference.

Are you sure those control names are accurate? In any case, you should probably check to see if those controls return null before you try to assign attribute values on them.
Was This Post Helpful? 0
  • +
  • -

#12 mohammed saleh  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 9
  • Joined: 18-October 12

Re: Error Object reference not set to an instance of an object.

Posted 18 October 2012 - 06:33 AM

after run
show msgbox object reference not set........... after click ok
show just the first record in gridview and
no update no delete
I sure the name attribute is correct

data in first record is
ID = 6 PHONE = NULL FIRST & LAST NAME= NULL
NO DATA FOR 6 IN DATABASE
Was This Post Helpful? 0
  • +
  • -

#13 mohammed saleh  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 9
  • Joined: 18-October 12

Re: Error Object reference not set to an instance of an object.

Posted 18 October 2012 - 07:07 AM

MY REPLY IS CLEAR OR NOT
mr atli
you I sure from this error e.row.findcontrol("pers_phonenumber")
show the same error
Was This Post Helpful? -1
  • +
  • -

#14 Atli  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3636
  • View blog
  • Posts: 5,759
  • Joined: 08-June 10

Re: Error Object reference not set to an instance of an object.

Posted 18 October 2012 - 07:41 AM

First of all, we are not your employees, your servants, or in any way obligated to help you. Do not demand answer from us in raised tones! We are volunteering our time here, and we will try to help you if we can, when we can.


If you want to get to the bottom of this, you need to figure out why your objects are being set to null. Luckily Visual Studio has a great debugger you can use. Set some breakpoints and follow the code. Use that to find where the null is coming from. - If you aren't sure how you can do that, start by reading this tutorial.

And, like I said, the FindControl method will return a null if a control is not found. It is a known possibility, so you should plan for that in your code. Test the controls before trying to use them to see if they are nulls and act accordingly.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1