Click here to Skip to main content
15,921,643 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Here I use Mysql with Asp.Net to find top 5 students.But I get the following error

SQL
conn1.Open();
                conn2.Open();
                string sql1 = "select TOP 5 `btechstudent_details`.Regd_no,Sname,Btech,Branch FROM btechstudent_details,btechstudent_academics WHERE `btechstudent_details`.Regd_no=`btechstudent_academics`.Regd_no ORDER BY Btech DESC;";
                MySqlCommand com1 = new MySqlCommand(sql1, conn1);
                MySqlDataAdapter da1 = new MySqlDataAdapter(com1);
                DataSet ds = new DataSet();
                da1.Fill(ds, "Regd_no");
                Show.DataSource = ds.Tables["Regd_no"];
                Show.DataBind();


ERROR-You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '5 `btechstudent_details`.Regd_no,Sname,Btech,Branch FROM btechstudent_details,bt' at line 1
Posted
Updated 16-Mar-15 2:07am
v3

onn1.Open();
conn2.Open();
string sql1 = "select TOP 5 btechstudent_details.Regd_no,Sname,Btech,Branch FROM btechstudent_details,btechstudent_academics WHERE btechstudent_details.Regd_no=btechstudent_academics.Regd_no ORDER BY Btech DESC;";
MySqlCommand com1 = new MySqlCommand(sql1, conn1);
MySqlDataAdapter da1 = new MySqlDataAdapter(com1);
DataSet ds = new DataSet();
da1.Fill(ds, "Regd_no");
Show.DataSource = ds.Tables["Regd_no"];
Show.DataBind();


Remove single quotes from SQL query.

Hope this will help.
 
Share this answer
 
Comments
CHill60 16-Mar-15 8:29am    
OP will still get error with TOP
As far as I know MySQL does not support the TOP clause. The equivalent is LIMIT

So the equivalent of
SQL
SELECT TOP 5 * FROM YourTable
is
SQL
SELECT * FROM YourTable LIMIT 0,5
 
Share this answer
 
Comments
TheRealSteveJudge 16-Mar-15 8:18am    
That's true. 5*

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



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