I have a page with a very distinct layout with 36 textboxes laid out using a table so that it reflects the layout of an exhibit hall and what exhibit tables can be reserved. I have been able save them all to the database seamlessly by looping through the form keys like this:
Protected Sub btnUpdate_Click(sender As Object, e As EventArgs) Handles btnUpdate.Click
Dim tbname As String = ""
Dim vendorName As String = ""
Dim formkey As String = ""
Dim conn As New SqlConnection(ConnectionStrings.Item("WebDB").ConnectionString)
Dim cmd As New SqlCommand("UPDATE FloorPlan SET VendorName = @VendorName WHERE tbName = @tbName", conn)
conn.Open()
For Each o As Object In Request.Form.Keys
formkey = o.ToString.Replace("ctl00$ContentPlaceHolder1$", "")
If Left(formkey, 2) = "tb" Then
tbname = formkey
vendorName = Request(o.ToString)
cmd.Parameters.AddWithValue("@VendorName", vendorName)
cmd.Parameters.AddWithValue("@tbName", tbname)
cmd.ExecuteNonQuery()
cmd.Parameters.Clear()
tbname = ""
vendorName = ""
End If
Next
conn.Close()
conn.Dispose()
End Sub
I can't figure out how to load the values on page load dynamically where it would locate the textbox id on the page and fill in the value. I have tried things like this:
For Each r In ds1.Tables("SE").Rows
CType(FindControl(r("tbname")), TextBox).Text = r("vendorname")
Next
but I just get the "object reference not set to an instance..." error. What approach should I take to get the values loaded properly without hard-coding tb1.text = "blah"
, tb2.text = "acme"
, etc...