locked
mail addresses in gridview are "clickable" in normal mode but gives error in edit mode RRS feed

  • Question

  • User-427368358 posted

    Hi

    I have a gridview with a field for email addresses. I would like these emails to be clickable (to start outlook for example) in normal mode only. It works fine, but when I switch to edit mode I get the error: "
    
    Object reference not set to an instance of an object (line with 'maill.text = ...')
    I know why: the label is not defined in EditItemTemplate, only in ItemTemplate:

    <asp:TemplateField HeaderText="mail" SortExpression="mail" ItemStyle-HorizontalAlign="Center" >
    <EditItemTemplate>
    <asp:TextBox ID="mail" runat="server" MaxLength="20" Width="120px" Text='<%# Bind("mail") %>' ></asp:TextBox>
    </EditItemTemplate>
    <ItemTemplate>
    <asp:Label ID="maill" runat="server" Text='<%# Bind("mail") %>'></asp:Label>
    </ItemTemplate>

    Code-behind: 

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

            For Each dvr In GridView1.Rows
                maill = CType(dvr.FindControl("maill"), Label)
                maill.Text = "<a href='mailto:" & maill.Text & "'>" & maill.Text & "</a>"
            Next

    How to prevent that error in edit mode?
    Thanks
    Raf
    Wednesday, November 11, 2020 10:17 PM

Answers

All replies

  • User-427368358 posted

    Hi

    i tried this and it works:

     Protected Sub GridView1_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GridView1.RowDataBound
            Dim maill As String = ""
            If (e.Row.RowType = DataControlRowType.DataRow And (e.Row.RowState & DataControlRowState.Normal) = DataControlRowState.Normal) Then
                maill = CType(e.Row.Cells(5).Controls(1), Label).Text
                maill = "<a href='mailto:" & maill & "'>" & maill & "</a>"
                e.Row.Cells(5).Text = maill
            End If
        End Sub

    But now, i get another error when i want to insert all the field in another table.

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

    See the code:

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

            Dim mail, mat, tel.  As String

            mat = e.Values("mat")
             mail = e.Values("mail")
             tel = e.Values("tel")

      Using mConnection As New SqlConnection(param.ConnectionString)
                mConnection.Open()

      sql = "INSERT INTO [archief] (mail,tel,mat) VALUES (@mail,@tel,@mat)"
                comd = New SqlCommand(sql, mConnection)
                 comd.Parameters.AddWithValue("@mail", mail)
                comd.Parameters.AddWithValue("@tel", tel)
                comd.Parameters.AddWithValue("@mat", mat)
                 comd.ExecuteNonQuery()
                mConnection.Close()
            End Using

    Any clue?

    Thanks

    Raf

    Thursday, November 12, 2020 11:12 AM
  • User1535942433 posted

    Hi raffarin,

    Accroding to your description and codes,you need to check whether your the value of mail,mat,tel is null.

    As far as I think,e.values couldn't be used with string type.It is object.

    By the way,why you write insert query in RowDeleted event?

    You could  get start with Gridview. More details,you could refer to below article:

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

    Best regards,

    Yijing Sun

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, November 13, 2020 7:47 AM