Solved

Pass Session Variables to SQL Query to GridView

Posted on 2011-09-07
4
463 Views
Last Modified: 2012-05-12
Experts,

Having some difficulties with this. I am passing session variables from 3 dropdownlists on one page to another page. On the 2nd page, I am grabbing the variables  and trying to use them in a SQL Query to return results to a GridView. Could really use some help on this.


public partial class results : System.Web.UI.Page
    {
        SqlConnection con = new SqlConnection(WebConfigurationManager.ConnectionStrings["MyConnect"].ConnectionString);
        protected void Page_Load(object sender, EventArgs e)
        {
            
                string ssState = (string)(Session["ssState"]);
                string ssCity = (string)(Session["ssCity"]);                
                string ssType = (string)(Session["ssType"]);

                con.Open();
                SqlCommand com = new SqlCommand("SELECT * FROM dbo.tbl_Property WHERE Prop_State=@ssState AND Prop_City=@ssCity AND Prop_Type=@ssType", con);
                com.Parameters.AddWithValue("@ssState", ssState);
                com.Parameters.AddWithValue("@ssCity", ssCity);                
                com.Parameters.AddWithValue("@ssType", ssType);

                SqlDataAdapter myAdapter = new SqlDataAdapter(com);
                DataSet dSet = new DataSet();
                myAdapter.Fill(dSet);   

                
                
                myGrid.DataSource = dSet;
                myGrid.DataBind();

                con.Close();
            
        }
    }

Open in new window

0
Comment
Question by:Shaggy1
  • 2
  • 2
4 Comments
 
LVL 28

Accepted Solution

by:
strickdd earned 500 total points
ID: 36496504
Are you sure you have values in the session variables when you get to this section of code? Debug and step through it to be sure. If you do, take those values and put them in your SQL string and execute that string directly against the SQL server to make sure that you are getting rows back.
0
 

Author Comment

by:Shaggy1
ID: 36496547
Thanks for the help. It seems as though I am getting the values. To test it, i used a label to make sure i was getting the values. I had seen that programmers have been placing the values in the SQL string, but wasn't sure if that was the safest way to do it.
Thanks again!
0
 
LVL 28

Expert Comment

by:strickdd
ID: 36496985
The way you have the SQL - parameterized - is perfect. I'm just suggesting to manaully add the values to the SQL so you can run it in management studio and make sure results are returned. Don't shy away from parameterization!
0
 

Author Closing Comment

by:Shaggy1
ID: 36497259
Well, looks like it was one of the variables...i just removed one of them, and the Grid filled up.
Thanks for the help!
0

Featured Post

Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

Join & Write a Comment

This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
In this article we will get to know that how can we recover deleted data if it happens accidently. We really can recover deleted rows if we know the time when data is deleted by using the transaction log.
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

705 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now