Results 1 to 5 of 5

Thread: [RESOLVED] How to check connection before instantiating context in LINQ to Entities,

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Apr 2007
    Location
    cobwebbed to PC
    Posts
    311

    Resolved [RESOLVED] How to check connection before instantiating context in LINQ to Entities,

    Hi Folks

    I've been using LINQ to Entites with the graphical Entity Designer taking care of creating all ORM stuff for my DB.

    Thus to use it I have something like the following that runs when my program loads.

    VB Code:
    1. Dim myContext As myDataEntities
    2. '...
    3. Try
    4.     myContext =  New myDataEntites
    5. Catch ex As Exception
    6.     MessageBox.Show("Could not talk to DB")
    7.     Me.Close()
    8.     Exit Sub
    9. End Try

    The problem is that if the connection is failing for some reason (e.g. I have no connection to the server the DB is on) it takes quiite a long while for anything at all to happen.

    Is there a better way I can confirm access to the DB before trying to create the context? (The DB is on a remote server that may be connected via proxy over ethernet)

    I did take a look at the generated code in the myData.Designer.vb from my .edmx, but couldn't really work out where it is that the connection to the DB is actually attempted.

    Thanks
    Thanks

  2. #2

    Thread Starter
    Hyperactive Member
    Join Date
    Apr 2007
    Location
    cobwebbed to PC
    Posts
    311

    Re: How to check connection before instantiating context in LINQ to Entities,

    So I'm currently thinking that I could just try pinging the SQL server and if the ping fails then cancel the form, while this is still slow-ish, its nowhere near as slow as waiting for an exception on the context creation, by a long shot.

    VB Code:
    1. '
    2.         Dim pingMe As New Ping()
    3.         Dim result = pingMe.Send(IPAddress.Parse("192.168.4.11"))
    4.  
    5.         If Not result.Status = IPStatus.Success Then
    6.             MessageBox.Show("Uh-Oh!")
    7.             Me.Close()
    8.             Exit Sub
    9.         End If

    This is in the Form1.Load event, but for some reason the form still shows... Surely Me.Close() Exit Sub should cancel the form (and so the application)??

    I have tried End instead of Exit Sub, but no difference... Anyone know how I should cancel the form correctly? And is there a better way of testing for server connection?
    Thanks
    Thanks

  3. #3
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,222

    Re: How to check connection before instantiating context in LINQ to Entities,

    You can't close a form in the Load event handler because it hasn't even opened yet. If you don't want to show a form then don't call its Show method. Once you call Show, it's gonna get shown.

    What you're doing is misguided though. The issue is that fact that the default timeout on an ADO.NET connection is 30 seconds. If you don't want to use a 30 second timeout then don't. You have no direct access to the SqlConnection that your EF context uses under the hood but you can simply set the timeout in the connection string. This code:
    Code:
    Dim builder As New SqlConnectionStringBuilder With {.DataSource = "MyServer",
                                                        .InitialCatalog = "MyDatabase",
                                                        .IntegratedSecurity = True,
                                                        .ConnectTimeout = 15}
    
    MessageBox.Show(builder.ConnectionString)
    display this connection string:
    Data Source=MyServer;Initial Catalog=MyDatabase;Integrated Security=True;Connect Timeout=15
    You just do something similar in your EF connection string in your config file.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  4. #4

    Thread Starter
    Hyperactive Member
    Join Date
    Apr 2007
    Location
    cobwebbed to PC
    Posts
    311

    Re: How to check connection before instantiating context in LINQ to Entities,

    This is the thing though, *I* dont call the show method, the user does when they start the application (or the VB project startup code does, I guess). The form is the only form in my project.

    I think one of the problems I had before was that I also already have code in the form.formClosing handler which references objects that never get instantiated if the connection fails, so that was causing another exception. As Visual Studio's response to unhandled exceptions in the form load and closing handlers seems to be to just skip the rest of the handler code and then show the form instead of actually reporting an exception (which is really annoying), it wasn't obvious what was going on.

    I've got around this by setting a boolean variable when I catch the exception and then checking for it in the formClosing handler.
    Thanks

  5. #5
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,222

    Re: How to check connection before instantiating context in LINQ to Entities,

    Quote Originally Posted by wolf99 View Post
    This is the thing though, *I* dont call the show method, the user does when they start the application (or the VB project startup code does, I guess). The form is the only form in my project.
    So are you saying that you want the application to close without displaying the main form? In that case you handle the Startup event of the application and set e.Cancel to True. If you provide the appropriate information then you can get the appropriate solution. Follow the CodeBank link in my signature and check out my WinForms Login thread for an example. It prompts the user to login at startup and exits the app without displaying a form if they don't/can't.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width