954,595 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Help with Databinding

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....

Horizon
Newbie Poster
2 posts since Dec 2004
Reputation Points: 10
Solved Threads: 0
 

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?

tgreer
Made Her Cry
Team Colleague
2,118 posts since Dec 2004
Reputation Points: 227
Solved Threads: 37
 

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.

Horizon
Newbie Poster
2 posts since Dec 2004
Reputation Points: 10
Solved Threads: 0
 

Which Command object? For SQL Commands?

tgreer
Made Her Cry
Team Colleague
2,118 posts since Dec 2004
Reputation Points: 227
Solved Threads: 37
 

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!

Paladine
Master Poster
Team Colleague
824 posts since Feb 2003
Reputation Points: 211
Solved Threads: 27
 

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>
        &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.

jturlington
Newbie Poster
4 posts since Jun 2005
Reputation Points: 10
Solved Threads: 0
 

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.

tgreer
Made Her Cry
Team Colleague
2,118 posts since Dec 2004
Reputation Points: 227
Solved Threads: 37
 

editted post :)

jturlington
Newbie Poster
4 posts since Jun 2005
Reputation Points: 10
Solved Threads: 0
 

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

editted post :)
Paladine
Master Poster
Team Colleague
824 posts since Feb 2003
Reputation Points: 211
Solved Threads: 27
 

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

jturlington
Newbie Poster
4 posts since Jun 2005
Reputation Points: 10
Solved Threads: 0
 

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

tgreer
Made Her Cry
Team Colleague
2,118 posts since Dec 2004
Reputation Points: 227
Solved Threads: 37
 

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 helpsThen 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).

Paladine
Master Poster
Team Colleague
824 posts since Feb 2003
Reputation Points: 211
Solved Threads: 27
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You