Sunday, March 25, 2012

Bind a variable to a SQL data source for labels, textboxes, etc.

I am trying to set a variable to be passed to my asp page but the value is not carrying over. I don't even know if this is supposed to work or not below:

<tdcolspan="3"style="font-size: 10pt; vertical-align: top; color:<%=Div7fontcolor%>; background-color:<%=Div7bgcolor%>; text-align: center; width: 0px;"id="TopBox"runat="server">

The above Div7fontcolor and Div7bgcolor are variables that I set in the VB file as shown below:

Dim obConnectionAs SqlConnection =New SqlConnection("Data Source=localhost\sqlexpress;Initial Catalog=OrgBoard;Integrated Security=True")Dim obCommandAs SqlCommand =New SqlCommand("SELECT Divisions.*, Dept19.*, Dept20.*, Dept21.*, ConfigDisplay.* FROM Divisions CROSS JOIN Dept19 CROSS JOIN Dept20 CROSS JOIN Dept21 CROSS JOIN ConfigDisplay", obConnection)

obConnection.Open()

Dim drAs SqlDataReader = obCommand.ExecuteReader()

dr.Read()

Dim Div7bgcolorAsString = dr("Div7color").ToString().Trim()

Dim Div7fontcolorAsString = dr("Div7textcolor").ToString().Trim()

dr.Close()

obConnection.Close()

The web page runs fine with no errors but the value does not carry over and change the property correctly.

I am able to set the bgColor value of the TopBox within the VB file if I use "TopBox.BgColor = Div7bgcolor" but I can't set other values on my web page like the font color. If I can do this from the VB file then please correct me. Using variables within the page will allow me to set almost any value but I don't even know if what I want is possible. Any help is greatly apprectiated.

Thank you,

Kris

The easiest thing for this is to use Literal controls:

<tdcolspan="3"style="font-size: 10pt; vertical-align: top; color:<asp:Literal ID="Div7fontcolor" runat="server" />; background-color:<asp:Literal ID="Div7bgcolor" runat="server" />; text-align: center; width: 0px;"id="TopBox"runat="server">

....

dr.Read()

Div7bgcolor.Text = dr("Div7color").ToString().Trim()

Div7fontcolor.Text = dr("Div7textcolor").ToString().Trim()

dr.Close()

No comments:

Post a Comment