Oh, good grief...
The first thing to do is sort out your indentation, so you can see what goes where. That's pretty easy: in VS, hold down CTRL and press K then D. If nothing happens, fix your compilation errors first, then do it again. But... this isn't going to work without some serious thinking on your part first.
You have a foreach loop working on
dataGridView2.Rows
to loop through each row. Inside the loop, you use:
this.dataGridView2.Rows.Clear();
Which is not allowed - you can't change a list of anything inside a loop which is running on it!
What I would suggest is that you do a number of things:
1) Back up this version.
2) Throw it away. It's confused, wrong and badly layid out, and shows too many signs of being hacked because it didn't quite work, but nearly...
3) Start again, and do it in a modular fashion.
3.1) Stop using
Convert.ToDouble
to read your textbox values. If your user makes a tiny mistake, your program will crash. Instead, either use a NumericUpDown control which won't let them make a mistake and provides a value directly, or use Double.TryParse instead. That will return a "It worked" / "it didn't work" bool so you can report problems to your user instead of crashing.
3.2) Change your initial if:
double amtpd, ttalamt, change;
if (!Double.TryParse(pos_amtpd.Text, out amtpd) || !Double.TryParse(txtTotalAmt.Text, out ttalamt))
{
MessageBox.Show("Please enter numbers!");
return;
}
if (amtpd < ttalamt)
{
MessageBox.Show("Insufficient payment!");
return;
}
Now the rest of your code doesn't have to worry about bad values at all.
3.3) Add your loop, but don't fill it in yet - just leave placeholders for the content:
foreach (DataGridViewRow data in dataGridView2.Rows)
{
if (!data.IsNewRow)
{
}
}
3.4) After the loop, clear out the data as required ready for next time.
MessageBox.Show("Transaction Saved!");
pos_amtpd.Clear();
pos_chnge.Clear();
txtTotalAmt.Clear();
textBox1.Clear();
textBox2.Clear();
textBox3.Clear();
textBox5.Clear();
srch_prdcod.Clear();
srch_prddesc.Clear();
dataGridView1.DataSource = null;
dataGridView1.Rows.Clear();
dataGridView2.Rows.Clear();
Now compile it. Fix any errors.
Now you are ready to work on the inner bit you left: which is pretty simple, if you just look at it.
I'm not going to do it for you now - I want you to think about it, and sort a few things out first (it's your homework, after all!)
But a couple of pointers:
1) You are correctly using parameters for some of your values. So what the heck did you decide to concatenate strings for the really dangerous ones? Never, ever concatenate textbox contents into SQL commands - it is the easiest way to introduce an SQL Injection attack and destroy your database.
2) Why would an INSERT command return any records? So what do you need an SqlDataReader for? Do you need to use ExecuteReader? Or is there a better method?
3) Please don't mix variable styles, and don't abbreviate names. Change your textBox1, textBox2, dataGridView1, etc. to names which describe what they do... Use camelCase instead of underlines - it is expected in C# applications!
Try that lot, and see where you get: then show me and we'll move on a bit, OK?