First of all sorry for my bad English. I am new in the world of programming. I have so many doubts about c# and its back ends. I googled about c# datagridview and mysql, and i found some codes. then i generate them in my own way. there is a problem. if there is one row in datagrid view then two times the data get stored in db. and also if there is 2 then 3 times. and so on... i think the problem with my for loop section. i want to solve this. i dont know how to do that. please help me friends. here is the code below.
private void cmdSaveOrPrint_Click(object sender, EventArgs e)
{
if (txtCustomerName.Text == "" || txtAddress.Text == "" || txtPhoneNumber.Text == "" || Convert.ToString(dgNewSale.Rows[0].Cells["dgItemName"].Value) == string.Empty || Convert.ToString(dgNewSale.Rows[0].Cells["dgQuantity"].Value) == string.Empty)
{
MessageBox.Show("Something Missing! Please Fill All The Required Sales Details", "Malabar Plywoods and Glass House", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
else if (txtCustomerName.Text != "" && txtAddress.Text != "" && txtPhoneNumber.Text != "" && Convert.ToString(dgNewSale.Rows[0].Cells["dgItemName"].Value) != string.Empty && Convert.ToString(dgNewSale.Rows[0].Cells["dgQuantity"].Value) != string.Empty)
{
int counter;
for (counter = 0; counter < (dgNewSale.Rows.Count); counter++)
{
int i;
mdiManager mdiTempManager = new mdiManager();
string host = "datasource=localhost;port=3306;username=root;password=";
string mysqlQueryInsertInto = "INSERT INTO db_malabar.sales_info (invoice_number, customer_name, address, customer_phone_number, date_of_purchase, time_of_purchase, item_name, item_code, unit, quantity, retail_unit_price, discount, total_amount) VALUES('" + this.txtInvoiceNumber.Text + "', '" + this.txtCustomerName.Text + "', '" + this.txtAddress.Text + "', '" + txtPhoneNumber.Text + "', '" + lblDateDisplay.Text + "', '" + lblTimeDisplay.Text + "', '" + Convert.ToString(dgNewSale.Rows[counter].Cells["dgItemName"].Value) + "','" + Convert.ToString(dgNewSale.Rows[counter].Cells["dgItemCode"].Value) + "', '" + Convert.ToString(dgNewSale.Rows[counter].Cells["dgUnit"].Value) + "', '" + Convert.ToString(dgNewSale.Rows[counter].Cells["dgQuantity"].Value) + "', '" + Convert.ToString(dgNewSale.Rows[counter].Cells["dgUnitPrice"].Value) + "', '" + txtDiscount.Text + "', '" + Convert.ToString(dgNewSale.Rows[counter].Cells["dgGrandTotal"].Value) + "') ;";
MySqlConnection rx = new MySqlConnection(host);
MySqlCommand condb = new MySqlCommand(mysqlQueryInsertInto, rx);
MySqlDataReader myReader;
try
{
rx.Open();
myReader = condb.ExecuteReader();
for (i = 1; i <= 1; i++)
{
MessageBox.Show("Sales Details Saved Successfully. Click OK to Print", "Malabar Plywoods and Glass House", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
while (myReader.Read())
{
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
rx.Close();
}
}
}