Click here to Skip to main content
15,889,344 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Here Is my Code to check available data in table if there exist any one value of Gridviews Column it will return the available value.
But there is somthing wrong so why it check only one value which is first in gridview.
The Code is as below
ok Here is the Full Code...

C#
private void SureToDeleteBill() // Cheking For There Is No Imei in Bill Which Is Sold
        {
            
                try
                {
                    for (int i = 0; i <= dataGridView2.Rows.Count-1; i++)
                    {
                        // check either imei is sold or available in Imei Sold table
                        cs.Open();
                        cmd = new OleDbCommand("select * from tblsIMEI where IMEI = @a0", cs);
                        cmd.Parameters.AddWithValue("@a0", dataGridView2.Rows[i].Cells[1].Value.ToString());
                        dr = cmd.ExecuteReader();
                        if (dr.Read())
                        {
                            string msg;
                            msg = dr["IMEI"].ToString();
                            MessageBox.Show("Sorry !!\nYou Can Not DeleteBill !\n Sold IMEI :  ", "Sold IMEI", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
                            return;
                        }
                        else
                        {
                            DialogResult r = MessageBox.Show("Sure To Proceed?", "Delete Data", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
                            if (r == DialogResult.Yes)
                            {
                                DeleteFromtblPurchase();
                                DeleteFromtblIMEI();
                                DeleteFromPurchaseModel();
                                DeleteFromDealerBill();
                                MessageBox.Show("Record Delete Success!!", "Delete");
                                this.Close();
                            }
                            else
                            {
                                return;
                            }
                        }
                        cs.Close();
                    }
                    }
                
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message, "Sure To Delete");
                }
                finally
                {
                    cs.Close();
                }
        }

Please help to solve it.
Posted
Updated 12-Jan-15 4:39am
v4
Comments
ZurdoDev 12-Jan-15 8:44am    
What's the problem? You have it in a loop so it should process each row in dataGridView2.
Member Albert 12-Jan-15 8:55am    
No it isnt. It just take first rows value only.
ZurdoDev 12-Jan-15 9:05am    
All you have to do is step through the code and see what is happening. Should be pretty easy to figure out.
CHill60 12-Jan-15 8:44am    
First thing I notice when I tried to put the pre-tags in to format your code is that the braces {} don't seem to match up.
Secondly you're jumping out of the function with those return statements. Use the debugger to step through the code and you should be able to see your problem

Well...it isn't going to compile. Or work if it did.

Let's have a look at your control structures:
C#
try
{
    for (int i = 0; i <= dataGridView2.Rows.Count-1; i++)
    {
        if (dr.Read())
        {
        }
        else
        {
        }
        else
        {
        }
    }
}
}

catch (Exception ex)
{
}
finally
{
}
}

So, starting in the middle, you have this:
C#
if (a) A;
else B;
else C;
What is that? you can't have two else clause for the same if! That's wrong, and I'm not sure what you are trying to do there...

Ignore that for a moment, (or assume you fixed it) and the catch doesn't match with the try - you have a spurious "}" in there.

Now let's look at what happens if (by a miracle) it did compile and work.
You open the connection in the loop. Why? You also close it in the loop. And in the finally block...

And whichever way you go in the if the result is the same: return or close the form completely!

Frankly, this looks like you randomly grabbed lines of code from the internet and through them together, hoping it would magically work. And when it didn't, stuffed it up here in the hope we would fix it for you.
Development doesn't work like that: it's one of the few subjects you can study at school where you have to think about what you are trying to do. In that, it's a great introduction to the real world - but if you don't start to think here, you don;t end up flipping burgers by the end of term. You do in the real world...:laugh:
 
Share this answer
 
You should use
C#
continue; 
instead of
C#
return;
:-)
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900