0

I want to move rezomekh.aspx page by choosing a record of gridview in C# code, here is my code:

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();
    }
}

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'.
ponica
  • 11
  • 2
  • What is the value of `e.CommandArgument`.. It's not a valid int – Gilad Green Aug 21 '17 at 06:23
  • how can i find out the value? i was told it has userid of the record that i am choosing – ponica Aug 21 '17 at 06:25
  • Try using [Int.TryParse](https://msdn.microsoft.com/en-us/library/f02979c7(v=vs.110).aspx) instead. E.g. `int number; if (Int32.TryParse(e.CommandName, out number)) { //success, the answer is in "number" } else { //parse failed, handle this case }` – Keyur PATEL Aug 21 '17 at 06:25
  • @ponica - debug and see what you get – Gilad Green Aug 21 '17 at 06:26

0 Answers0