0
string ddorder = DropDownList2.SelectedValue; // column
string ddtype = DropDownList3.SelectedValue; //asc or desc
String str1 = "Select * from table1 order by("+ddorder+"  "+ddtype+")";

//there is an error beacuse of ddtype, what am I doing wrong?

SqlCommand cmd = new SqlCommand(str1, con);
con.Open();
cmd.ExecuteNonQuery();
SqlDataAdapter da1 = new SqlDataAdapter();
da1.SelectCommand = cmd;
DataSet ds1 = new DataSet();
da1.Fill(ds1, DropDownList2.SelectedValue);
GridView2.DataSource = ds1;
GridView2.DataBind();
con.Close();
5
  • 2
    What is the error exactly? Aug 14, 2015 at 8:34
  • protected void Button3_Click(object sender, EventArgs e) code is inside this
    – Mert Kaya
    Aug 14, 2015 at 8:35
  • cmd.ExecuteNonQuery(); ıt shows here but because of +ddtype+ in string str1.
    – Mert Kaya
    Aug 14, 2015 at 8:36
  • update the question with error description..
    – TFrost
    Aug 14, 2015 at 8:36
  • An exception of type 'System.Data.SqlClient.SqlException' occurred in System.Data.dll but was not handled in user code Additional information: Incorrect syntax near the keyword 'DESC'.
    – Mert Kaya
    Aug 14, 2015 at 8:37

3 Answers 3

0

As far as I can see, you don't need to use ( and ) in order by clause. It's syntax doesn't have any usage for ( or ).

For example;

order by id desc

will work but

order by (id desc)

won't work.

By the way, use using statement to dispose your SqlConnection, SqlCommand and SqlDataAdapter automatically instead of calling Close method manually.

Also you don't need cmd.ExecuteNonQuery(); part for a SELECT statement. It is unnecessary since it's just execute your select query. It doesn't do or return something.

A few things more;

  • Change your table1 to something meaningful.
  • Don't use SELECT *. It's quite bad.
0
0

Use Dynamic Query:

Change Here:

string ddorder = DropDownList2.SelectedValue; // column
string ddtype = DropDownList3.SelectedValue; //asc or desc
String str1 = "exec(Select * from table1 order by "+ddorder+"  "+ddtype+")";

and

SqlCommand cmd = new SqlCommand(str1, con);
con.Open();
cmd.ExecuteNonQuery();
SqlDataAdapter da1 = new SqlDataAdapter();
da1.SelectCommand = cmd;
DataSet ds1 = new DataSet();
da1.Fill(ds1);
GridView2.DataSource = ds1;
GridView2.DataBind();
con.Close();
0

Remove the parenthesis in the "order by" clause:

String str1 = "Select * from table1 order by "+ddorder+" "+ddtype;
2
  • This does not provide an answer to the question. To critique or request clarification from an author, leave a comment below their post.
    – duffy356
    Aug 14, 2015 at 9:21
  • This would solve his problem, read the error : An exception of type 'System.Data.SqlClient.SqlException' occurred in System.Data.dll but was not handled in user code Additional information: Incorrect syntax near the keyword 'DESC'
    – Zaki
    Aug 14, 2015 at 9:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.