Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7065719
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T04:59:25+00:00 2026-05-28T04:59:25+00:00

I have my two select statements ( Str1 , Str2 ) in two string

  • 0

I have my two select statements (Str1,Str2) in two string variables. I also have a condition where the appropriate select statement is selected and assigned to another string (res).

This is the select statement that is to be assigned to the SelectCommand property of a SqlDataSource control on my web page.

The code is:

int id =1;
string Str1 = "Select * from table";
string Str2 = "Select * from table where id = "+id;
string res;
if(id == 0)
{
  res=Str2;
}else
{
  res=Str1;
}

My markup looks like:

<asp:gridview.....></aspgridview>
<asp:SqlDataSource ID="ds1" runat="server"
  ConnectionStrings="<%ConnectionStrings.ConnStr%>"
  SelectCommand="<%# res%>">
</asp:SqlDataSource>

How do I assign my string variable res to my SqlDataSource SelectCommand attribute from my codebehind or inline C# code?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T04:59:25+00:00Added an answer on May 28, 2026 at 4:59 am

    You cann set the SelectCommand property from your code-behind, by adding the following statement after the if/else block:

    ds1.SelectCommand = res;
    

    Then you can remove the expression from the markup code:

    <asp:SqlDataSource ID="ds1" runat="server"
      ConnectionStrings="<%ConnectionStrings.ConnStr%>" />
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have tried the following two statements: SELECT col FROM db.tbl WHERE col (LIKE
I have two SELECT statements that return a number each, and I'd like to
I have two separate SELECT statements: SELECT VCe.VId FROM `VCe` WHERE `YId` = 9007
I have two separate SELECT statements which are both GROUP-BY'd separately e.g.: SELECT x,
I have two TSQL EXEC statements EXECUTE (N'MyDynamicallyGeneratedStoredProcedure') -- return 0 on success SELECT
Let's say.. I have two statements select min(Log_In_Time) from tbl where (event_ID=4) select max(Log_Off_Time)
I have two select statements that I am trying to port from Oracle to
I have two select statements that find the maximum salary for a specific role.
I currently have two fairly long select statements, identical apart from the fact that
I have two SELECT statements in SQL Server like these: (SELECT [UserID] FROM [User])

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.