locked
Error : Object reference not set to an instence of an object RRS feed

  • Question

  • User910710390 posted

    I build textbox in GridView  ,I write code in event RowDataBound , show msgbox exeption Object Reference not set to an instence of an object  for object textbox such as :

    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 -> int use tostring()

    txtid.text=0

    end if

    if x.fnamepro.trim() =nothing then

    txtfname.text= 0 ; -> here show error . 

    end if

    if x.lnamepro.trim() =nothing then

    txtlname.text= 0 ; -> here show error . 

    end if

    if x.phonepro.trim() =nothing then

    txtphone.text= 0 ; -> here show error . 

    end if

    dim dt as new datatable

    dt=x.find()->func select

    gvperson.datasource=dt

    gvperson.databind()

    end sub

    After Run :

    SHOW MSGBOX AND SHOW THE FIRST ROW IN GRIDVIEW .

    Please help me ,how I can correct this error

     

    Monday, October 22, 2012 5:33 AM

Answers

  • User-1716253493 posted
    use : dim txtphone as textbox = ctype (e.row.findcontrol("txtphone"),textbox)
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, October 22, 2012 10:41 AM
  • User-1716253493 posted

    you need to make sure for all e.row.findcontrol("xxx") that control with id="xxx" found inside the row.

    OBject reference not set to an instence of an object indicate that the control with id you findcontrol not found.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, October 22, 2012 7:10 PM

All replies

  • User-179079940 posted

    txtphone.text=convert.tostring(0);

    Monday, October 22, 2012 5:35 AM
  • User910710390 posted

    I DID IT ,bUT SHOW TH SAME ERROR

    Monday, October 22, 2012 5:38 AM
  • User824124681 posted

    Probably you are getting null  in textbox variable as it might not getting control.

    Please try your VB version of this-

    (TextBox)(GV_InlineEditing.Rows[0].Cells[2].FindControls["myControlID"]).Text;



    Monday, October 22, 2012 5:40 AM
  • User-179079940 posted

    put your full code here....

    Monday, October 22, 2012 5:41 AM
  • User910710390 posted

    I put full code  in edit post

     

     

    Monday, October 22, 2012 5:59 AM
  • User-179079940 posted

    full code put in foreach loop. see the sample example o here

    http://stackoverflow.com/questions/1031704/visual-basic-how-do-i-read-each-row-in-a-datagrid

    Monday, October 22, 2012 6:02 AM
  • User910710390 posted

    mr anil..india

    THIS CODE CANN'T WRITE IN MY CODE BECOUE SHOW ERROR IN ERROR LIST

    Monday, October 22, 2012 6:06 AM
  • User910710390 posted

    Dim row As Integer = 0

    For Each row In GVperson -> show error for name grid view : : I use gridview not datagridview

    MessageBox.Show(DataGridView1.Pers_PhoneNumber.text &

    "," & GVperson.Pers_FirstName.text & "," & GVperson.Pers_LastName.text & "," & GVperson.Pers_PersonId.text)

    Next

    Monday, October 22, 2012 6:25 AM
  • User-1716253493 posted
    make sure you have textbox inside itemtemplate with id="Pers_ PhoneNumber" and runat="server"
    Monday, October 22, 2012 7:07 AM
  • User910710390 posted

    you can read it :

    i don't found   id="Column_name"

    mr oned_gk  please read  code and tell me it's right or not

    thanks

    <Columns>

    <asp:TemplateField HeaderText="ID" meta:resourcekey="TemplateFieldResource1">

    <EditItemTemplate>

    <asp:TextBox ID="TextBox1" runat="server" meta:resourcekey="TextBox1Resource1"></asp:TextBox>

    </EditItemTemplate>

    <ItemTemplate>

    <asp:TextBox ID="txtID" runat="server" Text='<%# BIND("Pers_PersonId") %>' meta:resourcekey="txtIDResource1"></asp:TextBox>

    </ItemTemplate>

    </asp:TemplateField>

    <asp:TemplateField HeaderText="First Name" meta:resourcekey="TemplateFieldResource2">

    <EditItemTemplate>

    <asp:TextBox ID="TextBox4" runat="server" meta:resourcekey="TextBox4Resource1"></asp:TextBox>

    </EditItemTemplate>

    <ItemTemplate>

    <asp:TextBox ID="txtfname" runat="server" Text='<%# bind("Pers_FirstName") %>' meta:resourcekey="txtfnameResource1"></asp:TextBox>

    </ItemTemplate>

    </asp:TemplateField>

    <asp:TemplateField HeaderText="Last Name" meta:resourcekey="TemplateFieldResource3">

    <EditItemTemplate>

    <asp:TextBox ID="TextBox6" runat="server" meta:resourcekey="TextBox6Resource1"></asp:TextBox>

    </EditItemTemplate>

    <ItemTemplate>

    <asp:TextBox ID="txtlname" runat="server" Text='<%# Bind("Pers_LastName") %>' meta:resourcekey="txtlnameResource1"></asp:TextBox>

    </ItemTemplate>

    </asp:TemplateField>

    <asp:TemplateField HeaderText="Phone Number" meta:resourcekey="TemplateFieldResource4">

    <EditItemTemplate>

    <asp:TextBox ID="TextBox2" runat="server" meta:resourcekey="TextBox2Resource1"></asp:TextBox>

    </EditItemTemplate>

    <ItemTemplate>

    <asp:TextBox ID="txtphone" runat="server" Text='<%# bind("Pers_PhoneNumber") %>' meta:resourcekey="txtphoneResource1"></asp:TextBox>

    </ItemTemplate>

    </asp:TemplateField>

    <asp:TemplateField meta:resourcekey="TemplateFieldResource5">

    <EditItemTemplate>

    <asp:TextBox ID="TextBox3" runat="server" meta:resourcekey="TextBox3Resource1"></asp:TextBox>

    </EditItemTemplate>

    <ItemTemplate>

    <asp:LinkButton ID="LinkButton1" runat="server" CommandName="update" meta:resourcekey="LinkButton1Resource1">Update</asp:LinkButton>

    </ItemTemplate>

    </asp:TemplateField>

    <asp:TemplateField meta:resourcekey="TemplateFieldResource6">

    <EditItemTemplate>

    <asp:TextBox ID="TextBox5" runat="server" meta:resourcekey="TextBox5Resource1"></asp:TextBox>

    </EditItemTemplate>

    <ItemTemplate>

    <asp:LinkButton ID="LinkButton2" runat="server" CommandName="Delete" meta:resourcekey="LinkButton2Resource1">Delete</asp:LinkButton>

    </ItemTemplate>

    </asp:TemplateField>

    </Columns>

    <RowStyle BackColor="#FFFBD6" ForeColor="#333333" />

    <SelectedRowStyle BackColor="#FFCC66" Font-Bold="True" ForeColor="Navy" />

    <PagerStyle BackColor="#FFCC66" ForeColor="#333333" HorizontalAlign="Center" />

    <HeaderStyle BackColor="#990000" Font-Bold="True" ForeColor="White" />

    <AlternatingRowStyle BackColor="White" />

    </asp:GridView>

    Monday, October 22, 2012 8:13 AM
  • User910710390 posted

    make sure you have textbox inside itemtemplate with id="Pers_ PhoneNumber" and runat="server"

    I sure it its right and you can sure for my source code ,I posted my source code  

    Monday, October 22, 2012 8:54 AM
  • User-1716253493 posted
    i dont see Pers_ PhoneNumber but see txtphone
    Monday, October 22, 2012 9:04 AM
  • User910710390 posted

    yes this is id for textbox  in itemtemplet

    Are they like it or there must be Name_column ?

     

    Monday, October 22, 2012 9:37 AM
  • User910710390 posted

    i dont see Pers_ PhoneNumber but see txtphone

    yes this is id for textbox  in itemtemplet

    Pers_PhnineNumber like txtphone or  there must be Name_column ?

    THANKS ONED_GK

     

     

    Monday, October 22, 2012 9:40 AM
  • User-1716253493 posted
    use : dim txtphone as textbox = ctype (e.row.findcontrol("txtphone"),textbox)
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, October 22, 2012 10:41 AM
  • User910710390 posted

    use : dim txtphone as textbox = ctype (e.row.findcontrol("txtphone"),textbox)

    I use it ,  show This error 

    Mr.oned_gk  AFTER RUN  SHOW MSGBOX OBject reference not set to an instence of an object  .click ok on msgbox show the first  row data for  table person in gridview , I want show all rows data and updated deleted

     

    Monday, October 22, 2012 4:04 PM
  • User-1716253493 posted

    you need to make sure for all e.row.findcontrol("xxx") that control with id="xxx" found inside the row.

    OBject reference not set to an instence of an object indicate that the control with id you findcontrol not found.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, October 22, 2012 7:10 PM
  • User910710390 posted

     

    you need to make sure for all e.row.findcontrol("xxx") that control with id="xxx" found inside the row.

    OBject reference not set to an instence of an object indicate that the control with id you findcontrol not found.

    I sure it, FINDCONTROL CORRECT 

    the  findcontrol = column _name in table person database CRM 

       

     

    Tuesday, October 23, 2012 2:33 AM
  • User910710390 posted

    you need to make sure for all e.row.findcontrol("xxx") that control with id="xxx" found inside the row.

    OBject reference not set to an instence of an object indicate that the control with id you findcontrol not found.

    sorry mr oned ,

    I coorrect findcontrol=id ,but now show error

    "An unhandled exception of type 'System.StackOverflowException' occurred in System.Data.dll "

    in rowdatabound I put

    object for textbox =0 ,sauch as : txtphone.text = 0

    You can read code select : 

    Public Function find() AsDataTable

    GETCONNECTION()

    Dim sql As String =

    " Select Pers_PersonId , Pers_PhoneNumber,Pers_FirstName,Pers_LastName from Person where 1 = 1 "

    Dim dt As New DataTable

    Dim da As New SqlDataAdapter(sql, CONN)

    If ID <> 0 Then

    da.SelectCommand.CommandText &=

    " AND Pers_PersonId=@PID"

    da.SelectCommand.Parameters.Add(

    "@PID", SqlDbType.Int).Value = ID

    End If

    If phone <> " " Then

    da.SelectCommand.CommandText &=

    " AND Pers_PhoneNumber=@phone"

    da.SelectCommand.Parameters.Add(

    "@Phone", SqlDbType.NChar).Value = phone

    End If

    If FNAME <> " " Then

    da.SelectCommand.CommandText &=

    " And Pers_FirstName=@FName"

    da.SelectCommand.Parameters.Add(

    "@FNAME", SqlDbType.NChar).Value = FNAME

    End If

    If LNAME <> " " Then

    da.SelectCommand.CommandText &=

    " AND Pers_LastName=@LNAME"

    da.SelectCommand.Parameters.Add(

    "@LNAME", SqlDbType.NChar).Value = LNAME

    End If

    Try

    da.Fill(dt)

    CONN.Open()

    Catch ex As Exception

    MsgBox(ex.Message &

    " Cannot fill ")

    Finally

    CONN.Close()

    End Try

    Return dt

    End Function

    Tuesday, October 23, 2012 3:48 AM
  • User1078835753 posted

    Hello  everyone,

    I am getting this error too - system.NullReferenceException: Object reference not set to an instence of an object.  I am working with window form, not web form.  Is it OK to post my problem  in this forum.  If so, Would you please show me how to go about doing this?  Any help is greatly appreciated!

    Tuesday, October 23, 2012 9:27 AM
  • User1078835753 posted

    This is a VB.net, vs2008 window form app

    Tuesday, October 23, 2012 9:30 AM
  • User910710390 posted

    Mr.hdng

    make sure your fields and id in your code please

    if not succeed you ca post your code here

    good luck

    Tuesday, October 23, 2012 9:52 AM