Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

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

Posted on 2009-07-09
4
Medium Priority
?
305 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
[X]
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
  • 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 2000 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

Looking for a new Web Host?

Lunarpages' assortment of hosting products and solutions ensure a perfect fit for anyone looking to get their vision or products to market. Our award winning customer support and 30-day money back guarantee show the pride we take in being the industry's premier MSP.

Question has a verified solution.

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

When it comes to write a Context Sensitive Help (an online help that is obtained from a specific point in state of software to provide help with that state) ,  first we need to make the file that contains all topics, which are given exclusive IDs. …
Australian government abolished Visa 457 earlier this April and this article describes how this decision might affect Australian IT scene and IT experts.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
Any person in technology especially those working for big companies should at least know about the basics of web accessibility. Believe it or not there are even laws in place that require businesses to provide such means for the disabled and aging p…

618 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