Archive for the ‘Visual Basic’ Category

System.Data.DataSetExtensions Config Error in Visual Studio 2008 RTM

3 December, 2007

I got the exception shown below when I opened an application that was developed under VS 2008:

ASP.NET runtime error: Could not load file or assembly ‘System.Data.DataSetExtensions, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089′ or one of its dependencies. The system cannot find the file specified

What’s happened is there is no more an assembly System.Data.DataSetExtensions with version number 2.0. There is a new version for the same assembly name under 3.5.0.0.

To resolve the problem, simply right-click on the web application, properties, then remove the reference to the 2.0.0.0 assembly.

Then, right-click on the web application solution, Add Reference, locate the System.Data.DataSetExtensions and add a reference to it. You may also need to update the entry web / app.config file under the <assemblies> tag. Update the version to 3.5.0.0

Random numbers – a .Net function to return a random number

5 December, 2006

A random number generator I put together as an example for a collegue. Thought I would share it here, it uses ticks as the seed.

Public Shared Function RandomNumber(ByVal MaxNumber As Integer, _
ByVal MinNumber As Integer) As Integer

‘initialize random number generator
Dim r As New Random(System.DateTime.Now.Ticks)
Return r.Next(MinNumber, MaxNumber)

End Function

Check URL validity ASP.net

5 December, 2006

A handy little function I’ve been using for a while to check if a URL is valid.

The code actually opens the URL and returns the content (html in most cases) as a string. If the code fails it will fall in the catch block, meaning the URL isn’t valid.

I dont like using a try catch like this as it creates a relatively large overhead – especially if it’s used multiple times in a single postback etc. I’ve tried some different methods and had a poke on the web but didn’t manage to get rid of the try statement. If you have any further ideas please comment.

 Dont forget if the file is within filesystem reach (on the same computer or within network range) then you could use System.IO to check the file exists.

Public Class UrlValidity

Public Shared Function IsValid(ByVal Url As String) As Boolean
Dim sStream As Stream
Dim URLReq As HttpWebRequest
Dim URLRes As HttpWebResponse

Try
     URLReq = WebRequest.Create(Url)
     URLRes = URLReq.GetResponse()
     sStream = URLRes.GetResponseStream()
     Dim reader As String = New StreamReader(sStream).ReadToEnd()
     Return True
Catch ex As Exception
     ‘Url not valid
     Return False
End Try
End Function

End Class