Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

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

Posted on 2009-07-09
4
295 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
ID: 24814539
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
ID: 24814607
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
ID: 24815081
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
ID: 31601631
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

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
"Emulate" TAB key when press Enter Key 3 47
Company website 6 30
How to repeat the data 4 18
Substring works but need to tweak it 14 15
Because your company can’t afford for you to make SEO mistakes, you’ll want to ensure you’re taking the right steps each and every time you post a new piece of content. This list of optimization do’s and don’ts can help you become an SEO wizard.
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
The is a quite short video tutorial. In this video, I'm going to show you how to create self-host WordPress blog with free hosting service.

828 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