bind sqldatasource result to textbox c#

gdubs picture gdubs · Mar 9, 2011 · Viewed 13k times · Source

I have a SqlDataSource that returns 1 field (1 row) for an ID. I would like to get that result and display it in a TextBox. I would normally do it using a stored procedure (since the stored procedure is already created), but I was thinking SqlDataSource would be easier. Is there a way to bind that result onto my TextBox?

<asp:SqlDataSource ID="ds1" runat="server"
    ConnectionString="<%$ ConnectionStrings:conn1%>"
    ProviderName="<%$ Connectionstrings:conn1.ProviderName %>"
    SelectCommand="sp_1"
    SelectCommandType="StoredProcedure">
    <SelectParameters>
        <asp:Parameter Name="accountID" Type="Int32" />
        <asp:Parameter Name="activitydate" Type="DateTime" Direction="Output" />
    </SelectParameters>
</asp:SqlDataSource>

Answer

obulet picture obulet · Mar 10, 2011

You cannot just bind textbox like that, there is no DataSourceID property on textbox. My suggestion, you may create a DataList using that DataSource and on ItemTemplate, you can do:

<asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind ('activitydate') %>'></asp:TextBox>