Khan
Khan

Reputation: 97

Stop Execution after a specific Value Got

I have a function Like FetchData(query) in class Name InsertAndFetch:

public static FetchData(query)
{
    da = new SqlDataAdapter();
    dt = new DataTable();
    dt.Clear();
    Connectivity.openconnection();
    cmd = new SqlCommand(query, Connectivity.cn);
    cmd.CommandType = CommandType.Text;
    da.SelectCommand = cmd;

    try
    {
        da.Fill(dt);
    }
    catch (SqlException e2)
    {
        MessageBox.Show(e2.Message);
    }
}

Call for the function from 'SaleOrder' Class.

InsertAndFetch.FetchData("Select CustName from Customer where CId='Cus_1'");
txtcustname.Text=InsertAndFetch.dt.Rows[0][0].ToString();

Here Suppose By Mistake In query, the column was Typed CId instead of Actual column 'CustId', In definition of FetchData, SQLException will be thrown that Column 'CId' is not a valid column.

Now I want that the control should stop execution after this exception(shouldn't exit the app) and not move back to the Call of the Function in SaleOrder Class as It will cause an error ' No row at Index 0' while assigning value to txtcustname.

Upvotes: 0

Views: 295

Answers (4)

V2Solutions - MS Team
V2Solutions - MS Team

Reputation: 1127

You can create your own Exception like

public class NewException : BaseException, ISerializable
{
    public NewException()
    {
        // Add implementation.
         // you can write code where you want to take your control after exception caught
    }
    public NewException(string message)
    {
        // Add implementation.
    }
}

and try to use it in your code

 try
    {
        da.Fill(dt);
    }
    catch (NewException e2)
    {
       throw e2
    }

then exception class function execute

Upvotes: 1

Srikanth
Srikanth

Reputation: 1010

Is this you want?

private bool canEnter=false;
private void TestFunc(int a)
{
    switch(a)
    {
         case(0):
         {
             Console.WriteLine("Zero");
             canExit=true;
             break;
         }
    }

    TestFunc(b);
    if(!canEnter)
    {
        HelloFunc(0);
        ThisFunc();
    }
}

Upvotes: 2

Shadow
Shadow

Reputation: 4016

What you want to do is make the function return upon reaching 0. Like so:

private void TestFunc(int a)
{
    switch(a)
    {
        case(0):
        {
            Console.WriteLine("Zero");
            return;
        }
    }
}

And just so you know return is suitable to replace break: c# switch statement is return suitable to replace break

Alternatively, if you don't want to return, you can throw an exception, which will exit your function. You can handle the exception to do nothing:

try
{
    private void TestFunc(int a)
    {
        switch(a)
        {
            case(0):
            {
                Console.WriteLine("Zero");
                throw new NotImplementedException();
            }
        }
    }
}
catch (NotImplementedException e)
{
    //Just do nothing here
}

Upvotes: 1

Nirav Kamani
Nirav Kamani

Reputation: 3272

You can use following code to terminate your application.

You can use following code for Console Application :-

Environment.Exit(0);

You can use following code for WinForm Application :-

Application.Exit();

Check out following links.

MSDN Link for Application.Exit() Method.

http://msdn.microsoft.com/en-us/library/ms157894%28v=vs.110%29.aspx

MSDN Link for Environment.Exit(0) Method.

msdn.microsoft.com/en-us/library/system.environment.exit(v=vs.110).aspx

It will give you the details explanation.

Upvotes: 3

Related Questions