Solved

c#.NET Create Dynamic SQL statement from info in a URL OnClick

Posted on 2004-09-28
4
260 Views
Last Modified: 2010-04-15
I have two pages:
The first page displays an image, the image is a map of london, I have used an image map to cut up the districts on the map. The idea is that a user clicks a district and go to a new page (page two) containing the areas within that district.
I have a mssql database with the info for the districts/areas setup and working fine.
My current code and SQL query that is performed when a user clicks a district is:

<script language="C#" runat="server">
   void Page_Load(Object sender, EventArgs e)
   {
     
     
        SqlConnection myConnection = new SqlConnection("server=localhost;" +
         "database=ldc;Trusted_Connection=Yes");

      SqlDataAdapter myCommand = new SqlDataAdapter("SELECT * FROM" +
         " MapLocations where district='camden'", myConnection);
     
     
      DataSet ds = new DataSet();
      myCommand.Fill(ds);
     
      MyRepeater.DataSource = ds;
      MyRepeater.DataBind();
   }
</script>

I want to change the SQL query so that it populates district= with the name of the district the user clicked on the previous page.

I tried something along the line of:

SqlDataAdapter myCommand = new SqlDataAdapter("SELECT * FROM" +
         " MapLocations where district=Response.Write([id])", myConnection);

Where id would be passed in the URL   such as  map.aspx?id=OxfordCircus

all the data in the dbase is vchar.

But it fails....is there some easy way of doing this so i dont have to create 30 separate aspx files containing 30 unique sql queries to reflect the 30 different districts in london...?
0
Comment
Question by:HoomanJamshidiNHE
[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
4 Comments
 
LVL 15

Expert Comment

by:praneetha
ID: 12174949
change it !Page.IsPostback..

ad when u r passing the id in the url..it is accessed

using Request.QueryString["id"].toString()....now you shoul dbe able to use it

SqlDataAdapter myCommand = new SqlDataAdapter("SELECT * FROM" +
         " MapLocations where district="+Request.querystring["id"].ToString(), myConnection);

<script language="C#" runat="server">
   void Page_Load(Object sender, EventArgs e)
   {
      if(!Page.ispostback) //change that to not postback
{
     
       SqlConnection myConnection = new SqlConnection("server=localhost;" +
         "database=ldc;Trusted_Connection=Yes");

      SqlDataAdapter myCommand = new SqlDataAdapter("SELECT * FROM" +
         " MapLocations where district='camden'", myConnection);
     
     
      DataSet ds = new DataSet();
      myCommand.Fill(ds);
     
      MyRepeater.DataSource = ds;
      MyRepeater.DataBind();
}
   }
</script>
0
 
LVL 5

Expert Comment

by:tomasX2
ID: 12174960
string id = whatever your id is... // don´t think you can get it from response.write.....
string select = "SELECT * FROM MapLocations where district='" + id + "'";
SqlDataAdapter myCommand = new SqlDataAdapter(select , myConnection);
0
 
LVL 5

Accepted Solution

by:
tomasX2 earned 500 total points
ID: 12174972
string id would probably be something like so...


string id = Request["id"].ToString();
string select = "SELECT * FROM MapLocations where district='" + id + "'";
SqlDataAdapter myCommand = new SqlDataAdapter(select , myConnection);
0
 

Author Comment

by:HoomanJamshidiNHE
ID: 12177960
thanks man....that saves me tons of time.
0

Featured Post

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Google Directions API to Map URL -C#? 3 46
tableview is not updating 1 28
How to read text with RegEx... 2 34
Dictionary and array of [N] size - performance tuned 12 41
Extention Methods in C# 3.0 by Ivo Stoykov C# 3.0 offers extension methods. They allow extending existing classes without changing the class's source code or relying on inheritance. These are static methods invoked as instance method. This…
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.
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

749 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