my MDB
my MDB

Reputation: 129

Returning SELECT data in C#

I have a SqlDataSource and a literal:

<asp:SqlDataSource ID="CategoryNameSQL" runat="server" 
ConnectionString="<%$ ConnectionStrings:ConnectionString %>" 
    SelectCommand="SELECT [Name] FROM [Categories] WHERE ([ID] = @ID)" 
    ProviderName="<%$ ConnectionStrings:ConnectionString.ProviderName %>">
    <SelectParameters>
        <asp:QueryStringParameter Name="ID" QueryStringField="ID" Type="Int16" />
    </SelectParameters>
</asp:SqlDataSource>
<asp:Literal ID="CategoryName" runat="server"></asp:Literal>

What I'm trying to do is return the category name that has as certain ID, and then change the text for the literal with the retrieved name from the database. How is this done?

Upvotes: 0

Views: 1457

Answers (1)

Samich
Samich

Reputation: 30095

You can do it in codebehind:

protected void Page_Load(object sender, EventArgs e)
{
    DataView dvSql = (DataView)CategoryNameSQL.Select(DataSourceSelectArguments.Empty);
    foreach (DataRowView drvSql in dvSql)
    {
        CategoryName.Text = drvSql["Name"].ToString();
    }   
}

protected void CategoryNameSQL_Selecting(object sender, SqlDataSourceSelectingEventArgs e)
{
    e.Command.Parameters["Name"].Value = Request.QueryString["ID"];
}

Source: http://www.mikesdotnetting.com/Article/64/Bind-Data-From-a-SqlDataSource-to-a-Label

Upvotes: 2

Related Questions