I'm getting errors when I try open a URL in VB

2

I get a "A first chance exception of type 'System.ComponentModel.Win32Exception' occurred in System.dll" error when I try to open a URL in VB. I have tried multiply ways of opening the site, but all have returned this error. The code I'm using now is this

Public Class Revise

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Bitesize.Click
        Dim bitesize As String = "http://www.bbc.co.uk/bitesize/standard/"
        Process.Start(bitesize)
    End Sub
End Class

I'm very new to programming, so sorry if this is a dumb mistake I've made.

error details:

System.ComponentModel.Win32Exception was unhandled
  ErrorCode=-2147467259
  HResult=-2147467259
  Message=Unknown error (0x80041002)
  NativeErrorCode=-2147217406
  Source=System
  StackTrace:
       at System.Diagnostics.Process.StartWithShellExecuteEx(ProcessStartInfo startInfo)
       at System.Diagnostics.Process.Start()
       at System.Diagnostics.Process.Start(ProcessStartInfo startInfo)
       at System.Diagnostics.Process.Start(String fileName)
       at WindowsApplication1.Revise.Button1_Click(Object sender, EventArgs e) in C:\Users\Lewis\Documents\Visual Studio 11\Projects\Study Time!\Study Time!\Revise.vb:line 7
       at System.Windows.Forms.Control.OnClick(EventArgs e)
       at System.Windows.Forms.Button.OnClick(EventArgs e)
       at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
       at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
       at System.Windows.Forms.Control.WndProc(Message& m)
       at System.Windows.Forms.ButtonBase.WndProc(Message& m)
       at System.Windows.Forms.Button.WndProc(Message& m)
       at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
       at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
       at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
       at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
       at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr dwComponentID, Int32 reason, Int32 pvLoopData)
       at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
       at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
       at System.Windows.Forms.Application.Run(ApplicationContext context)
       at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.OnRun()
       at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.DoApplicationModel()
       at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.Run(String[] commandLine)
       at WindowsApplication1.My.MyApplication.Main(String[] Args) in 17d14f5c-a337-4978-8281-53493378c1071.vb:line 81
       at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
       at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
       at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
       at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
       at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
       at System.Threading.ThreadHelper.ThreadStart()
  InnerException: 
vb.net
url
visual-studio-2012
asked on Stack Overflow Dec 7, 2012 by Lewis Lappin • edited Feb 13, 2014 by tshepang

2 Answers

0

Process.Start actually executes a program within the operating system. The url you're attempting to visit isn't the name of a program on the operating system so it fails. If you want to open an external browser instance with this url you'll need to open that program as a process.

Private Sub Button1_Click(ByVal sender as Object, ByVal e as EventArgs) Handles Bitesize.Click
    Dim bitesize as String = "http://www.bbc.co.uk/bitesize/standard/"
    Dim programName as String = "iexplore.exe"

    Process.Start(programName & " " & bitesize)
End Sub

This should achieve the effect you're looking for, but in a winforms application it would make more sense to make use of the WebBrowser control and populate its url property on the button click instead.

Edit:
To open up a url in the default browser of the system, you can simply have your programName set to "start".

Dim programName as String = "start"

This is a short cut in Windows that will open a url with the default browser automagically.

answered on Stack Overflow Dec 7, 2012 by Joel Etherton • edited Dec 7, 2012 by Joel Etherton
0

That code (as it sounds like you already know) will open that website with your computer's default web browser. Your code is correct (allbeit, the method name should probably match the control that it's listening for, so it's a little easier to read when you have a form with lots of event handlers).

My guess (without having the details -- in the future, try to post the Exception stack trace), would be that your default web browser can't be determined. Try this: Open Control Panel\Programs\Default Programs\Set Associations .. and find HTTP protocol (toward the bottom, after all the file extension associations) and reset which application is associated.

Second, this is another good learning opportunity, when you've got code where things can go wrong (such as launching another process), it is a very good idea to wrap that code in a Try/Catch block.. then if something goes wrong, you can handle it, instead of crashing your application.

Try
    Dim bitesize As String = "http://www.bbc.co.uk/bitesize/standard/"
    Process.Start(bitesize)
Catch Ex As Exception
    MessageBox.Show(Ex.Message)
End Try

MSDN for System.Diagnostic.Process.Start http://msdn.microsoft.com/en-us/library/53ezey2s.aspx

If you're still having problems, post the stack trace and that might help shine a little more light on what's going wrong.

answered on Stack Overflow Dec 7, 2012 by Derek Curtis

User contributions licensed under CC BY-SA 3.0