hi people! I badly need a help regarding this program.. I want to insert this two data in my database...
try
{
OleDbConnection conn = new OleDbConnection();
conn.ConnectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\lenovo\Desktop\C CONTRUCTS\C CONTRUCTS\CONSTRUCTION.accdb";
conn.Open();
OleDbCommand ccmd1 = new OleDbCommand("INSERT INTO [ORDER_DRIVER]([DELIVERY_NO],[DRIVER_ID],[STATUS])" + "VALUES(" + "'" + textBox1.Text + "'" + "," + "'" + comboBox4.Text + "'" + "," + "'" + textBox16.Text + "'" + ")", conn);
ccmd1.ExecuteNonQuery();
OleDbCommand ccmd2 = new OleDbCommand("INSERT INTO [ORDER_DRIVER]([DELIVERY_NO],[DRIVER_ID],[STATUS])" + "VALUES(" + "'" + textBox1.Text + "'" + "," + "'" + comboBox5.Text + "'" + "," + "'" + textBox17.Text + "'" + ")", conn);
ccmd2.ExecuteNonQuery();
conn.Close();
}
but is unable to do that.. what do you think was the problem?
the error was..
you cannot add or change a record because a related record is required in table DRIVER.
---accrdg to connection error. please help... anywasy this is the whole program of my save button. :)
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;
using System.Data.SqlClient;
namespace C_CONTRUCTS
{
public partial class ORDER : Form
{
public ORDER()
{
InitializeComponent();
}
private void ORDER_Load(object sender, EventArgs e)
{
try
{///opening of database
OleDbConnection conn = new OleDbConnection();
conn.ConnectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\lenovo\Desktop\C CONTRUCTS\C CONTRUCTS\CONSTRUCTION.accdb";
conn.Open();
MessageBox.Show("You are connected to the Database", "Data Connection");
}
catch (Exception x)
{
MessageBox.Show(x.GetBaseException().ToString(), "Error in Connection");
}
try
{////combobox of customer ID
OleDbConnection conn = new OleDbConnection();
conn.ConnectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\lenovo\Desktop\C CONTRUCTS\C CONTRUCTS\CONSTRUCTION.accdb";
conn.Open();
OleDbDataAdapter dac = new OleDbDataAdapter("SELECT * FROM CUSTOMER", conn);
DataTable ds = new DataTable();
dac.Fill(ds);
comboBox1.Text = "Please Select Customer ID";
for (int i = 1; i < ds.Rows.Count; i++)
{
comboBox1.Items.Add(ds.Rows[i]["CUSTOMER_ID"]);
}
conn.Close();
}
catch (Exception x)
{
MessageBox.Show(x.GetBaseException().ToString(), "Error in Connection");
}
try
{////combobox of product ID
OleDbConnection conn = new OleDbConnection();
conn.ConnectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\lenovo\Desktop\C CONTRUCTS\C CONTRUCTS\CONSTRUCTION.accdb";
conn.Open();
OleDbDataAdapter dap = new OleDbDataAdapter("SELECT * FROM PRODUCT", conn);
DataTable ds = new DataTable();
dap.Fill(ds);
comboBox2.Text = "Please Select Product ID";
for (int a = 1; a < ds.Rows.Count; a++)
{
comboBox2.Items.Add(ds.Rows[a]["PRODUCT_ID"]);
}
conn.Close();
}
catch (Exception x)
{
MessageBox.Show(x.GetBaseException().ToString(), "Error in Connection");
}
try
{///combobox of truck ID
OleDbConnection conn = new OleDbConnection();
conn.ConnectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\lenovo\Desktop\C CONTRUCTS\C CONTRUCTS\CONSTRUCTION.accdb";
conn.Open();
OleDbDataAdapter dat = new OleDbDataAdapter("SELECT * FROM TRUCK", conn);
DataTable ds = new DataTable();
dat.Fill(ds);
comboBox3.Text = "Please Select Truck ID";
for (int i = 1; i < ds.Rows.Count; i++)
{
comboBox3.Items.Add(ds.Rows[i]["TRUCK_ID"]);
}
conn.Close();
}
catch (Exception x)
{
MessageBox.Show(x.GetBaseException().ToString(), "Error in Connection");
}
try
{///combobox driver 1
OleDbConnection conn = new OleDbConnection();
conn.ConnectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\lenovo\Desktop\C CONTRUCTS\C CONTRUCTS\CONSTRUCTION.accdb";
conn.Open();
OleDbDataAdapter dad = new OleDbDataAdapter("SELECT * FROM DRIVER ", conn);
DataTable ds = new DataTable();
dad.Fill(ds);
comboBox4.Text = "Please Select Driver ID";
for (int i = 1; i < ds.Rows.Count; i++)
{
comboBox4.Items.Add(ds.Rows[i]["DRIVER_ID"]);
}
conn.Close();
}
catch (Exception x)
{
MessageBox.Show(x.GetBaseException().ToString(), "Error in Connection");
}
try
{///combobox driver2
OleDbConnection conn = new OleDbConnection();
conn.ConnectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\lenovo\Desktop\C CONTRUCTS\C CONTRUCTS\CONSTRUCTION.accdb";
conn.Open();
OleDbDataAdapter dadds = new OleDbDataAdapter("SELECT * FROM DRIVER ", conn);
DataTable ds = new DataTable();
dadds.Fill(ds);
comboBox5.Text = "Please Select Driver ID";
for (int i = 1; i < ds.Rows.Count; i++)
{
comboBox5.Items.Add(ds.Rows[i]["DRIVER_ID"]);
}
conn.Close();
}
catch (Exception x)
{
MessageBox.Show(x.GetBaseException().ToString(), "Error in Connection");
}
}
private void button6_Click(object sender, EventArgs e)
{
Form1 f = new Form1();
this.Hide();
f.ShowDialog();
this.Close();
}
private void comboBox1_SelectedIndexChanged_1(object sender, EventArgs e)
{
try
{
OleDbConnection conn = new OleDbConnection();
conn.ConnectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\lenovo\Desktop\C CONTRUCTS\C CONTRUCTS\CONSTRUCTION.accdb";
conn.Open();
OleDbDataAdapter dda = new OleDbDataAdapter("SELECT * FROM CUSTOMER WHERE CUSTOMER_ID=" + "'" + comboBox1.Text + "'", conn);
DataSet ddsa = new DataSet();
dda.Fill(ddsa);
dataGridView2.DataSource = ddsa.Tables[0].DefaultView;
DataView dv = (DataView)dataGridView2.DataSource;
if (dv.Count > 0)
{
textBox2.Text = dv[0]["CUST_COMPANY"].ToString();
textBox3.Text = dv[0]["CUST_FNAME"].ToString();
textBox4.Text = dv[0]["CUST_MNAME"].ToString();
textBox5.Text = dv[0]["CUST_LNAME"].ToString();
}
else
{
MessageBox.Show("No Reocrd hs been found", "Record Access");
}
conn.Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.GetBaseException().ToString(), "teka parang may mali?");
}
}
private void comboBox2_SelectedIndexChanged(object sender, EventArgs e)
{
try
{
OleDbConnection conn = new OleDbConnection();
conn.ConnectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\lenovo\Desktop\C CONTRUCTS\C CONTRUCTS\CONSTRUCTION.accdb";
conn.Open();
OleDbDataAdapter pdaa = new OleDbDataAdapter("SELECT * FROM PRODUCT WHERE PRODUCT_ID=" + "'" + comboBox2.Text + "'", conn);
DataSet pdss = new DataSet();
pdaa.Fill(pdss);
dataGridView2.DataSource = pdss.Tables[0].DefaultView;
DataView dv = (DataView)dataGridView2.DataSource;
if (dv.Count > 0)
{
textBox6.Text = dv[0]["PRODUCT_DESC"].ToString();
}
else
{
MessageBox.Show("No Reocrd hs been found", "Append");
}
conn.Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.GetBaseException().ToString(), "Append");
}
}
private void comboBox3_SelectedIndexChanged(object sender, EventArgs e)
{
try
{
OleDbConnection conn = new OleDbConnection();
conn.ConnectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\lenovo\Desktop\C CONTRUCTS\C CONTRUCTS\CONSTRUCTION.accdb";
conn.Open();
OleDbDataAdapter tdaa = new OleDbDataAdapter("SELECT * FROM TRUCK WHERE TRUCK_ID=" + "'" + comboBox3.Text + "'", conn);
DataSet tdss = new DataSet();
tdaa.Fill(tdss);
dataGridView2.DataSource = tdss.Tables[0].DefaultView;
DataView dv = (DataView)dataGridView2.DataSource;
if (dv.Count > 0)
{
textBox7.Text = dv[0]["TRUCK_TYPE"].ToString();
textBox8.Text = dv[0]["PLATE_NO"].ToString();
}
else
{
MessageBox.Show("No Reocrd hs been found", "Append");
}
conn.Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.GetBaseException().ToString(), "teka parang may mali?");
}
}
private void comboBox4_SelectedIndexChanged(object sender, EventArgs e)
{
}
private void comboBox4_SelectedIndexChanged_1(object sender, EventArgs e)
{
if (comboBox4.Text == comboBox5.Text)
{
MessageBox.Show("You already choose that driver!", "Choosing error!");
}
else
{
try
{
OleDbConnection conn = new OleDbConnection();
conn.ConnectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\lenovo\Desktop\C CONTRUCTS\C CONTRUCTS\CONSTRUCTION.accdb";
conn.Open();
OleDbDataAdapter dda = new OleDbDataAdapter("SELECT * FROM DRIVER WHERE DRIVER_ID=" + "'" + comboBox4.Text + "'", conn);
DataSet ddsa = new DataSet();
dda.Fill(ddsa);
dataGridView2.DataSource = ddsa.Tables[0].DefaultView;
DataView dv = (DataView)dataGridView2.DataSource;
if (dv.Count > 0)
{
textBox14.Text = dv[0]["DRIVER_FNAME"].ToString();
textBox15.Text = dv[0]["DRIVER_LNAME"].ToString();
}
else
{
MessageBox.Show("No Reocrd hs been found", "Record Access");
}
conn.Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.GetBaseException().ToString(), "teka parang may mali?");
}
}
}
private void comboBox5_SelectedIndexChanged(object sender, EventArgs e)
{
if (comboBox4.Text == comboBox5.Text)
{
MessageBox.Show("You already choose that driver!", "Choosing error!");
}
else
{
try
{
OleDbConnection conn = new OleDbConnection();
conn.ConnectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\lenovo\Desktop\C CONTRUCTS\C CONTRUCTS\CONSTRUCTION.accdb";
conn.Open();
OleDbDataAdapter dda = new OleDbDataAdapter("SELECT * FROM DRIVER WHERE DRIVER_ID=" + "'" + comboBox5.Text + "'", conn);
DataSet ddsa = new DataSet();
dda.Fill(ddsa);
dataGridView2.DataSource = ddsa.Tables[0].DefaultView;
DataView dv = (DataView)dataGridView2.DataSource;
if (dv.Count > 0)
{
textBox19.Text = dv[0]["DRIVER_FNAME"].ToString();
textBox18.Text = dv[0]["DRIVER_LNAME"].ToString();
}
else
{
MessageBox.Show("No Reocrd hs been found", "Record Access");
}
conn.Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.GetBaseException().ToString(), "teka parang may mali?");
}
}
}
private void button4_Click(object sender, EventArgs e)
{
}
private void button3_Click(object sender, EventArgs e)
{
try
{
OleDbConnection conn = new OleDbConnection();
conn.ConnectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\lenovo\Desktop\C CONTRUCTS\C CONTRUCTS\CONSTRUCTION.accdb";
conn.Open();
DateTime d = DateTime.Today;
OleDbCommand ccmd = new OleDbCommand("INSERT INTO [ORDER]([DELIVERY_NO],[CUSTOMER_ID],[ODER_INDATE],[TOTAL_AMOUNT],[DR_LENGTH],[DR_WIDTH],[DR_HEIGHT],[DR_RECEIVED_BY],[DR_STREET],[DR_CITY],[DR_ZIPCODE],[TRUCK_ID],[PRODUCT_ID],[PAYMENT_TYPE])" + "VALUES(" + "'" + textBox1.Text + "'" + "," + "'" + comboBox1.Text + "'" + "," + "'" + DateTime.Today + "'" + "," + "'" + textBox9.Text + "'" + "," + "'" + textBox11.Text + "'" + "," + "'" + textBox12.Text + "'" + "," + "'" + textBox10.Text + "'" + "," + "'" + textBox13.Text + "'" + "," + "'" + textBox22.Text + "'" + "," + "'" + textBox21.Text + "'" + "," + "'" + textBox20.Text + "'" + "," + "'" + comboBox3.Text + "'" + "," + "'" + comboBox2.Text + "'" + "," + "'" + textBox23.Text + "'" + ")", conn);
ccmd.ExecuteNonQuery();
MessageBox.Show("Order has been successfully saved. ", "Record Added");
textBox1.Clear();
textBox2.Clear();
textBox3.Clear();
textBox4.Clear();
textBox5.Clear();
textBox6.Clear();
textBox7.Clear();
textBox8.Clear();
textBox9.Clear();
textBox10.Clear();
textBox11.Clear();
textBox12.Clear();
textBox13.Clear();
textBox14.Clear();
textBox15.Clear();
textBox16.Clear();
textBox17.Clear();
textBox18.Clear();
textBox19.Clear();
textBox20.Clear();
textBox21.Clear();
textBox22.Clear();
textBox23.Clear();
comboBox1.Text = "Please Select Customer ID";
comboBox2.Text = "Please Select Product ID";
comboBox3.Text = "Please Select Truck ID";
comboBox4.Text = "Please Select Driver ID";
comboBox5.Text = "Please Select Driver ID";
conn.Close();
}
catch (Exception x)
{
MessageBox.Show(x.GetBaseException().ToString(), "Connection in Error");
}
////////////////
try
{
OleDbConnection conn = new OleDbConnection();
conn.ConnectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\lenovo\Desktop\C CONTRUCTS\C CONTRUCTS\CONSTRUCTION.accdb";
conn.Open();
OleDbCommand ccmd1 = new OleDbCommand("INSERT INTO [ORDER_DRIVER]([DELIVERY_NO],[DRIVER_ID],[STATUS])" + "VALUES(" + "'" + textBox1.Text + "'" + "," + "'" + comboBox4.Text + "'" + "," + "'" + textBox16.Text + "'" + ")", conn);
ccmd1.ExecuteNonQuery();
OleDbCommand ccmd2 = new OleDbCommand("INSERT INTO [ORDER_DRIVER]([DELIVERY_NO],[DRIVER_ID],[STATUS])" + "VALUES(" + "'" + textBox1.Text + "'" + "," + "'" + comboBox5.Text + "'" + "," + "'" + textBox17.Text + "'" + ")", conn);
ccmd2.ExecuteNonQuery();
conn.Close();
}
catch (Exception x)
{
MessageBox.Show(x.GetBaseException().ToString(), "Connection in Error");
}
}
private void button2_Click(object sender, EventArgs e)
{
MessageBox.Show("You may now Add another transaction :) ", "gora na!");
textBox1.Clear();
textBox2.Clear();
textBox3.Clear();
textBox4.Clear();
textBox5.Clear();
textBox6.Clear();
textBox7.Clear();
textBox8.Clear();
textBox9.Clear();
textBox10.Clear();
textBox11.Clear();
textBox12.Clear();
textBox13.Clear();
textBox14.Clear();
textBox15.Clear();
textBox16.Clear();
textBox17.Clear();
textBox18.Clear();
textBox19.Clear();
textBox20.Clear();
textBox21.Clear();
textBox22.Clear();
textBox23.Clear();
comboBox1.Text = "Please Select Customer ID";
comboBox2.Text = "Please Select Product ID";
comboBox3.Text = "Please Select Truck ID";
comboBox4.Text = "Please Select Driver ID";
comboBox5.Text = "Please Select Driver ID";
}
}
}