N
NicolaiHoyer
Guest
Hi.
Im doing a project with a group of fellow students at Aalborg University - Denmark, where we have to code an IT-system. We are using C# and now we are stucked from further progression. Im working with a part of the system where an admninistator can handle different tables from a database. I can get the tables displayed in a datagrid by choosing which one by using a combobox.
Now the problem is that i cant figure out how make a save, delete, insert and clear/cancel buttons that can handle the specific selected table and update the right one, eventhough ive tried multiple soultions but none of them seems to work.
The DataSet contains 5 tables:
DataSet = p4DataSet.
The tables: customer_table, employee_table, shop_table, order_table and product_table.
Hope someone can help with a suggestion how to make this work.
This is the code i have so far:
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;
using MySql.Data;
namespace AdminFinal
{
public partial class Form1 : Form
{
MySqlDataAdapter daTableinfo;
DataSet dsTableinfo;
string connStr = "server=localhost;user=root;database=p4;port=3306;password=fordgt40;charset=latin1;";
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
{
if (comboBox1.SelectedItem == "Kunder")
{
MySqlConnection conn = new MySqlConnection(connStr);
try
{
label2.Text = "Connecting to MySQL...";
string sql = "SELECT * FROM customer_table";
daTableinfo = new MySqlDataAdapter(sql, conn);
MySqlCommandBuilder cb = new MySqlCommandBuilder(daTableinfo);
dsTableinfo = new DataSet();
daTableinfo.Fill(dsTableinfo, "customer_table");
dataGridView1.DataSource = dsTableinfo;
dataGridView1.DataMember = "customer_table";
}
catch (Exception ex)
{
label2.Text = ex.ToString();
}
}
else if (comboBox1.SelectedItem == "Ansatte")
{
MySqlConnection conn = new MySqlConnection(connStr);
try
{
label2.Text = "Connecting to MySQL...";
string sql = "SELECT * FROM employee_table";
daTableinfo = new MySqlDataAdapter(sql, conn);
MySqlCommandBuilder cb = new MySqlCommandBuilder(daTableinfo);
dsTableinfo = new DataSet();
daTableinfo.Fill(dsTableinfo, "employee_table");
dataGridView1.DataSource = dsTableinfo;
dataGridView1.DataMember = "employee_table";
}
catch (Exception ex)
{
label2.Text = ex.ToString();
}
}
else if (comboBox1.SelectedItem == "Produkter")
{
MySqlConnection conn = new MySqlConnection(connStr);
try
{
label2.Text = "Connecting to MySQL...";
string sql = "SELECT * FROM order_table";
daTableinfo = new MySqlDataAdapter(sql, conn);
MySqlCommandBuilder cb = new MySqlCommandBuilder(daTableinfo);
dsTableinfo = new DataSet();
daTableinfo.Fill(dsTableinfo, "order_table");
dataGridView1.DataSource = dsTableinfo;
dataGridView1.DataMember = "order_table";
}
catch (Exception ex)
{
label2.Text = ex.ToString();
}
}
else if (comboBox1.SelectedItem == "Ordre")
{
MySqlConnection conn = new MySqlConnection(connStr);
try
{
label2.Text = "Connecting to MySQL...";
string sql = "SELECT * FROM product_table";
daTableinfo = new MySqlDataAdapter(sql, conn);
MySqlCommandBuilder cb = new MySqlCommandBuilder(daTableinfo);
dsTableinfo = new DataSet();
daTableinfo.Fill(dsTableinfo, "product_table");
dataGridView1.DataSource = dsTableinfo;
dataGridView1.DataMember = "product_table";
}
catch (Exception ex)
{
label2.Text = ex.ToString();
}
}
else if (comboBox1.SelectedItem == "Butikker")
{
MySqlConnection conn = new MySqlConnection(connStr);
try
{
label2.Text = "Connecting to MySQL...";
string sql = "SELECT * FROM shop_table";
daTableinfo = new MySqlDataAdapter(sql, conn);
MySqlCommandBuilder cb = new MySqlCommandBuilder(daTableinfo);
dsTableinfo = new DataSet();
daTableinfo.Fill(dsTableinfo, "shop_table");
dataGridView1.DataSource = dsTableinfo;
dataGridView1.DataMember = "shop_table";
}
catch (Exception ex)
{
label2.Text = ex.ToString();
}
}
else
{
label2.Text = "Vælg en tabel at arbejde med...";
}
}
}
private void btnAdd_Click(object sender, EventArgs e)
{
this.p4DataSetBindingSource.AddNew();
}
private void btnSave_Click(object sender, EventArgs e)
{
this.Validate();
this.p4DataSetBindingSource.EndEdit();
// this.p4DataSet.UpdateAll(this.p4DataSet);
}
private void btnDelete_Click(object sender, EventArgs e)
{
this.p4DataSetBindingSource.RemoveCurrent();
}
}
}
Many thanks in advance.
Nicolai, Denmark
Continue reading...
Im doing a project with a group of fellow students at Aalborg University - Denmark, where we have to code an IT-system. We are using C# and now we are stucked from further progression. Im working with a part of the system where an admninistator can handle different tables from a database. I can get the tables displayed in a datagrid by choosing which one by using a combobox.
Now the problem is that i cant figure out how make a save, delete, insert and clear/cancel buttons that can handle the specific selected table and update the right one, eventhough ive tried multiple soultions but none of them seems to work.
The DataSet contains 5 tables:
DataSet = p4DataSet.
The tables: customer_table, employee_table, shop_table, order_table and product_table.
Hope someone can help with a suggestion how to make this work.
This is the code i have so far:
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;
using MySql.Data;
namespace AdminFinal
{
public partial class Form1 : Form
{
MySqlDataAdapter daTableinfo;
DataSet dsTableinfo;
string connStr = "server=localhost;user=root;database=p4;port=3306;password=fordgt40;charset=latin1;";
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
{
if (comboBox1.SelectedItem == "Kunder")
{
MySqlConnection conn = new MySqlConnection(connStr);
try
{
label2.Text = "Connecting to MySQL...";
string sql = "SELECT * FROM customer_table";
daTableinfo = new MySqlDataAdapter(sql, conn);
MySqlCommandBuilder cb = new MySqlCommandBuilder(daTableinfo);
dsTableinfo = new DataSet();
daTableinfo.Fill(dsTableinfo, "customer_table");
dataGridView1.DataSource = dsTableinfo;
dataGridView1.DataMember = "customer_table";
}
catch (Exception ex)
{
label2.Text = ex.ToString();
}
}
else if (comboBox1.SelectedItem == "Ansatte")
{
MySqlConnection conn = new MySqlConnection(connStr);
try
{
label2.Text = "Connecting to MySQL...";
string sql = "SELECT * FROM employee_table";
daTableinfo = new MySqlDataAdapter(sql, conn);
MySqlCommandBuilder cb = new MySqlCommandBuilder(daTableinfo);
dsTableinfo = new DataSet();
daTableinfo.Fill(dsTableinfo, "employee_table");
dataGridView1.DataSource = dsTableinfo;
dataGridView1.DataMember = "employee_table";
}
catch (Exception ex)
{
label2.Text = ex.ToString();
}
}
else if (comboBox1.SelectedItem == "Produkter")
{
MySqlConnection conn = new MySqlConnection(connStr);
try
{
label2.Text = "Connecting to MySQL...";
string sql = "SELECT * FROM order_table";
daTableinfo = new MySqlDataAdapter(sql, conn);
MySqlCommandBuilder cb = new MySqlCommandBuilder(daTableinfo);
dsTableinfo = new DataSet();
daTableinfo.Fill(dsTableinfo, "order_table");
dataGridView1.DataSource = dsTableinfo;
dataGridView1.DataMember = "order_table";
}
catch (Exception ex)
{
label2.Text = ex.ToString();
}
}
else if (comboBox1.SelectedItem == "Ordre")
{
MySqlConnection conn = new MySqlConnection(connStr);
try
{
label2.Text = "Connecting to MySQL...";
string sql = "SELECT * FROM product_table";
daTableinfo = new MySqlDataAdapter(sql, conn);
MySqlCommandBuilder cb = new MySqlCommandBuilder(daTableinfo);
dsTableinfo = new DataSet();
daTableinfo.Fill(dsTableinfo, "product_table");
dataGridView1.DataSource = dsTableinfo;
dataGridView1.DataMember = "product_table";
}
catch (Exception ex)
{
label2.Text = ex.ToString();
}
}
else if (comboBox1.SelectedItem == "Butikker")
{
MySqlConnection conn = new MySqlConnection(connStr);
try
{
label2.Text = "Connecting to MySQL...";
string sql = "SELECT * FROM shop_table";
daTableinfo = new MySqlDataAdapter(sql, conn);
MySqlCommandBuilder cb = new MySqlCommandBuilder(daTableinfo);
dsTableinfo = new DataSet();
daTableinfo.Fill(dsTableinfo, "shop_table");
dataGridView1.DataSource = dsTableinfo;
dataGridView1.DataMember = "shop_table";
}
catch (Exception ex)
{
label2.Text = ex.ToString();
}
}
else
{
label2.Text = "Vælg en tabel at arbejde med...";
}
}
}
private void btnAdd_Click(object sender, EventArgs e)
{
this.p4DataSetBindingSource.AddNew();
}
private void btnSave_Click(object sender, EventArgs e)
{
this.Validate();
this.p4DataSetBindingSource.EndEdit();
// this.p4DataSet.UpdateAll(this.p4DataSet);
}
private void btnDelete_Click(object sender, EventArgs e)
{
this.p4DataSetBindingSource.RemoveCurrent();
}
}
}
Many thanks in advance.
Nicolai, Denmark
Continue reading...