0
votes

I'm trying to change the text in a label inside formview. In theory when the user clicks the button this should go to a SQL table, extract data, and enter the new text into a label named Description. This is databound so I suspect this is part of my problem as I get the error message "Object reference not set to an instance of an object."

Below is the asp portion (sorry but it won't display exactly as it is entered in asp):

td class="style29" colspan="4" style="border: 1px solid #000000"> strong>Description: /strong>

asp:Label ID="DescriptionLabel" runat="server" Text='<%# Bind("Description") %>' Width="1000px"

Below is the VB code behind:

Protected Sub Button1_Click(sender As Object, e As System.EventArgs) Handles Button1.Click
    Dim conCString1 As String = ConfigurationManager.ConnectionStrings("conCString1").ConnectionString
    Dim sqlConnection1 As SqlConnection = New SqlConnection(ConfigurationManager.ConnectionStrings("conCString1").ConnectionString)
    Dim cmd1 As New SqlCommand
    cmd1.CommandType = CommandType.Text
    cmd1.Connection = sqlConnection1

    Dim querystring1 As String = "SELECT [Rule] FROM [BuildRules] WHERE ([Table] = N'Capacitors') AND (Field = N'Description')"
    sqlConnection1.Open()
    Dim command2 As New SqlCommand(querystring1, sqlConnection1)
    Dim reader2 As SqlDataReader = command2.ExecuteReader()
    Dim lblD As Label = FormView1.FindControl("DescriptionLabel")

    While reader2.Read()
        'below is test line only
        lblD.Text = reader2(0) 'Example: 'CAP', Value, Dielectric Type, Package Size, Rated Voltage, Tolerance, Temperature Coefficient


    End While
    sqlConnection1.Close()

End Sub
1

1 Answers

1
votes

I don't think your 'FindControl' is working properly. Try this:

  Dim lblD As Label = DirectCast(FindName("DescriptionLabel"), Label)

And see if that works.