Solved

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

Posted on 2009-07-09
4
297 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 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

Back Up Your Microsoft Windows Server®

Back up all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Problem to Office 1 40
C# Windows Form Navigation - Total Beginner 9 40
SQL parsing XML works but want to do it another way 4 18
denied execute as 13 25
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
This article will inform Clients about common and important expectations from the freelancers (Experts) who are looking at your Gig.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.

763 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