HERE IS MY INSERT BUTTON CODE
private void btn_save_Click_1(object sender, EventArgs e)
{
string dt = Convert.ToDateTime(dateTimePicker1.Text.ToString()).ToShortDateString();
int polfnum = 0;
con.Close();
try
{
string query = "SELECT COUNT(polf_number) FROM polf_details";
con.Open();
OleDbCommand cmd = new OleDbCommand(query, con);
polfnum = (int)cmd.ExecuteScalar();
polfnum = polfnum - 1;
cb_polf_number.Items.Add(polfnum);
con.Close();
}
catch (Exception ex)
{
MessageBox.Show("Error generating new number.\nError: " + ex.Message + "","ERROR");
return;
}
string cust_name = cb_cust_name.Text.ToString();
string circle = txt_circle.Text.ToString();
int quantity = Convert.ToInt32(txt_qty.Text);
string ponum = txt_po_num.Text.ToString();
string artwrk = txt_artwork_name.Text.ToString();
string moduletype = txt_module_type.Text.ToString();
string product = txt_prod_name.Text.ToString();
string SQLString = "INSERT INTO polf_details(polf_number, polf_date, customer_name, circle, quantity, po_number, artwork_no, module_type, product_type) VALUES('" + polfnum + "','" + dt + "','" + cust_name + "','" + circle + "', " + quantity + ",'" + ponum + "','" + artwrk + "','" + moduletype + "','" + product + "');";
con.Open();
OleDbCommand cBuilder = new OleDbCommand(SQLString,con);
int response = -1;
try
{
response = cBuilder.ExecuteNonQuery();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
if (response >= 1) MessageBox.Show("Entry is added to database", "Successful", MessageBoxButtons.OK, MessageBoxIcon.Information);
txt_circle.Clear();
txt_qty.Clear();
txt_po_num.Clear();
txt_artwork_name.Clear();
txt_module_type.Clear();
txt_prod_name.Clear();
cb_cust_name.ResetText();
cb_polf_number.ResetText();
datasaved = true;
btn_new_customer.Enabled = true;
btn_new_polf.Enabled = true;
btn_save.Enabled = false;
con.Close();
}
}
AND HERE IS MY UPDATE EVENT CODE
private void btn_update_Click(object sender, EventArgs e)
{
polf_details f1 = new polf_details();
f1.con.Close();
int polf_num=Convert.ToInt32( txt_polf_num_update.Text);
string artwrk=txt_artwork_update.Text.ToString();
string circle=txt_circle_update.Text.ToString();
string module_type=txt_module_update.Text.ToString();
string pono=txt_po_num_update.Text.ToString();
string prod_type=txt_prod_name_update.Text.ToString();
string cust_name=cb_customer_name_update.Text.ToString();
int qty=Convert.ToInt32(txt_quantity_update.Text);
string dt = Convert.ToDateTime(dateTimePicker_update.Text.ToString()).ToShortDateString();
string SQLUpdateString = "UPDATE polf_details SET [polf_date] = @dt, [customer_name] = @cust_name, [circle]= @circle, [quantity]= @qty, [po_number]= @pono, [artwork_no]= @artwrk, [module_type]= @module_type, [product_type]=@prod_type WHERE [polf_number]= '"+ txt_po_num_update.Text+"'";
OleDbCommand SQLCommand = new OleDbCommand();
SQLCommand.CommandText = SQLUpdateString;
SQLCommand.Parameters.AddWithValue("@dt", dt);
SQLCommand.Parameters.AddWithValue("@cust_name", cust_name);
SQLCommand.Parameters.AddWithValue("@circle", circle);
SQLCommand.Parameters.AddWithValue("@quantity", qty);
SQLCommand.Parameters.AddWithValue("@pono", pono);
SQLCommand.Parameters.AddWithValue("@artwrk", artwrk);
SQLCommand.Parameters.AddWithValue("@module_type", module_type);
SQLCommand.Parameters.AddWithValue("@prod_type", prod_type);
SQLCommand.Connection = f1.con;
f1.con.Open();
int response = SQLCommand.ExecuteNonQuery();
if (response>=1) MessageBox.Show("Update successful!", "Message", MessageBoxButtons.OK, MessageBoxIcon.Information);
Close();