locked
If connection string fails then wpf application not throwing any exception? RRS feed

  • Question

  • hi all,

                 I have a WPF application. in my App.config i have connection string.

    inside connectionstring Datasource(machinename) i put it wrongly. then i just run the application. i didn't get any output .

    here i am expecting exception in the page initialized event. i didn't get any exception eventhough i put try catch inside initialized event.(i am getting some whitescreen (progreesing)).

    what is the reason, how can i get the exception in this case??

    Monday, May 5, 2014 12:00 PM

Answers


  • Since you are setting the connection timeout to 100000 in your connection string, it will take 100000 seconds until the adapter.Fill method throws an SqlException. You should decrease this value:

    <add name="CONNECTIONSTRING" connectionString="Data Source=SIVA1;Initial Catalog=PCS;uid=PCS;pwd=PCS;timeout=15;"/>
    

    Tuesday, May 6, 2014 3:06 PM
  • Hi,

    Magnus’s point is correct. In default, the timeout is 15 seconds. If you set the timeout to 100000. You will get the exception after 100000 seconds. J

    For more information about Timeout property, see: http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlconnection.connectiontimeout(v=vs.110).aspx

    http://www.connectionstrings.com/all-sql-server-connection-string-keywords/

    The length of time (in seconds) to wait for a connection to the server before terminating the attempt and generating an error.

    Have a nice time!

    Sincerely,


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Wednesday, May 7, 2014 1:20 AM

All replies

  • In Application.Startup event (or in MainPage) you can validate whether connection string is correct or not. If invalid you can show a message notifying user about it.

    You can refer following links to validate connection string

    http://stackoverflow.com/questions/434864/how-to-check-if-connection-string-is-valid
    http://stackoverflow.com/questions/16171144/how-to-check-for-database-availability


    Gaurav Khanna | Microsoft VB.NET MVP | Microsoft Community Contributor

    Monday, May 5, 2014 6:11 PM
  • I am not sure when you say white screen progressing. It would be helpful if you send a screen shot of it. My only suggestion with this restricted information is to use debugger.

    Monday, May 5, 2014 6:48 PM
  • Hi,

           Here i have posted my sample shite screen.

    for ex: just try to connect sql server from your wpf application. with DataSource(machin name) wronly.

    other inputs like InitialCatalog, UserName,Password are correct.

    in that loading event try to open the SqlConnection. its not returning any issue. its tring to connect the database again and again.

    SqlConnection conn = new SqlConnection (connectionString)

    conn.open() --> this line is not returning any issue when the machine name is not correct.

    any ideas?

    i want to throw exception???

    Tuesday, May 6, 2014 6:23 AM
  • Hi,

    I tested connection string on my side in WPF application. If the connection string is invalid or incorrect. It will throw the exception when I try to open the connection.

    Here are my suggestions about this issue.

    Prerequisite: You should make sure the connection string is incorrect/invalid. If you not sure the connection string is correct or not, you can put it in the forum. To create a connection string correct, we can use VS IDE. Follow this way: Add the connection to Server Explorer, then right click on the connection and select properties. The Properties window shows the connection string.

    Here is the correct connection string: http://www.connectionstrings.com/sql-server/

    1. I am afraid that there is an issue regarding your development environment, such as VS or related. I suggest testing this project on another machine. If it works fine. We can make sure it’s VS IDE issue;

    2. Share the connection string and the relevant code for us. It will help us to figure out this issue;

    3. Handle all exception in WPF application, like this:

    using System.Windows; // Application 
    using System.Windows.Threading; // DispatcherUnhandledExceptionEventArgs 
    
    namespace SDKSample
    {
        public partial class App : Application
        {
            void App_DispatcherUnhandledException(object sender, DispatcherUnhandledExceptionEventArgs e)
            {
                // Process unhandled exception
                // Prevent default unhandled exception processing
                e.Handled = true;
            }
        }
    }

    For more information, visit: http://msdn.microsoft.com/en-us/library/system.windows.application.dispatcherunhandledexception.aspx

    4. The last idea. But it’s not a good idea, In VS IDE, DEBUG -> Exceptions… Select all types of exception.

    BTW, we cannot see your screenshot, it just shown the taskbar. Please upload a full screenshot.

    Have a nice time!

    Sincerely,


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Tuesday, May 6, 2014 8:43 AM
  • Hi,

          I have posted my sample screen only in my previous post. :) since i was telling that i am getting some whitescreen only(so you will get a task bar only)..

    anyway i will post my code

    App.config:

    <?xml version="1.0" encoding="utf-8" ?>
    <configuration>
        <startup> 
            <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
        </startup>
      <connectionStrings>
        <add name="CONNECTIONSTRING" connectionString="Data Source=SIVA1;Initial Catalog=PCS;uid=PCS;pwd=PCS;timeout=100000;"/>
      </connectionStrings>
    </configuration>

    the above connectionstring my DataSource name is actually SIVA. But i gave wrongly SIVA1. so the application should throw exception. other details like InitialCatalog, Uid,pwd are exactly correct.

    Code behind:-

    private void ExceptionWindow_Initialized(object sender, EventArgs e)
            {
                string connectionString = string.Empty;
                connectionString = ConfigurationManager.ConnectionStrings["CONNECTIONSTRING"].ToString();
                this._conn = new SqlConnection(connectionString);
                DataTable dataTable = new DataTable();
                SqlDataAdapter adapter = new SqlDataAdapter("SELECT * FROM [dbo].[LIBSUP]", this._conn);
                adapter.Fill(dataTable);
                this.SampleDataGrid.ItemsSource = dataTable.DefaultView;
            }

    in my window i have only one Datagrid.

    check it and let me know how to get exception in this.

    Tuesday, May 6, 2014 9:35 AM

  • Since you are setting the connection timeout to 100000 in your connection string, it will take 100000 seconds until the adapter.Fill method throws an SqlException. You should decrease this value:

    <add name="CONNECTIONSTRING" connectionString="Data Source=SIVA1;Initial Catalog=PCS;uid=PCS;pwd=PCS;timeout=15;"/>
    

    Tuesday, May 6, 2014 3:06 PM
  • Hi,

    Magnus’s point is correct. In default, the timeout is 15 seconds. If you set the timeout to 100000. You will get the exception after 100000 seconds. J

    For more information about Timeout property, see: http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlconnection.connectiontimeout(v=vs.110).aspx

    http://www.connectionstrings.com/all-sql-server-connection-string-keywords/

    The length of time (in seconds) to wait for a connection to the server before terminating the attempt and generating an error.

    Have a nice time!

    Sincerely,


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Wednesday, May 7, 2014 1:20 AM