Click here to Skip to main content
15,887,596 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
I want to move rezomekh.aspx page by choosing a record of gridview in C# codeو and i get error on this line:

dr = cmd.ExecuteReader();


and error text is:

Incorrect syntax near the keyword 'from'.
 Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
 Exception Details: System.Data.SqlClient.SqlException: Incorrect syntax near the keyword 'from'.


What I have tried:

C#
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
    string usnam = e.CommandArgument.ToString();
    try
    {
        if (e.CommandName == "rezome")
        {
            string query = "select from Ordertb where Username=@username";

            SqlCommand cmd = new SqlCommand(query, con);
            cmd.Parameters.AddWithValue("@username", usnam);

            con.Open();
            SqlDataReader dr;
            dr = cmd.ExecuteReader();
            dr.Read();
            Session["usernamekh"] = dr["Username"].ToString();
            //ViewState["userId"] = usId;
            cmd.ExecuteNonQuery();
            con.Close();
            Response.Redirect("rezomekh.aspx");
        }

    }
    finally
    {
        con.Close();
    }
}
Posted
Updated 21-Aug-17 2:43am
v2

Look at your query:
C#
string query = "select from Ordertb where Username=@username";
There is no indication of which columns to return. List them in the order you want them:
C#
string query = "select MyColumn1, MyColumn2 from Ordertb where Username=@username";
Or return them all (not recommended):
C#
string query = "select * from Ordertb where Username=@username";
 
Share this answer
 
the query
string query = "select from Ordertb where Username=@username";
seems wrong, if its not a typo, it should be "Select <column name> from "
 
Share this answer
 

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