Click here to Skip to main content
15,914,165 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hello guys, how do i fix this kind of error, i used all kinds of insert query, but nothing is working, so i decided to coding again back to the beginning. and still getting this error, it's so very frustrating. anyone would like to help me?,

this message always prompt: "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 'Description = 'ink Cartridges', Item Code = '0012', Stock = 'System.Windows.Form.' at line 1.

C#
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using MySql.Data.MySqlClient;

namespace ToolManagementTest
{
    public partial class Form5 : Form
    {
        public Form5()
        {
            InitializeComponent();
        }

        public void btn1_Click(object sender, EventArgs e)
        {
            try
            {
            string modConnection = "datasource=localhost;port=3306;username=root;password=1234";
            string modQuery = "UPDATE toolsmanagement.itemlist SET Item Description = '" + this.modItemTxt.Text + "',Item Code = '" + this.modCodeTxt.Text + "',Stock = '" + this.modStockTxt + "',Unit = '" + this.modUnitTxt.Text + "',Item Type = '" + this.modTypeTxt.Text + "',Item Status = '" + this.modStatTxt.Text + "' WHERE Item Code = '" + this.modCodeTxt.Text + "'";

            MySqlConnection modData = new MySqlConnection(modConnection);
            MySqlCommand modCommandData = new MySqlCommand(modQuery, modData);
            MySqlDataReader MyReader;
         

            
                modData.Open();
       MyReader = modCommandData.ExecuteReader();                                  
                MessageBox.Show("Registered Data..");
                
                this.Hide();
                while (MyReader.Read())
                {

                }
                modCommandData.ExecuteNonQuery();
                modData.Close();

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

       
    }
}
Posted
Updated 4-Jun-15 19:10pm
v2

1 solution

change this.modStockTxt to this.modStockTxt.Text
other than that you better use parameters instead of concatenating strings to build sql statement, your application is widely open for sql attacks now.
sample sql statement with parameters:
SQL
UPDATE toolsmanagement.itemlist SET 
     `Item Description` = @ItemDescription,
     `Stock` = @Stock,
     `Unit` = @Unit,
     `Item Type`= @ItemType,
     `Item Status` = @ItemStatus 
WHERE 
     `Item Code` = @ItemCode 

Note that when you have space in column names, use ``
since you have update statement you don't need MySqlDataReader , create MySqlCommand using parameterized sql statement as above and then you can set each parameter values as below
C#
cmd.Parameters.AddWithValue("@ItemDescription", this.modItemTxt.Text);
// do the same for all other parameters

finally execute the command
C#
cmd.ExecuteNonQuery();
 
Share this answer
 
v2
Comments
Member 11707739 5-Jun-15 21:41pm    
my update query is now working and i also added parameters you've given,. thanks a lot!

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