0
votes

i have the following C# code for database connection in VS2013, which when i run i get the following error message, how can i get around it.

private void Form1_Load(object sender, EventArgs e)
{
    //using (SqlConnection conn = new SqlConnection())
    //{
    try
    {
        //con = new System.Data.SqlServerCe.SqlCeConnection();
        conn = new System.Data.SqlClient.SqlConnection();
        conn.ConnectionString = "Data Source=(LocalDB)\v11.0;AttachDbFilename=C:\\vs2013projects\\WFDBApp\\WFDBApp\\Dbapp.mdf;Integrated Security=True";
        conn.Open();
        MessageBox.Show("Openned database well");
        conn.Close();
    }
    catch
    {
        MessageBox.Show("failed to connect");
        //conn.Close();
    }
    //}
}

error message:

'WFDBApp.vshost.exe' (CLR v4.0.30319: WFDBApp.vshost.exe): Loaded 'c:\vs2013projects\WFDBApp\WFDBApp\bin\Debug\WFDBApp.exe'. Symbols loaded. 'WFDBApp.vshost.exe' (CLR v4.0.30319: WFDBApp.vshost.exe): Loaded 'C:\WINDOWS\Microsoft.Net\assembly\GAC_32\System.Transactions\v4.0_4.0.0.0__b77a5c561934e089\System.Transactions.dll'. Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled. 'WFDBApp.vshost.exe' (CLR v4.0.30319: WFDBApp.vshost.exe): Loaded 'C:\WINDOWS\Microsoft.Net\assembly\GAC_32\System.EnterpriseServices\v4.0_4.0.0.0__b03f5f7f11d50a3a\System.EnterpriseServices.dll'. Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled. 'WFDBApp.vshost.exe' (CLR v4.0.30319: WFDBApp.vshost.exe): Loaded 'C:\WINDOWS\Microsoft.Net\assembly\GAC_32\System.EnterpriseServices\v4.0_4.0.0.0__b03f5f7f11d50a3a\System.EnterpriseServices.Wrapper.dll'. Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.

A first chance exception of type 'System.Data.SqlClient.SqlException' occurred in System.Data.dll The thread 0x1788 has exited with code 259 (0x103). The thread 0x1428 has exited with code 259 (0x103). The program '[7048] WFDBApp.vshost.exe' has exited with code 0 (0x0).

1
To get a more specific error message try to catch SqlException and then return the Message of the error: catch(SqlException se){ Console.Writeline(se.Message); }Mnemonics
You have to see what that exception actually is. The fact that it's a SqlException is helpful, but you need at least the message to actually troubleshoot. Seems like you're unfamiliar with basic exception handling code, so I'd start with some try/catch tutorials, and go from there.Joe Enos
You have several mistakes; you should double the backslash in (localDB)\v11.0. You should remove the catch clause as that is discarding the error message and stack frame.Dour High Arch
There is no error here. A first-chance exception simply means that the exception was thrown. It appears to have been caught, so it is not a problem. In fact, it seems to have been thrown in System.Data, and caught there as well. @Mnemonics advice is incorrect. To get the full exception (if there is one), surround the code with try {/* code here */} catch (Exception ex){Console.WriteLine(ex.ToString()); Console.ReadLine();throw;} ex.Message only gets you the message portion, not stack trace and any inner exceptions.John Saunders
thank you so much i had overlooked the backslash, the connection was a success now after putting the two backslash at the (localDB)\v11.0chikwapuro

1 Answers

0
votes

The error is most likely caused by incorrect connection string. There are several canonical samples of connection string to SQL Server Express file .mdf (see MSDN pub: https://msdn.microsoft.com/en-us/library/jj653752%28v=vs.110%29.aspx):

Using localDB syntax:

<add name="ConnectionStringName"
    providerName="System.Data.SqlClient"
    connectionString="Data Source=(LocalDB)\v11.0;AttachDbFileName=|DataDirectory|\DatabaseFileName.mdf;InitialCatalog=DatabaseName;Integrated Security=True;MultipleActiveResultSets=True" />

<add name="ConnectionStringName"
    providerName="System.Data.SqlClient"
    connectionString="Server=(LocalDB)\v11.0;Initial File Name=|DataDirectory|\DatabaseFileName.mdf;Database=DatabaseName;Trusted_Connection=True;MultipleActiveResultSets=True" />

Other possible options are shown below:

<add name="ConnectionStringName"
    providerName="System.Data.SqlClient"
    connectionString="Data Source=.\SQLEXPRESS;Initial Catalog=DatabaseName;Integrated Security=True;MultipleActiveResultSets=True"/>

<add name="ConnectionStringName"
    providerName="System.Data.SqlClient"
    connectionString="Data Source=.\SQLEXPRESS;AttachDbFileName=|DataDirectory|\DatabaseFileName.mdf;Integrated Security=True;User Instance=True;MultipleActiveResultSets=True" />

Select the one pertinent to your case and make sure you are using correct syntax.

Also, keep in mind that Database (that file-based SQL Express DatabaseFileName.mdf) could be password-protected: if so, then you have to include this info into connection string (typically such connection string is stored in .config file for security purpose). In the regular code behind you should use a verbatim string starting with @ (in lieu of double backslash "\\").

Hope this may help. Best regards,