0

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.

Khan
  • 87
  • 2
  • 10
  • Is this a winforms app? a console app? a web app? If you're looking at just killing your thread mid process, then you ought to think about re-architecting your application to not do this. – crthompson Sep 15 '14 at 05:23
  • if you are not going back, where you want to go? directly exit the apps? if yes then Environment.Exit(); – ah_hau Sep 15 '14 at 05:23
  • @pagogomez its a WinForms App – Khan Sep 15 '14 at 05:26
  • @ah_hau I don't want to exit the app. Instead I just Stop all the execution in the function definition – Khan Sep 15 '14 at 05:27
  • the break; or return; should get what you want right? if not please re-post more code, I saw some bugs here-->HelloFunc(0; – ah_hau Sep 15 '14 at 05:29
  • You can return some value from this function and check the value in returned function if value is right then execute the code else not. So i mean you can simply use if condition. – Nirav Kamani Sep 15 '14 at 05:53
  • @NiravKamani I am calling this function from almost 30 places. I don't want to use extra 30 'if' conditions after each call – Khan Sep 15 '14 at 05:56

4 Answers4

3

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.

Nirav Kamani
  • 3,192
  • 7
  • 39
  • 68
  • I don't want to close the app, Instead as I m using a winform, so I want to see the Form alive where the function was called. But the Instructions after that function call shouldn't execute. And I don't want to use any Check after function Call, All I want is to do something in function Definition – Khan Sep 15 '14 at 05:30
  • You can use something like goto statement check out following link.http://msdn.microsoft.com/en-IN/library/13940fs2.aspx somewhat using goto statement in programming is not good. I think You should correct somewhat your structure. – Nirav Kamani Sep 15 '14 at 05:38
  • You can return some value from this function and check the value in returned function if value is right then execute the code else not. So i mean you can simply use if condition. – Nirav Kamani Sep 15 '14 at 05:54
2

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();
    }
}
Nirav Kamani
  • 3,192
  • 7
  • 39
  • 68
Srikanth
  • 980
  • 3
  • 16
  • 30
1

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
}
Community
  • 1
  • 1
Shadow
  • 3,926
  • 5
  • 20
  • 41
1

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