Click here to Skip to main content
15,891,777 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
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 System.Data.OleDb;

namespace ACCESS_DATABASE
{
    public partial class DailyMaterial : Form
    {
        OleDbConnection con = new OleDbConnection();
        public DailyMaterial()
        {
            InitializeComponent();
        }

        private void DailyMaterial_Load(object sender, EventArgs e)
        {
            try
            { 
                con.ConnectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\Carlos\Documents\Office.accdb;
Persist Security Info=False;";
                con.Open();
              label68.Text = "Connection successful";
                con.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show("ERROR: "+ex);
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                con.Open();
                OleDbCommand cmd = new OleDbCommand();
                cmd.Connection = con;
                cmd.CommandText = "insert into DailyMaterial (Dat,Milk Powder,Corn Flour,Flour,Ammonium Bi Carbonate,Sodium Bi Carbonate,"+
                "Baking Powder,Acid Sodium Pyro Phospate,Calcium Carbonate,Coca POwder Normal,Coca Powder Black,Glucose Powder,Sweet Cocnut,"+
                "Ethyl Vanillin,Citric Acid,Caramel,Lecithin,Gulucose Lquid,Sugar,Salt,Vegetable Fat)"
  +"values('"+dateTimePicker1.Value+"','"+textBox1.Text+"','"+textBox2.Text + "','"+ textBox3.Text +"','"+ textBox4.Text + "','" + textBox5.Text + "','" + textBox6.Text + "','" + textBox7.Text + "','" + textBox8.Text + "','" + textBox9.Text + "','" + textBox10.Text + "','" + textBox11.Text + "','" + textBox12.Text + "','" + textBox13.Text + "','" + textBox14.Text + "','" + textBox15.Text + "','" + textBox16.Text + "','" + textBox17.Text + "','" + textBox18.Text + "','" + textBox19.Text + "','" + textBox20.Text + "',
       cmd.ExecuteNonQuery();
                con.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show("ERROR: "+ex);
            }
        }

Now I'm Getting The ERROR:"Syntax error in INSERT INTO statement."

What I have tried:

I have tried everything from my side what i can do but no i'm fedup of this problem :@
Posted
Updated 22-May-16 0:11am
v3

Don't do that.
Never concatenate strings to build a SQL command. It leaves you wide open to accidental or deliberate SQL Injection attack which can destroy your entire database. Use Parametrized queries instead.
And if you want column names that contain spaces, you need to quote them:
SQL
INSERT INTO DailyMaterial (Dat, `Milk Powder`, `Corn Flour`, ...

Your problem will disappear at the same time...
C#
using (SqlConnection con = new SqlConnection(strConnect))
    {
    con.Open();
    using (SqlCommand cmd = new SqlCommand("INSERT INTO myTable (myColumn1, myColumn2) VALUES (@C1, @C2)", con))
        {
        cmd.Parameters.AddWithValue("@C1", myValueForColumn1);
        cmd.Parameters.AddWithValue("@C2", myValueForColumn2);
        cmd.ExecuteNonQuery();
        }
    }


BTW: Do yourself a favour, and stop using Visual Studio default names for everything - you may remember that "TextBox8" is the mobile number today, but when you have to modify it in three weeks time, will you then? Use descriptive names - "tbMobileNo" for example - and your code becomes easier to read, more self documenting, easier to maintain - and surprisingly quicker to code because Intellisense can get to to "tbMobile" in three keystrokes, where "TextBox8" takes thinking about and 8 keystrokes...
 
Share this answer
 
As I understand it, your code is wrong
C#
...
textBox18.Text + "','" + textBox19.Text + "','" + textBox20.Text + "',
       cmd.ExecuteNonQuery();

I think you forgot the end of INSERT INTO statement.
C#
...
textBox18.Text + "','" + textBox19.Text + "','" + textBox20.Text + "'");
       cmd.ExecuteNonQuery();


Even if this correct this code, you absolutely need to pay attention to code injection as stated in all solutions.
 
Share this answer
 
Comments
George Jonsson 22-May-16 6:55am    
You were probably the only one reading that slab of code to the end. :P
Patrice T 22-May-16 7:09am    
May be :)
First of all, you should learn to use parameterized queries. The benefits are, for example:
1. Remove the risk of SQL injection attacks.
2. Makes the code easier to read and easier to debug.
3. You can set the type of your parameter, especially good for DateTime variables.

OleDbCommand.Parameters Property (System.Data.OleDb)[^]

c# - using parameters inserting data into access database - Stack Overflow[^]

That said I have air some concern about the number of columns you have and their specific names.
Wouldn't it be better to have two tables, one called DailyMaterials and one called Ingredients?

DailyMaterial
Id   Date         Description
1    2016-05-21   Some description if necessary


Ingredients
Id   IngredientName   Amount   DailyMaterialId
1    Milk Powder      30 g     1
2    Corn Flour       200 g    1
etc
 
Share this answer
 
try this

C#
private void button1_Click(object sender, EventArgs e)
       {
           try
           {
               con.Open();
               OleDbCommand cmd = new OleDbCommand();
               cmd.Connection = con;

               string sql = "insert into DailyMaterial (Dat,[Milk Powder],[Corn Flour],Flour,[Ammonium Bi Carbonate],[Sodium Bi Carbonate]," +
               "[Baking Powder],[Acid Sodium Pyro Phospate],[Calcium Carbonate],[Coca POwder Normal],[Coca Powder Black],[Glucose Powder],[Sweet Cocnut]," +
               "[Ethyl Vanillin],[Citric Acid],Caramel,Lecithin,[Gulucose Lquid],Sugar,Salt,[Vegetable Fat]) values ( " +
               "@Dat,@Milk_Powder,@Corn_Flour,@Flour,@Ammonium_Bi_Carbonate,@Sodium_Bi_Carbonate,@Baking_Powder,@Acid_Sodium_Pyro_Phospate,@Calcium_Carbonate,@Coca_POwder_Normal,@Coca_Powder_Black,@Glucose_Powder,@Sweet_Cocnut,@Ethyl_Vanillin,@Citric_Acid,@Caramel,@Lecithin,@Gulucose_Lquid,@Sugar,@Salt,@Vegetable_Fat)";
               cmd.CommandText = sql;
               cmd.Parameters.AddWithValue("@Dat", dateTimePicker1.Value);
               cmd.Parameters.AddWithValue("@Milk_Powder", textBox1.Text);
               cmd.Parameters.AddWithValue("@Corn_Flour", textBox2.Text);
               cmd.Parameters.AddWithValue("@Flour", textBox3.Text);
               cmd.Parameters.AddWithValue("@Ammonium_Bi_Carbonate", textBox4.Text);
               cmd.Parameters.AddWithValue("@Sodium_Bi_Carbonate", textBox5.Text);
               cmd.Parameters.AddWithValue("@Baking_Powder", textBox6.Text);
               cmd.Parameters.AddWithValue("@Acid_Sodium_Pyro_Phospate", textBox7.Text);
               cmd.Parameters.AddWithValue("@Calcium_Carbonate", textBox8.Text);
               cmd.Parameters.AddWithValue("@Coca_POwder_Normal", textBox9.Text);
               cmd.Parameters.AddWithValue("@Coca_Powder_Black", textBox10.Text);
               cmd.Parameters.AddWithValue("@Glucose_Powder", textBox11.Text);
               cmd.Parameters.AddWithValue("@Sweet_Cocnut", textBox12.Text);
               cmd.Parameters.AddWithValue("@Ethyl_Vanillin", textBox13.Text);
               cmd.Parameters.AddWithValue("@Citric_Acid", textBox14.Text);
               cmd.Parameters.AddWithValue("@Caramel", textBox15.Text);
               cmd.Parameters.AddWithValue("@Lecithin", textBox16.Text);
               cmd.Parameters.AddWithValue("@Gulucose_Lquid", textBox17.Text);
               cmd.Parameters.AddWithValue("@Sugar", textBox18.Text);
               cmd.Parameters.AddWithValue("@Salt", textBox19.Text);
               cmd.Parameters.AddWithValue("@Vegetable_Fat", textBox20.Text);
               cmd.ExecuteNonQuery();
               con.Close();
           }
           catch (Exception ex)
           {
               MessageBox.Show(ex.Message);
           }
       }



You should always use Parameterised query instead of concatenating the string values to form a query/statement, it will lead to SQL Injection[^] attacks.
 
Share this answer
 
Comments
Member 12528711 21-May-16 7:32am    
@KARTHIK By applying your suggestion's on my code now i am getting a error of "Data type mismatch in criteria expression." so what to do now?
Karthik_Mahalingam 21-May-16 7:36am    
what is the data type for Dat and other columns in your db ?
Member 12528711 21-May-16 9:06am    
@KARTHIK it's Date/Time
Karthik_Mahalingam 21-May-16 9:09am    
rest of the columns are of varchar/string type or numeric ?
Member 12528711 21-May-16 9:12am    
as my db is of Acess so other column's data type are "number"
Some more on SQL injection:

Your approach is wrong from the very beginning. The query composed by concatenation with strings taken from UI. Not only repeated string concatenation is inefficient (because strings are immutable; do I have to explain why it makes repeated concatenation bad?), but there is way more important issue: it opens the doors to a well-known exploit called SQL injection.

This is how it works: http://xkcd.com/327.

Are you getting the idea? The string taken from a control can be anything, including… a fragment of SQL code.

What to do? Just read about this problem and the main remedy: parametrized statements: http://en.wikipedia.org/wiki/SQL_injection.

With ADO.NET, use this: http://msdn.microsoft.com/en-us/library/ff648339.aspx.

Please see my past answers for some more detail:
EROR IN UPATE in com.ExecuteNonQuery();,
hi name is not displaying in name?.

—SA
 
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