Link to home
Start Free TrialLog in
Avatar of mcrmg
mcrmg

asked on

Query question

Hi,

I am trying to run a query in code behind, because the query is simple, I do not want to create a sp just for that. Here is my code. It runs well without a parameter.


        SqlConnection con = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["conn"].ToString());

        SqlCommand cmd = new SqlCommand("SELECT CategoryName from Category ORDER BY CategoryName="  + Session["sCMID"], con);

        SqlDataAdapter da = new SqlDataAdapter(cmd);
        DataTable table = new DataTable();
        da.Fill(table);

        ContactName.DataSource = table;
        ContactName.DataValueField = "CategoryName";
        ContactName.DataTextField = "CategoryName";
        ContactName.DataBind();


        con.Close();

Open in new window


The result will be loaded into a dropdown list.  thAnks
SOLUTION
Avatar of Dustin Saunders
Dustin Saunders
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
What is the question here?
Avatar of mcrmg
mcrmg

ASKER

I need help with passing the parameter. thanks
Avatar of mcrmg

ASKER

The code works fine

SqlCommand cmd = new SqlCommand("SELECT CategoryName from Category ORDER BY CategoryName=", con);

But when I try to add parameter, it does not work

SqlCommand cmd = new SqlCommand("SELECT CategoryName from Category ORDER BY CategoryName="  + Session["sCMID"], con);

thanks
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of mcrmg

ASKER

I do not know what I was thinking...  Where is the "WHERE clause"...............
Avatar of mcrmg

ASKER

thanks