Click here to Skip to main content
15,891,136 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Good day all!!! I have a form where I'm entering the connection details to connect to an MSSql database. This is what I have done...

C#
private void sbTest_Click(object sender, EventArgs e)
        {
            if (!dxvpConnectionWizard.Validate())
                return;

            TaskScheduler uiScheduler = TaskScheduler.FromCurrentSynchronizationContext();
            CancellationTokenSource tokenSource = new CancellationTokenSource();
            CancellationToken token = tokenSource.Token;

            this.Cursor = Cursors.AppStarting;

            Task connectionTestTask = Task.Factory.StartNew(() =>
            {
                bsiConnWizardStatus.Glyph = ClaimCatch_RIS.Properties.Resources.loading;
                bsiConnWizardStatus.Caption = "Attempting to connect to database...";

                using (SqlConnection connection = new SqlConnection(GetDataBaseConnectionString()))
                {
                    try
                    {
                        connection.Open();
                        connection.Close();
                    }
                    catch// (Exception ex)
                    {
                        tokenSource.Cancel(true);
                    }
                }
            }, token).ContinueWith(result =>
            {
                this.Cursor = Cursors.Default;
                
                if (token.IsCancellationRequested)
                {
                    bsiConnWizardStatus.Glyph = ClaimCatch_RIS.Properties.Resources.warning_16x16;
                    bsiConnWizardStatus.Caption = ServerModeStrings.failedDatabaseConnection;
                }
                else
                {
                    bsiConnWizardStatus.Glyph = ClaimCatch_RIS.Properties.Resources.apply_16x16;
                    bsiConnWizardStatus.Caption = "Ready...";
                }
            }, CancellationToken.None, TaskContinuationOptions.None, TaskScheduler.FromCurrentSynchronizationContext());
        }


But it's not exactly what I want. I actually want is to be able catch the error from the database and also get the task status.

How would I go about doing that...

Thanx in advance...
Posted
Updated 25-Sep-13 12:43pm
v2

1 solution

Hi Azinyama,
You can use
C#
System.Data.SqlClient.SqlException
class to catch exception from Database.
Visit this link
http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlexception.aspx[^]
--
Thanks
 
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