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!
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 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");
this.adapter.DeleteCommand = delete;
this.adapter.InsertCommand = insert;
this.adapter.UpdateCommand = update;
this.adapter.MissingSchemaAction = MissingSchemaAction.AddWithKey;
}
public void GetData(DataGridView MySQLl)
{
adapter.Fill(table);
MySQLl.DataSource = table;
}
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()
{
this.adapter.Update(this.table);
}
}
private void Form1_Load(object sender, EventArgs e)
{
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>