Sk Asraf Sk Asraf - 3 months ago 6
MySQL Question

mysql, Asp.net error when insert value

i cant find out where my error please help me any one.

when i am run my asp.net page then i am getting an exception like thet


:MySql.Data.MySqlClient.MySqlException (0x80004005): 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 'usage,details,comment)values( 4, 'Laptop', 'g.purpose', 'gfhg', '')' at line 1 at MySql.Data.MySqlClient.MySqlStream.ReadPacket() at MySql.Data.MySqlClient.NativeDriver.GetResult(Int32& affectedRow, Int64& insertedId) at


i am trying to find out error but i cant

my asp.net code is

MySqlConnection con = new MySqlConnection();
con.ConnectionString = WebConfigurationManager.ConnectionStrings["mysql_con"].ConnectionString;

try
{
string query2 = "INSERT INTO m_prod_type(com_id,name,usage,details,comment)" +
"values( @com_id, @name, @usage, @details, @comment)";
MySqlCommand cmd = new MySqlCommand(query2, con);
cmd.Parameters.AddWithValue("@com_id",companyid);
cmd.Parameters.AddWithValue("@name", name);
cmd.Parameters.AddWithValue("@usage", uses);
cmd.Parameters.AddWithValue("@details", details);
cmd.Parameters.AddWithValue("@comment", comment);
con.Open();
// cmd.Transaction = tran;
cmd.ExecuteNonQuery();
// tran.Commit();
// isSucess = true;
con.Close();


//SubmitData();
}


plese help me.

Answer

In MySQL, usage is a reserved word (see the list here).

So, you need to escape it:

INSERT INTO m_prod_type(com_id, name, `usage`, details, comment)" +
            "values( @com_id, @name, @usage, @details, @comment)

I would also advise you to specify the type explicitly, rather than using AddWithValue(). That function can cause problems.