none
How to pass a Gridview label to code behind via a function RRS feed

  • Question

  • Hello,

    I am new to VB and Visual Studio and would greatly appreciate help in accessing data from a GridView Column.

    I need to EITHER pass a Gridview label to the code behind via a function OR retrieve the Gridview label directly from the function in the code behind. The function is GetVideoLink() and I coded it to return the location of a video to the video controls source in the GridView. The video is held in a table and it is retrieved via SqlDataSource using the email as a unique identifier. Each video and corresponding email are held in the same GridView column. The Gridview only displays rows that have a video and email. This code works if I hard-code the email in the code behind or if I pass the hard-coded email to the code behind via the function. 

    My initial approach was to pass "EmailLabel.Text" to the code behind via the GetVideoLink() function. 

    My second approach was to retrieve the GridView label directly from the code behind, which seems much more involved, and which is why my preference would be to pass the value of the email to the function (the function is called once for each row).

    When I try to run with either approach, I get the error "object reference not set to an instance of an object ". 

    This is the portion of the current code for my second approach (I bolded the relevant items):

    FRONT END CODE:

    <asp:TemplateField HeaderText="Video" SortExpression="Video1">

                        <EditItemTemplate>
                            &nbsp;<br />
                            <asp:Label ID="LabelVideo1" runat="server" Height="25px" Text="Label" Width="28px"></asp:Label>
                            <asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("Video1") %>'></asp:TextBox>
                        </EditItemTemplate>
                        <ItemTemplate>
                            <asp:Label ID="EmailLabel" runat="server" Text='<%# Bind("Email") %>'></asp:Label>
                    <video controls ="controls" poster="/Background_Images/myposter.png" style="border: thick solid #FF0000;    width:350px; height: 198px;" src='<%= GetVideoLink() %>'></video>
                        </ItemTemplate>
                    </asp:TemplateField>

    CODE BEHIND (the FindControl line does not work)

       Protected Function GetVideoLink() As String
            Dim email As Label = TryCast(DirectCast(GridView1.SelectedRow.FindControl("EmailLabel"), Label), Label)
            Dim VideoLink As String = " "

            Dim connectionString As String = ConfigurationManager.ConnectionStrings("DefaultConnection").ConnectionString
            Dim cnn As SqlConnection

            cnn = New SqlConnection(connectionString)
            Dim cmd As SqlCommand = New SqlCommand("SELECT Video1 FROM AspNetUserProfileMedia WHERE Email = @Email", cnn)

            cmd.Parameters.Add("@Email", System.Data.SqlDbType.NVarChar, 256)
            cmd.Parameters("@Email").Value = email.Text
            'cmd.Parameters("@Email").Value = "myemail@yahoo.com" (note: this is commented out and works)

            cnn.Open()
            Dim obj As Object = DirectCast(cmd.ExecuteScalar(), Object)

            If obj IsNot Nothing Then
                VideoLink = Convert.ToString(obj)
            End If

            ' Close the connection
            cnn.Close()

            Return VideoLink
        End Function

    Thank you very much for your help,

    Anne

    Wednesday, March 14, 2018 1:48 AM

All replies

  • Hi Anne_G,

    Since this forum is discussing and asking questions about the Visual Basic programming

    language, IDE, libraries, samples, and tools . And your issue is more related to asp.net. This link about asp.net, please refer that: https://forums.asp.net/

    Thank you for participating in the forum activities.

    Best Regards,

    Cherry


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Proposed as answer by Cor Ligthert Wednesday, March 14, 2018 11:12 AM
    Wednesday, March 14, 2018 5:15 AM