Click here to Skip to main content
15,881,248 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello!

I can't save to my MySql database, when I try to save it I get the error "Fatal error encountered during command execution."

and I think it's because of this line this.preemTableAdapter.Fill(this.dataset.preem)

I got 2 errors here and I don't know how to fix it!

If anyone can help me it would be much appreciated.

I'm new to programming so I'm really bad at this!

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



namespace training2
{
    public partial class Form1 : Form
    {
        MySql hello = new MySql();
        public Form1()
        {
            InitializeComponent();
        }
        private void ImportTableGrid_Click(object sender, EventArgs e)
        {
              hello.GetData(dataGridView1);
        }
         public class MySql
        {
            
             
             public MySqlConnection connection = new MySqlConnection("SERVER=localhost;DATABASE=deviaq;UID=root;PASSWORD=Qwerty123;");
             public MySqlDataAdapter adapter;
             public DataTable table = new DataTable();
             DataSet dataset = new DataSet();
             
 
            public void InitialiseDataAccessObjects()
            {


                this.adapter = new MySqlDataAdapter("SELECT  MOVEX_BsNr, Namn, Adress FROM preem", this.connection);
               
                
                //MySqlCommand delete = new MySqlCommand("DELETE FROM departments WHERE pa_code = @pc", this.connection);
                MySqlCommand insert = new MySqlCommand("INSERT INTO preem (MOVEX_BsNr, Namn, Adress) VALUES (@od, @f, @pa, @pc, @fa, @y )", this.connection);
                MySqlCommand update = new MySqlCommand("UPDATE preem SET MOVEX_BsNr = @od, Namn = @f, Adress = @pa", this.connection);
                MySqlCommand delete = new MySqlCommand("DELETE FROM preem WHERE Namn = @pc", this.connection);


                delete.Parameters.Add("@pc", MySqlDbType.Int32, 11, "MOVEX_BsNr");

                insert.Parameters.Add("@od", MySqlDbType.Text, 45, "Namn");
                insert.Parameters.Add("@f", MySqlDbType.Text, 45, "Adress");
                /*insert.Parameters.Add("@pa", MySqlDbType.VarChar, 45, "proj_act");
                insert.Parameters.Add("@pc", MySqlDbType.Int32, 4, "pa_code");
                insert.Parameters.Add("@fa", MySqlDbType.VarChar, 8, "fundspecial_acct");
                insert.Parameters.Add("@y", MySqlDbType.Year, 4, "year");
 
                update.Parameters.Add("@od", MySqlDbType.VarChar, 45, "office_dept");
                update.Parameters.Add("@f", MySqlDbType.VarChar, 45, "function");
                update.Parameters.Add("@pa", MySqlDbType.VarChar, 45, "proj_act");
                update.Parameters.Add("@pc", MySqlDbType.Int32, 4, "pa_code");
                update.Parameters.Add("@fa", MySqlDbType.VarChar, 45, "fundspecial_acct");
                update.Parameters.Add("@y", MySqlDbType.Year, 4, "year");*/
 
                
 
                
                this.adapter.DeleteCommand = delete;
                this.adapter.InsertCommand = insert;
                this.adapter.UpdateCommand = update;
 
                this.adapter.MissingSchemaAction = MissingSchemaAction.AddWithKey;
                
            }
        
         public void GetData(DataGridView MySQLl)
            {
                // Retrieve the data.

                
                adapter.Fill(table);
 
                MySQLl.DataSource = table;
                
 
                // The table can be used here to display and edit the data.
                // That will most likely involve data-binding but that is not a data access issue.
            }
 
            public void excel(DataGridView datagrid)
            {
                OpenFileDialog ofd = new OpenFileDialog();
 
                
 
                if (ofd.ShowDialog() == DialogResult.OK)
                {
 
                    string path = System.IO.Path.GetFullPath(ofd.FileName);
 
                    string querry = "SELECT * FROM [Sheet1$]";
 
                    OleDbConnection conn = new OleDbConnection();
 
                    conn.ConnectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source = '" + path + "'" + @";Extended Properties=""Excel 8.0;HDR=YES;IMEX=1;ImportMixedTypes=Text;TypeGuessRows=0""";
 
                    OleDbDataAdapter hej = new OleDbDataAdapter(querry, conn);
                    

 
                    hej.AcceptChangesDuringFill = false;
 
                    
                    hej.Fill(table);
 
                    
                    datagrid.DataSource = table;
 
                }
 
                else
                {
 
                    ofd.Dispose();
                }
            }
 
            
            public void SaveData()
            {
                // Save the data.
                this.adapter.Update(this.table);
                
            }
        }
        private void Form1_Load(object sender, EventArgs e)
        {
           
            // TODO: This line of code loads data into the 'mbmsDataSet.departments' table. You can move, or remove it, as needed.
           
            this.preemTableAdapter.Fill(this.dataset.preem);
           
            hello.InitialiseDataAccessObjects();
            
        }

       

        private void OpenExcelFile_Click(object sender, EventArgs e)
        {
             hello.excel(dataGridView1);  
        }

        private void SaveToTable_Click(object sender, EventArgs e)
        {
            hello.SaveData();
        }

        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {

        }

        public object dataSet { get; set; }
    }
}</<pre></pre>
Posted
Updated 23-Mar-15 0:50am
v2
Comments
Sinisa Hajnal 23-Mar-15 7:10am    
You get two errors. Which ones? At which line? Winforms or web or WPF?

You have more parameters in insert then you have columns which is probably the problem.

Or maybe preemTableAdapter doesn't exist (at least in this piece of code)...

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