Click here to Skip to main content
15,891,704 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
public DataSet BindStates(string country)
       {
           StudentContext db = new StudentContext();
           SqlConnection con = new SqlConnection(@"Data Source=DE-SHREE-02;Initial Catalog=Abhijeet;Integrated Security=True");
           con.Open();
           SqlCommand cmd = new SqlCommand("Select * from StateTable where CountryName=" + country + "order by Id asc",con);
           cmd.CommandType = CommandType.Text;

           SqlDataAdapter da = new SqlDataAdapter(cmd);
           DataSet ds = new DataSet();
          da.Fill(ds);
           con.Close();
           return ds;

       }


What I have tried:

the error is at "da.Fill(ds);"
and giving error as {"Incorrect syntax near the keyword 'by'."}

i want to fetch data from one DropdownList and generate data as per selection in other dropdown list in MVC
Posted
Updated 16-Feb-17 22:24pm

well, first off, the exception actually says there's something wrong with your sql - do you have a column 'Id' for example - what happens if you remove the 'order by...' clause ?

secondly, thats a horrible way to write SQL statements - you should use parameterised queries, along the lines of

C#
using (SqlCommand command = new SqlCommand(
                "Select * from StateTable where CountryName=@countryname", con))
{
    //
    // Add new SqlParameter to the command.
    //
    command.Parameters.Add(new SqlParameter("countryname", country));
    cmd.CommandType = CommandType.Text;
            
    SqlDataAdapter da = new SqlDataAdapter(cmd);
    DataSet ds = new DataSet();
    da.Fill(ds);
    con.Close();
    return ds;
}
 
Share this answer
 
v2
Comments
Karthik_Mahalingam 17-Feb-17 2:33am    
5
Member 13006667 17-Feb-17 2:59am    
-yes I do have Column called as 'Id' in StateTable
-while execution it is showing the value of country is accepting correctly. but its not getting the value of "Id"
Garth J Lancaster 17-Feb-17 3:58am    
ok - not sure what you mean by 'but its not getting the value of "Id"' but we'll keep going

- your SQL doesnt have a space before your 'order by' clause if I see it correctly - so if you've re-written the better form of query as I suggested, and put back the 'order by', you'd have

"Select * from StateTable where CountryName=@countryname ORDER BY Id ASC"

note the space between @countryname and ORDER - without it, and using country = 'Australia', I think what your (original SQL) would do is this

"Select * from StateTable where CountryName=@countrynameorder by Id asc"

so, if you're using your original sql (bleh), put a space before the 'order' keyword -also have a look at https://msdn.microsoft.com/en-us/library/ms188385.aspx#BasicSyntax for the syntax
Member 13006667 17-Feb-17 4:08am    
Thank you sir for your help.
my problem is solved after implementing your solutions.
Thank You !
Garth J Lancaster 17-Feb-17 4:14am    
excellent - you can see why its harder if you 'concatenate' your SQL statement, hopefully - it makes it easier to introduce bugs
string should be like this '"+stringname+"' inside your query string.

public DataSet BindStates(string country)
        {
            StudentContext db = new StudentContext();
            SqlConnection con = new SqlConnection(@"Data Source=DE-SHREE-02;Initial Catalog=Abhijeet;Integrated Security=True");
            con.Open();
            SqlCommand cmd = new SqlCommand("Select * from StateTable where CountryName='" + country + "' order by Id asc",con);
            cmd.CommandType = CommandType.Text;
            
            SqlDataAdapter da = new SqlDataAdapter(cmd);
            DataSet ds = new DataSet();
           da.Fill(ds);
            con.Close();
            return ds;
            
        }
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900