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
C#

Avatar of undefined
Last Comment
mcrmg
SOLUTION
Avatar of Dustin Saunders
Dustin Saunders
Flag of United States of America image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Avatar of kaufmed
kaufmed
Flag of United States of America image

What is the question here?
Avatar of mcrmg
mcrmg

ASKER

I need help with passing the parameter. thanks
Avatar of mcrmg
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
Avatar of johnsone
johnsone
Flag of United States of America image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
SOLUTION
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
Avatar of mcrmg
mcrmg

ASKER

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

ASKER

thanks
C#
C#

C# is an object-oriented programming language created in conjunction with Microsoft’s .NET framework. Compilation is usually done into the Microsoft Intermediate Language (MSIL), which is then JIT-compiled to native code (and cached) during execution in the Common Language Runtime (CLR).

98K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo