?
Solved

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

Posted on 2009-07-09
4
Medium Priority
?
301 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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

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.
Although a lot of people devote their energy toward marketing for specific industries, there are some basic principles that can be applied to any sector imaginable. We’ll look at four steps to take and examine how those steps were put into action fo…
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…
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).

777 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