I am trying to databind one entry retrieved from a SQL statement.

All I want to do is databind in the code behind page.

For some reason I have lost my brain and cannot figure out how to do it....

Recommended Answers

All 11 Replies

Databind WHAT to WHAT? You have your data in SQL, you get it out and it's now inside, what? A DataReader? DataTable?

You want to bind this data to which ASP.NET Server control?

Sorry I should have made myself clearer.

I was trying to databind to a label, and just found an easier way. Through the use of the Command Object.

I was losing my train of thought and went on to post the question but didnt realize I left out the main reason.

Which Command object? For SQL Commands?

Yes, sorry to say but you are not being very clear in what you are wanting to do.

Have you successfully connected to the SQL Server DB?

You can use a Container to the specific Value you want to associate with the LABEL Object.

Below is a sample to this code:

<asp:Label id="label1" runat="server"
  Text='<%# Container.DataItem("CustomerName") %>

Hope this helps!

So, would you use it like this:

<tr><td colspan="2"><hr color="#F53167" width="100%" size="3"></td></tr>
<tr><td colspan="2">
    <form id="form1" runat="server">
        <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:tcmConnectionString1 %>" SelectCommand="SELECT * FROM [prod]">
        </asp:SqlDataSource>
        <asp:SqlDataSource ID="SqlDataSource2" runat="server" ConnectionString="<%$ ConnectionStrings:tcmConnectionString1 %>" SelectCommand="SELECT [prod_description] FROM [prod] WHERE ([ID] = @ID)">
            <SelectParameters>
                <asp:ControlParameter ControlID="DropDownList1" Name="ID" PropertyName="SelectedValue"
                    Type="Int64" />
            </SelectParameters>
        </asp:SqlDataSource>
        <asp:DropDownList ID="DropDownList1" runat="server" DataSourceID="SqlDataSource1"
            DataTextField="prod_name" DataValueField="ID">
        </asp:DropDownList><br />
        &nbsp;<asp:Label ID="Label1" runat="server" Text='<%# SQLDataSource2.DataItem("prod_description") %>'></asp:Label>
    </form></td></tr>
    </table>

I am pretty sure I am way off.

Please wrap your code in code tags. Please start a new thread, rather than posting your question as a reply to a resolved thread.

With ASP.NET, it's best practice to separate your pages into an "HTML" template, and a code-behind file.

Edited...ok. But what are you asking still?

editted post :)

to have the dropdownlist change what the text of the label is.

Then ask that in another thread, or forum. You don't need databinding to accomplish that task (nor do you even need ASP.NET or any server-side code).

Agreed Tgreer!

Ok, before I give you how to do this, please read some of the tutorials, explorer MSDN and learn some basics on the coding of ASP.Net pages. Trust me even if you don't understand it all, it will begin to make sense the more you use/apply that knowledge.

Here is the pseudo-code to do what you asked: (**note: You can do this in one line of code, that fires when you select something different in the dropdownlist, but it requires one property parameter to be changed, and doesn't require a button. But I will let you figure that out. )

HTML side:

<body MS_POSITIONING="GridLayout">
	    <form id="Form1" method="post" runat="server">
		    <asp:DropDownList id="cboValue" style="Z-INDEX: 101; LEFT: 48px; POSITION: absolute; TOP: 40px" runat="server"
		        Width="184px">
		        <asp:ListItem>Result One</asp:ListItem>
		        <asp:ListItem>Result Two</asp:ListItem>
		        <asp:ListItem>Result Three</asp:ListItem>
			</asp:DropDownList>
		    <asp:Label id="lblResult" style="Z-INDEX: 102; LEFT: 328px; POSITION: absolute; TOP: 40px"
		        runat="server" Width="176px"></asp:Label>
		    <asp:Button id="Button1" style="Z-INDEX: 103; LEFT: 208px; POSITION: absolute; TOP: 88px" runat="server"
		        Text="Button"></asp:Button></form>
	</body>

Code Behind:

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
		'Put user code to initialize the page here
	End Sub
	Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
		lblResult.Text = cboValue.SelectedItem.Text
	End Sub

Hope this helps

Then ask that in another thread, or forum. You don't need databinding to accomplish that task (nor do you even need ASP.NET or any server-side code).

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.