1

I have the following code in C#, for a interface with a DB. I introduce values in TextBoxes on the interface. No error, but when executed, data is not stored in the DB created with Microsoft Access 2010. I'll give the complete code, here. Thanks for answers!

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.OleDb;

namespace WindowsFormsApplication5
{
    public partial class Form1 : Form
    {   
        OleDbConnection con = new OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\\Users\\lumi\\Desktop\\Test_DataB.accdb");
 OleDbDataAdapter ad = new OleDbDataAdapter();
DataSet ds = new DataSet();


        public Form1()
        {
            InitializeComponent();
        }

        private void indexBindingNavigatorSaveItem_Click(object sender, EventArgs e)
        {
            this.Validate();
            this.indexBindingSource.EndEdit();
            this.tableAdapterManager.UpdateAll(this.test_DataBDataSet);

        }

        private void Form1_Load(object sender, EventArgs e)
        {
            // TODO: This line of code loads data into the 'test_DataBDataSet.Index' table. You can move, or remove it, as needed.
            this.indexTableAdapter.Fill(this.test_DataBDataSet.Index);

        }

        private void button1_Click(object sender, EventArgs e)
        {
               try
    {       con.Open();               
            ad.InsertCommand = new OleDbCommand("Insert @Birth_month, @Firstname, @Surname, @ID", con);
            ad.InsertCommand.Parameters.Add("@Birth_month", OleDbType.Numeric).Value = int.Parse(textBox1.Text);
            ad.InsertCommand.Parameters.Add("@Firstname", OleDbType.VarChar).Value = textBox2.Text.ToString();
            ad.InsertCommand.Parameters.Add("@Surname", OleDbType.VarChar).Value = textBox3.Text.ToString();
            ad.InsertCommand.Parameters.Add("@ID", OleDbType.Numeric).Value = int.Parse(textBox4.Text);
            ad.InsertCommand.ExecuteNonQuery();
            con.Close();
    }


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


        }
    }
}
2
  • 2
    What..? Your OleDbCommand looks not a valid sql statement. o.O Commented Feb 3, 2014 at 11:15
  • I translated it, sorry! Commented Feb 3, 2014 at 11:18

6 Answers 6

1

The correct syntax for an INSERT command is

INSERT INTO tablename VALUES(list of comma separated values or parameters)

So you query lacks of something very important, the table name where the insert should occur.
I am a bit perplexed that you don't get any error message from that.

ad.InsertCommand = new OleDbCommand(@"Insert INTO table 
                                      VALUES (@Birth_month, @Firstname, @Surname, @ID)", con);

And for the purpose of this query you could leave the OleDbDataAdapter out. It is not needed

OleDbCommand cmd = new OleDbCommand(query, con);
cmd.Parameters.AddWithValue(.....);
....
cmd.ExecuteNonQuery(); 

Keep in mind that this form of INSERT requires that you specify as parameters every value for the fields in the underlying table in the exact order of definition. If you have less parameters than columns then you need to specify a column list before the VALUES

INSERT INTO tablename (comma separated list of columns) VALUES (comma separated list of values)

By the way, this is the preferred method to follow. In particular this keeps you shielded by schema changes like adding a new nullable column that would break the first statement (the one without the column list)

Sign up to request clarification or add additional context in comments.

Comments

0

You can't parameterize your column names. You can parameterize your values.

Here the syntax of INSERT (Transact-SQL)

INSERT 
{
        [ TOP ( expression ) [ PERCENT ] ] 
        [ INTO ] 
        { <object> | rowset_function_limited 
          [ WITH ( <Table_Hint_Limited> [ ...n ] ) ]
        }
    {
        [ ( column_list ) ] 
        [ <OUTPUT Clause> ]
        { VALUES ( { DEFAULT | NULL | expression } [ ,...n ] ) [ ,...n     ] 
        | derived_table 
        | execute_statement
        | <dml_table_source>
        | DEFAULT VALUES 
        }
    }
}

For your case;

INSERT INTO YourTableName VALUES(@Birth_month, @Firstname, @Surname, @ID)

You should add column names if they are necessary.

Comments

0

Problem : Your INSERT INTO statement is not valid.

Syntax of INSERT INTO statement:

INSERT INTO [TableName]([col1],[col2],[coln]) values(@val1,@val2,@valn);

Solution : assuming your table name is UserInfo

con.Open();               
ad.InsertCommand = new OleDbCommand("INSERT INTO UserInfo values(@Birth_month, @Firstname, @Surname, @ID", con);   
ad.InsertCommand.Parameters.Add("@Birth_month", OleDbType.Numeric).Value = int.Parse(textBox1.Text);
ad.InsertCommand.Parameters.Add("@Firstname", OleDbType.VarChar).Value = textBox2.Text.ToString();
ad.InsertCommand.Parameters.Add("@Surname", OleDbType.VarChar).Value = textBox3.Text.ToString();
ad.InsertCommand.Parameters.Add("@ID", OleDbType.Numeric).Value = int.Parse(textBox4.Text);
ad.InsertCommand.ExecuteNonQuery();
con.Close();

Comments

0

Your Insert query seems wrong

 ad.InsertCommand = new OleDbCommand  Insert into tablename (Birth_month,Firstname,Surname,id) values (@Birth_month, @Firstname, @Surname, @ID)
 ad.InsertCommand.Parameters.Add("@Birth_month", OleDbType.Numeric).Value = int.Parse(textBox1.Text);
 ad.InsertCommand.Parameters.Add("@Firstname", OleDbType.VarChar).Value = textBox2.Text.ToString();
 ad.InsertCommand.Parameters.Add("@Surname", OleDbType.VarChar).Value = textBox3.Text.ToString();
 ad.InsertCommand.Parameters.Add("@ID", OleDbType.Numeric).Value = int.Parse(textBox4.Text);
 ad.InsertCommand.ExecuteNonQuery();
 con.Close();

Make sure your connection string like
Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\myFolder\myAccessFile.accdb;

Comments

0

try this code

  using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    using System.Data.OleDb;

    namespace WindowsFormsApplication5
    {
        public partial class Form1 : Form
        {
            OleDbConnection con = new OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\\Users\\lumi\\Desktop\\Test_DataB.accdb");
            OleDbDataAdapter ad = new OleDbDataAdapter();
            DataSet ds = new DataSet();


            public Form1()
            {
                InitializeComponent();
            }

            private void indexBindingNavigatorSaveItem_Click(object sender, EventArgs e)
            {
                this.Validate();
                this.indexBindingSource.EndEdit();
                this.tableAdapterManager.UpdateAll(this.test_DataBDataSet);

            }

            private void Form1_Load(object sender, EventArgs e)
            {
                // TODO: This line of code loads data into the 'test_DataBDataSet.Index' table. You can move, or remove it, as needed.
                this.indexTableAdapter.Fill(this.test_DataBDataSet.Index);

            }

            private void button1_Click(object sender, EventArgs e)
            {
                try
                {

               con.Open();  

                    SqlCeCommand insert_command = new SqlCeCommand("Luna_nasterii,Nume,Prenume,Nr_fisa) " + "VALUES (@Luna_nasterii, @Nume, @Prenume, @Nr_fisa)", con);

                    insert_command.Parameters.AddWithValue("@Luna_nasterii", OleDbType.Numeric).Value = int.Parse(textBox1.Text);
                    insert_command.Parameters.AddWithValue("@Nume",OleDbType.VarChar).Value = textBox2.Text.ToString();
                    insert_command.Parameters.AddWithValue("@Prenume",  OleDbType.VarChar).Value = textBox3.Text.ToString();
                    insert_command.Parameters.AddWithValue("@Nr_fisa",  OleDbType.Numeric).Value = int.Parse(textBox4.Text);


                    create_adapt.InsertCommand = insert_command;
                    insert_command.ExecuteNonQuery();
                    con.close();
                }


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


            }
        }
    }

1 Comment

Error: The type or namespace name 'SqlCeCommand' could not be found
0

Indeed, my first mistake was the syntax of the INSERT statement where I did not pay attention. But even correcting, it did not work. No data was stored in the DB. The problem was that the Access Data Base was not localized in the BIN folder of the project. When I moved it, all worked correctly. Thank you all for help!

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.