Showing posts with label labels. Show all posts
Showing posts with label labels. Show all posts

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()

Bind a label to an SqlDataSource

Hi all,

I would like to do something I thought was simple but I can't seem to figure it out. I would like to bind the Text properties to two labels to the columns in an SqlDataSource, much the same as you can bind a listbox to a SqlDataSource.

Is this possible?

The reason why I'm trying it this way is because I originally used an OleDB connection and datareader, but it doesn't seem to work on our service providers server (keeps saying that it can't find the database) even though it works on the four other server's I've tried. It definitely connects to the database when I use the SqlDataSource on a listbox control, but it fails when I use the same connection string with the OleDB connection.

Is this the best way to go about it, or should I persist with finding the OleDB/datareader (the service provider has been no help at all).

Thanks.

If you want to use SqlDataSource, why not bind SqlDataSource with GridView? So that you can view all records:

<asp:SqlDataSource ID="mySDS" runat="server" ConnectionString="<%$ ConnectionStrings:Conn2000 %>"
SelectCommand="SELECT FirstName, LastName, Title FROM Employees">
</asp:SqlDataSource>
<asp:GridView runat="server" ID="myGV" DataSourceID="mySDS" AutoGenerateColumns="true">
</asp:GridView
BTW, what's the exception you got when connecting with OleDB connection? OleDB connection string can differ from SqlDataSource connection string, you can check http://www.connectionstrings.com/