Pass Session Variables to SQL Query to GridView


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"]);

                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();

                myGrid.DataSource = dSet;


Open in new window

Who is Participating?
strickddConnect With a Mentor Commented:
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.
Shaggy1Author Commented:
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!
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!
Shaggy1Author Commented:
Well, looks like it was one of the variables...i just removed one of them, and the Grid filled up.
Thanks for the help!
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.