Solved

How to pass a c# variable to a sql datasource (asp c# sql server)

Posted on 2009-07-09
4
289 Views
Last Modified: 2012-05-07
Hi, please see the code below.  I am trying to access a c# variable in a sqldatasource definition.  Could someone please tell me what I need to change to that I can use the domain variable in the WHERE?  Thanks!  I've been trying for hours...   (I am new to asp, c#  and sql server, since last 2 days).
I have the follwing code in a c# aspx file:
 

<%string domain = (HttpContext.Current.Request.ServerVariables["HTTP_HOST"]);%>
 

<asp:SqlDataSource ID="SqlDataSource1" runat="server" 

        ConnectionString="<%$ ConnectionStrings:testfirststring %>" 

        SelectCommand="SELECT testField FROM table1test WHERE testField = '<% domain %>' "></asp:SqlDataSource>
 

I am missing something to have the domain variable to be accessible in the where statement.
 

Everything else works (for now)!  :)

Open in new window

0
Comment
Question by:ThanksWelcome
  • 2
  • 2
4 Comments
 
LVL 13

Expert Comment

by:crazyman
Comment Utility
just override OnPreRender and add the following :

protected override void OnPreRender(EventArgs e)
{
         this.SqlDataSource1.SelectCommand = String.Format("SELECT testField FROM table1test WHERE testField ='{0}'",HttpContext.Current.Request.ServerVariables["HTTP_HOST"]);
}
0
 

Author Comment

by:ThanksWelcome
Comment Utility
Hi, thanks for your help.

Unfortunately, being a newbie, I have absolutery no clue of how to override OnPreRender as you ask.

Also, I do not know where to add the code you provided.

Thanks for more details!

0
 
LVL 13

Accepted Solution

by:
crazyman earned 500 total points
Comment Utility
Just place it in the code behind, or even inside.

<script runat="server" type="text/C#">
</script>
Then dont set you SelectCommand on your SqlDataSource in the markup..
0
 

Author Closing Comment

by:ThanksWelcome
Comment Utility
Woooohooo!!!  It works!  So happy, thank's!  (I'm do not understanding why, though, because I do not understand what do this function. Any extra thought? For example, what is the purpuse of the {0}? )  Thank's!
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Using SQL Scripts we can save all the SQL queries as files that we use very frequently on our database later point of time. This is one of the feature present under SQL Workshop in Oracle Application Express.
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
The viewer will get a basic understanding of what section 508 compliance can entail, learn about skip navigation links, alt text, transcripts, and font size controls.

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now