locked
error in edit mode of a gridview RRS feed

  • Question

  • User-427368358 posted

    Hi

    This is part of a gridview. When the gridview is loaded, no error. When i go to edit mode, i get the error: 

    The object reference is not set to an instance of an object. with this code: (at line  m.Text = "ok")

    <asp:TemplateField  >
    <EditItemTemplate>
    <asp:TextBox ID="mail" runat="server" Text='<%# Bind("m") %>' ></asp:TextBox>
    </EditItemTemplate>
    <ItemTemplate>
    <asp:Label ID="maill" runat="server" Text='<%# Bind("m") %>'></asp:Label>
    </ItemTemplate>

     Protected Sub GridView1_DataBound(ByVal sender As Object, ByVal e As System.EventArgs) Handles GridView1.DataBound
           Dim m As Label 

            For Each dvr In GridView1.Rows
                m = CType(dvr.FindControl("m"), Label)
                m.Text = "ok"
            Next

    Thanks

    Raf

    Thursday, November 12, 2020 4:29 PM

Answers

  • User-427368358 posted

    Thanks.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, November 17, 2020 10:53 AM

All replies

  • User475983607 posted

    The label is named "Mail" not "m".  Also, you need to make sure the row is a ItemTempate.  See the standard documentation.

    https://docs.microsoft.com/en-us/dotnet/api/system.web.ui.webcontrols.gridview.rowdatabound?view=netframework-4.8

     

    Thursday, November 12, 2020 4:36 PM
  • User1535942433 posted

    Hi raffarin,

    As far as I think,you need to compare databound event and rowdatabound event.  OnDataBound only fires once for the entire grid.  OnRowDataBound fires once for every row. I'm guessing that you need to use OnRowDataBound.

    The second problem is you need findcontrol of the label's id.

    Since you don't post more details,I created a test.

    <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false" OnRowDataBound="GridView1_RowDataBound">
                    <Columns>
                        <asp:TemplateField>
                            <ItemTemplate>
                                <asp:Label ID="maill" runat="server" Text='<%# Bind("num") %>'></asp:Label>
                            </ItemTemplate>
                            <EditItemTemplate>
                                <asp:TextBox ID="mail" runat="server" Text='<%# Bind("num") %>' ></asp:TextBox>
                            </EditItemTemplate>
                        </asp:TemplateField>
                    </Columns>
                </asp:GridView>

    Code-behind:

    Protected Sub GridView1_RowDataBound(ByVal sender As Object, ByVal e As GridViewRowEventArgs)
        If e.Row.RowType = DataControlRowType.DataRow Then
            Dim m As Label = CType(e.Row.FindControl("maill"), Label)
            m.Text = "ok"
        End If
    End Sub

    Best regards,

    Yijing Sun

    Friday, November 13, 2020 2:28 AM
  • User-427368358 posted

    Thanks for replying.

    Thr wrong label, that's my fault. In my real code its' correct. That's not the problem.

    I copied/paste your code and it works, using: Protected Sub GridView1_RowDataBound(ByVal sender As Object, ByVal e As GridViewRowEventArgs)

    Now, when i use this (comes automatically)  Protected Sub GridView1_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GridView1.RowDataBound
        

    I get the error (only in edit mode!, not in normal mode): Object reference not set to an instance of an object (line m.Text = "ok")

    Amazing, no? I have spenr hours to find out what the problem was ... My previous post is then also solved.Do you have an explanation for this?

    Thanks
       

    Friday, November 13, 2020 9:35 AM
  • User-427368358 posted

    Sorry, i was a little bit too fast. My purpose is to make the mail addresses clickable in the gridview.

    With this code, the mailaddresses are clickable in normal mode but in edit mode, i get the error "Object reference not set to an instance of an object".

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

      If e.Row.RowType = DataControlRowType.DataRow Then
                Dim m As Label = CType(e.Row.FindControl("maill"), Label)
                m.Text = "<a href='mailto:" & m.Text & "'>" & m.Text & "</a>"
            End If
        End Sub

    And with this code, the mail addresses are not clickable in normal/edit mode but the edit mode gives no error.

    Protected Sub GridView1_RowDataBound(ByVal sender As Object, ByVal e As GridViewRowEventArgs)
            If e.Row.RowType = DataControlRowType.DataRow Then
                Dim m As Label = CType(e.Row.FindControl("maill"), Label)
                m.Text = "<a href='mailto:" & m.Text & "'>" & m.Text & "</a>"
            End If
        End Sub

     ??

    Friday, November 13, 2020 10:01 AM
  • User1535942433 posted

    Hi raffarin,

    As far as I think,when you edit the data,it will be a edit state and it find the textbox control instead of label.

    If you want show data as a link in the ItemTemplate ,you could use HyperLink. You need to do like this:

    <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false" OnRowDataBound="GridView1_RowDataBound"  OnRowEditing="GridView1_RowEditing" OnRowUpdating="GridView1_RowUpdating">
                    <Columns>
                        <asp:TemplateField>  
                        <ItemTemplate>  
                            <asp:Button ID="btn_Edit" runat="server" Text="Edit" CommandName="Edit" />  
                        </ItemTemplate>  
                        <EditItemTemplate>  
                            <asp:Button ID="btn_Update" runat="server" Text="Update" CommandName="Update"/>  
                            <asp:Button ID="btn_Cancel" runat="server" Text="Cancel" CommandName="Cancel"/>  
                        </EditItemTemplate>  
                    </asp:TemplateField>  
                        <asp:TemplateField>
                            <ItemTemplate>
                                <asp:HyperLink ID="maill" runat="server" Text='<%# Eval("num") %>' NavigateUrl='<%# Eval("num", "mailto:{0}") %>' />
                            </ItemTemplate>
                            <EditItemTemplate>
                                <asp:TextBox ID="mail" runat="server" Text='<%# Bind("num") %>'  ></asp:TextBox>
                            </EditItemTemplate>
                        </asp:TemplateField>
                    </Columns>
                </asp:GridView>

    Code-behind:

     Protected Sub bind()
            Dim str, strSql As String
            str = System.Configuration.ConfigurationManager.ConnectionStrings("aspnet-TestApplicationWithDatabase-20190820030542").ConnectionString
            Dim conn As SqlConnection = New SqlConnection(str)
            strSql = "select * from Test"
            Dim da As SqlDataAdapter = New SqlDataAdapter(strSql, str)
            Dim ds As DataSet = New DataSet()
            da.Fill(ds, "Test")
            GridView1.DataSource = ds.Tables(0).DefaultView
            GridView1.DataBind()
            conn.Close()
        End Sub
    
        Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
            If Not IsPostBack Then
                bind()
            End If
        End Sub
    
        Protected Sub GridView1_RowDataBound(ByVal sender As Object, ByVal e As GridViewRowEventArgs)
            If e.Row.RowType = DataControlRowType.DataRow Then
    
                If e.Row.RowState = DataControlRowState.Edit Then
                    Dim m1 As TextBox = CType(e.Row.FindControl("mail"), TextBox)
                    m1.Text = "OK"
                End If
            End If
        End Sub
    
        Protected Sub GridView1_RowEditing(ByVal sender As Object, ByVal e As GridViewEditEventArgs)
            GridView1.EditIndex = e.NewEditIndex
            bind()
        End Sub
    
        Protected Sub GridView1_RowUpdating(ByVal sender As Object, ByVal e As GridViewUpdateEventArgs)
        End Sub

    Best regards,

    Yijing Sun

    Monday, November 16, 2020 9:01 AM
  • User-427368358 posted

    Hi i tried your code and it works. Many thanks, but now, i have also a delete-button in the gridview:

     DeleteCommand="DELETE FROM [lening] WHERE [leningnr] = @leningnr" 

    <DeleteParameters><asp:Parameter Name="leningnr" Type="Int32" /></DeleteParameters>

    When i want to delete a row, i first want to copy the row in an archive table. The problem is that the value of 'm' is lost (empty). I get this error when trying to insert the row to table archive::

    The parameterized query '(@naam nvarchar(2),@vnaam nvarchar(4),@mail nvarchar(4000),@tel ' expects the parameter '@mail', which was not supplied.

    I tried this:

      Protected Sub GridView1_RowDeleted(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewDeletedEventArgs) Handles GridView1.RowDeleted

     Dim maila As String

    maila = e.values("mail")

    ' or this: maila = e.Values.Item(5).ToString

    sql = "INSERT INTO [archive] (mail, ..., ...) values (@mail,...,...) (there are more fields in the table)

     comd.Parameters.AddWithValue("@mail", maila)

    if i put this, no error but the mail is not copied with all other fields into table archive.

    If maila = Nothing Then
    comd.Parameters.AddWithValue("@mail", DBNull.Value)
    End If

    All other fields have their values kept (e.g. dim name as string = e.values("name") is ok except the value of the mail. The delete command works.

    Do you have an explanation maybe?

    Thanks again

    Raf

    Monday, November 16, 2020 1:27 PM
  • User-427368358 posted

    I could solve the problem by this way:

        Protected Sub GridView1_RowDeleting(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewDeleteEventArgs) Handles GridView1.RowDeleting

    Dim row As GridViewRow = GridView1.Rows(e.RowIndex)
            Dim hyp As HyperLink = CType(row.FindControl("mail2"), HyperLink)
            Dim maila As String = hyp.Text

    this gives the value i missed for inserting in the table archive.

    I still don't undertsand why this gives no value in the event GridView1_RowDeleting:

    m = e.Values("mail") while it works for all other fields.

    And i also noticed that your event Protected Sub GridView1_RowDataBound is not even needed. It works without it too.

    Monday, November 16, 2020 6:02 PM
  • User1535942433 posted

    Hi raffarin,

    I still don't undertsand why this gives no value in the event GridView1_RowDeleting:

    m = e.Values("mail") while it works for all other fields.

    When you select the row that need to be delete,you will get the current row's index.And you could find the current selected row's HyperLink control.Finally,you could get the control's text.

    If you use e.value("mail"),you could get  all of the "mail " control inside the gridview and you will get all of the text of these "mail" controls.You will get an object.If you need to get the selected value,you must loop for the e.value("mail").

    e.Values means you can get a dictionary of the non-key field name/value pairs for the deleted record, not key field. If you want the key field, please get it from e.Keys.

    More details,you could refer to below article:

    https://forums.asp.net/t/1820111.aspx?e+values+value+GridviewDeleting+GridviewDeleted+

    You could refer to below article about Edit,Update and Delete in gridview:

    https://www.aspsnippets.com/Articles/Edit-Update-and-Delete-in-ASPNet-GridView-with-AutoGenerateColumns-True-using-C-and-VBNet.aspx

    Best regards,

    Yijing Sun

    Tuesday, November 17, 2020 3:10 AM
  • User-427368358 posted

    Thanks.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, November 17, 2020 10:53 AM