user9984958
user9984958

Reputation:

Skip all upcomimg lines of code but dont close the program

I have a C# program in which if the code in the else block is reached then it should display a message and must stop further execution of the code.

I have this code below, I have tried to use return but it is not working. The program continues onto the next function after else is executed.

if (((batch_sold - dqty_y) >= 0) && ((batch_left + dqty_y) <= batch_qty) && ((batch_left + dqty_y) >0))
{
    con.Open();
    SqlCommand command = new SqlCommand("update batch set sold_qty=sold_qty-@soldqty2, left_qty=left_qty+@soldqty2 where id=@id2", con);
    command.Parameters.AddWithValue("@soldqty2", Convert.ToInt32(dqty_y));
    command.Parameters.AddWithValue("@id2", Convert.ToInt32(k_batch));
    rexe = command.ExecuteNonQuery();
}
else
{
    check_qty = -1;
}

Upvotes: 1

Views: 94

Answers (2)

Arsalan
Arsalan

Reputation: 744

Hi you can put try catch block a level higher

private void Update()
{

           if (((batch_sold + dqty_y) <= batch_qty) && ((batch_left - dqty_y) >= 0))
            {
                SqlCommand command = new SqlCommand("update batch set sold_qty=sold_qty+@soldqty2, left_qty=left_qty-@soldqty2 where id=@id2", con);
                command.Parameters.AddWithValue("@soldqty2", Convert.ToInt32(dqty_y));
                command.Parameters.AddWithValue("@id2", Convert.ToInt32(k_batch));
                rexe = command.ExecuteNonQuery();
                            }
               else throw new Exception("Check Ordered Quantity");
}

in your main :

try{
    Update();
    Foo();
    Bar();
    .
    .
    .
}
catch(exception e)
{
     MessageBox.Show(e.Message);
     return;
}

Upvotes: 1

croth
croth

Reputation: 21

Put a

Console.ReadKey();

after

check_qty = -1;

That way, the program will wait until the user presses a key before proceeding with the execution.

Upvotes: 1

Related Questions