Server Intellect

Check for Internet Connection (Final Part)

Category: .NET Framework

Check for an Internet Connection – Part Three of Three

This is the Final Part of this article series. Read Part 2 here

Introduction

In the previous two parts of this series of articles we have shown how we can utilize an external library and import that library into our code very easily.  We also showed how we call that external library and display the results of the call to the external library.
Has started to design the application and have created the enumeration, screen and methods necessary to check for an Internet connection.

We migrated our web sites to Server Intellect over one weekend and the setup was so smooth that we were up and running right away. They assisted us with everything we needed to do for all of our applications. With Server Intellect's help, we were able to avoid any headaches!


Implementation

In this example we will show you entire set of source code and what the finished application looks like.  You can take this source code and cut and paste it into Visual Studio, or you can download the attached compressed file and open the project within Visual Studio.

Source Code

We are now ready to see how everything is put together and how we trigger the call for the check of an Internet connection.  The following source code can be cut and pasted into Visual Studio for your use or you can open the project in the attached compressed file that holds the source code.

 

 

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
namespace CheckInternet
{
public partial class Form1 : Form
{
[Flags]
public enum StateOfConnection
{
Modem = 0x1,
Lan = 0x2,
Proxy = 0x4,
Installed = 0x10,
OffLine = 0x20,
Configured = 0x40
}
[DllImport("wininet.dll", CharSet = CharSet.Auto)]
static extern bool InternetGetConnectedState(ref StateOfConnection lpdwFlags, int dwReserved);
public Form1()
{
InitializeComponent();
}
private void btnTestConnection_Click(object sender, EventArgs e)
{
StateOfConnection MyState = 0;
txtFlag.Text = InternetGetConnectedState(ref MyState, 0).ToString();
txtState.Text = MyState.ToString();
}
}
}

We moved our web sites to Server Intellect and have found them to be incredibly professional. Their setup is very easy and we were up and running in no time.

Compile and execute the above code.  When the application starts you will be presented with a screen that will allow you to select a button to “Test” the Internet connection.  The results of that connection test will be displayed within two labels.  Please see Fig. 2 for a visual representation of the screen we just discussed.
Screen shot of the results of the above example: Fig. 2

Image 1
[Click to see full-size]


What have we learned?

Need help with Windows Dedicated Hosting? Try Server Intellect. I'm a happy customer!

  • That C# has the ability to import external libraries easily
  • That we can test an Internet Connection before we try to access it
  • How to use an enumeration
  • That an enumeration can be treated as a bit field
  • Definition of Connection States
  • How we call and utilize the external library

This is the Final Part of this article series. Read Part 1 here

Attachments



Download Project Source - Enter your Email to be emailed a link to download the Full Source Project used in this Tutorial!



100% SPAM FREE! We will never sell or rent your email address!

Leave a Comment

Comments on this Article

Post a Comment
Name:
Website:
Email:
Comments:

#1 Anuities

Posted By: Anuities | 11.23.2009 at 1:21 AM

I am so glad I stumbled over your article. This is just what i needed to know as i was having a tough time trying to figure out checking for the internet source code. Thanks.

#2 Galvanizing Companies

Posted By: Galvanizing Companies | 11.23.2009 at 4:49 AM

I have learned a lot from this article & I appreciate you sharing your knowledge. Keep it coming. Thanks.

#3 Advanced Correctional Healthcare

Posted By: Advanced Correctional Healthcare | 11.23.2009 at 4:53 AM

Thanks for the source code. It really saved me so much time getting my server running.

#4 Non Chexsystems Banks

Posted By: Non Chexsystems Banks | 12.02.2009 at 4:41 AM

After going through this, I can now say, I know how to check for an internet connection before trying to access it. Thanks.

#5 Train Horn

Posted By: Train Horn | 12.02.2009 at 11:19 AM

Will the same source code work with java also?

#6 Collections Etc.

Posted By: Collections Etc. | 12.16.2009 at 8:28 AM

This is very informative, very much helpful as we depend so much on the internet and we need some kind of tips to troubleshoot connection.

#7 Second Chance Checking

Posted By: Second Chance Checking | 12.23.2009 at 3:20 AM

I have been following your tutorials on this topic & I must say I am learning a lot from it. Just want to say thanks & I look forward to more such tutorials from you.

#8 Horse Saddles

Posted By: Horse Saddles | 12.23.2009 at 7:27 AM

It has been really nice of you to share the tutorials with us. I like the way you have been explaining them in a step by step manner. Thanks.

#9 Credit Cards

Posted By: Credit Cards | 12.23.2009 at 11:43 AM

Visual Studio is indeed a powerful web coding tool. Thanks for sharing this with us. I can now check for internet connection before i browse.

#10 Laptop Handbags

Posted By: Laptop Handbags | 12.23.2009 at 6:07 PM

Thanks for the article. It is really helpful.

#11 Eye Surgery Associates

Posted By: Eye Surgery Associates | 1.04.2010 at 10:03 PM

Thanks for the information on Server Intellect. Very helpful.

#12 Tiens

Posted By: Tiens | 1.13.2010 at 2:14 AM

from your explanation, i feel i understand. But i get lost the moment i try to implement

#13 Relyndis

Posted By: Relyndis | 1.13.2010 at 2:22 AM

I find this information very helpful. Thanks for sharing

#14 Relyndis

Posted By: Relyndis | 1.13.2010 at 2:29 AM

Tha&t's a grat post

#15 Miz

Posted By: Miz | 1.13.2010 at 3:28 AM

That's a very interesting post. Thanks for the info.

www.tienshealth.net

#16 Free tips to lose stomach fat

Posted By: Free tips to lose stomach fat | 1.29.2010 at 12:54 PM

thanks for I.T. advice really helpful informations!!

#17 Se TV Online

Posted By: Se TV Online | 2.03.2010 at 4:16 PM

Thanks for great information, it was really helpful!

#18 25th Wedding Anniversary

Posted By: 25th Wedding Anniversary | 2.04.2010 at 2:21 AM

This is sure help for many internet users who are facing some sort of connection problems. Thanks for sharing all the information. :)

#19 Mitel Telephone System

Posted By: Mitel Telephone System | 2.08.2010 at 8:31 AM

I've already bookmark this article and will surely refer this to all my close friends working in IT field. :)

#20 Best Spyware Blockers

Posted By: Best Spyware Blockers | 2.09.2010 at 7:55 PM

This is very useful...thank you for sharing...

#21 internet marketing affiliate program

Posted By: internet marketing affiliate program | 2.14.2010 at 1:56 PM

Thanks for the very useful information. I look forward to some new posts from you

#22 internet marketing affiliate program

Posted By: internet marketing affiliate program | 2.14.2010 at 1:57 PM

Thanks for the great post and useful information

#23 Online Advertising Agency

Posted By: Online Advertising Agency | 2.24.2010 at 7:17 AM

I must say that overall I am really impressed with this blog.It is easy to see that you are impassioned about your writing. I wish I had got your ability to write. I look forward to more updates and will be returning.

#24 Skip Hire

Posted By: Skip Hire | 2.26.2010 at 3:50 AM

Skip Hire, the number name.

#25 Skip Hire

Posted By: Skip Hire | 2.26.2010 at 3:51 AM

Skip Hire, the number name.

#26 American Food and Sweets

Posted By: American Food and Sweets | 2.26.2010 at 3:52 AM

American Food UK is a UK site selling a great range of American Food favourites in the UK, with same day dispatch and fast delivery on all orders.

#27 Austin Divorce Attorney

Posted By: Austin Divorce Attorney | 3.04.2010 at 4:58 AM

This will be very useful for many computer professionals, thanks for sharing this article. :)

#28 Robey Thon

Posted By: Robey Thon | 3.04.2010 at 10:06 PM

Thanks for the article

#29 movie watch online

Posted By: movie watch online | 3.07.2010 at 3:38 AM

Wonderful article.I've bookmarked it already. Take care, Xenia.

#30 Toddler Girls Dresses

Posted By: Toddler Girls Dresses | 3.29.2010 at 8:20 PM

This was very interesting to read, thanks!

#31 Daily Motivational Quotes

Posted By: Daily Motivational Quotes | 5.30.2010 at 12:05 AM

The above source code can be cut and pasted into Visual Studio for your use or you can open the project in the attached compressed file that holds the source code.

#32 DFO Gold

Posted By: DFO Gold | 6.19.2010 at 11:55 PM

Thanks for the source code.

#33 DFO Gold

Posted By: DFO Gold | 6.19.2010 at 11:56 PM

Thanks for the source code.

#34 DFO Gold

Posted By: DFO Gold | 6.19.2010 at 11:56 PM

Thanks for the source code

#35 DFO Gold

Posted By: DFO Gold | 6.19.2010 at 11:56 PM

It has been really nice of you to share the tutorials with us. I like the way you have been explaining them in a step by step manner. Thanks.

#36 tiffany co

Posted By: tiffany co | 7.21.2010 at 3:17 AM

For classic and quality genuine silver jewelry, many people choose Tiffany & Co brand.Now,we present you the hottest Tiffany silver jewelry .get more http://www.tiffanyonsale.com/

#37 tiffany jewelry

Posted By: tiffany jewelry | 7.21.2010 at 3:17 AM

For classic and quality genuine silver jewelry, many people choose Tiffany & Co brand.Now,we present you the hottest Tiffany silver jewelry .get more http://www.tiffanyonsale.com/

#38 tiffany jewelry

Posted By: tiffany jewelry | 7.21.2010 at 3:17 AM

For classic and quality genuine silver jewelry, many people choose Tiffany & Co brand.Now,we present you the hottest Tiffany silver jewelry .get more http://www.tiffanyonsale.com/

#39 tiffany jewellery

Posted By: tiffany jewellery | 7.21.2010 at 3:17 AM

For classic and quality genuine silver jewelry, many people choose Tiffany & Co brand.Now,we present you the hottest Tiffany silver jewelry .get more http://www.tiffanyonsale.com/

#40 tiffany ring

Posted By: tiffany ring | 7.26.2010 at 7:15 AM

This is exactly cheap tiffany jewelry you can get cheap Tiffany Rings, Necklaces, Errings, Bracelets and other Tiffany Jewellery.get more http://www.tiffanyonsale.com/

#41 tiffany bracelet

Posted By: tiffany bracelet | 7.26.2010 at 7:15 AM

This is exactly cheap tiffany jewelry you can get cheap Tiffany Rings, Necklaces, Errings, Bracelets and other Tiffany Jewellery.get more http://www.tiffanyonsale.com/

#42 tiffany necklace

Posted By: tiffany necklace | 7.26.2010 at 7:15 AM

This is exactly cheap tiffany jewelry you can get cheap Tiffany Rings, Necklaces, Errings, Bracelets and other Tiffany Jewellery.get more http://www.tiffanyonsale.com/

#43 gucci bags

Posted By: gucci bags | 7.29.2010 at 11:18 PM

<a href="http://www.guccicn.net" title="gucci bags"><strong>gucci bags</strong></a>

<a href="http://www.guccicn.net" title="gucci bag"><strong>gucci bag</strong></a>

<a href="http://www.guccicn.net" title="gucci handbags"><strong>gucci handbags</strong></a>

<a href="http://www.guccicn.net" title="gucci handbag"><strong>gucci handbag</strong></a>

<a href="http://www.guccicn.net" title="discount gucci handbags"><strong>discount gucci handbags</strong></a>