0

Am not able to fix the error below:

`"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 'When,Then) values( '79','WBT-CoE','gyj','yi','yi')' at line 1"` error.

Here's the code:

 protected void Button3_Click(object sender, EventArgs e){

        string MyconnectionString = "server=localhost;database=requirement_doc;Uid=t;Pwd=123;";
        MySqlConnection conn = new MySqlConnection(MyconnectionString);
        MySqlCommand cmd;

        DataTable dt1 = new DataTable();
        cmd = conn.CreateCommand();

        cmd.CommandText = "SELECT  Req_ID, Actor FROM UseCase where Req_ID='" + txtReqID.Text + "' AND Actor='" + DropDownList1.Text + "'";
                    MySqlDataAdapter da1 = new MySqlDataAdapter();
        da1.SelectCommand = cmd;
        da1.Fill(dt1);

        if (dt1.Rows.Count > 0)
        {

            Label1.Text = "Data already exist";

        }


        else
        {


            string sql = "INSERT INTO UseCase (Req_ID,Actor,Given,When,Then) values(  '" + txtReqID.Text + "','" + DropDownList1.Text + "','" + giventxt.Text + "','" + whentbl.Text + "','" + thentbl.Text + "')"; 

                          cmd.Connection = conn;
            cmd.CommandText = sql;
            conn.Open();



        }
        try
        {

            cmd.ExecuteNonQuery();
            Label1.Text = " Successfully saved";
        }

        catch (Exception ex)
        {
            throw new Exception(ex.Message);
        }
    }
    }
1

2 Answers 2

2

Surround When and then with `` because they are reserved names.

string sql = "INSERT INTO UseCase (Req_ID,Actor,Given,`When`,`Then`) values(  '" + txtReqID.Text + "','" + DropDownList1.Text + "','" + giventxt.Text + "','" + whentbl.Text + "','" + thentbl.Text + "')"; 
Sign up to request clarification or add additional context in comments.

Comments

1

When and Then are reserved names in MySQL. So if you use those as column names, you get that error.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.