In this project we need several Classes. They must be
included in the Internet part. To create a new Class, select the
ObservableCollection.Web with a right mouse click and select "New
Item".
Select "Class", give it a name and click
"OK".
Below a new Class Contact.vb is created.
We need four Classes.
· Contact.vb
· Functions.vb
· SQLHelper.vb
· Config.vb
You need to create the Classes this way. Just adding a Class
to the ObservableCollection.Web directory on your computer will show it but
will not be recognized.
The SQLhelper.vb Class can be copied from the attached
project. Copy this code and paste in into the SQLHelper.vb Class just created.
In the Contact.vb Class I will use just the name, address
and City of a Contact. Further, each contact will have an ID named IDContact.
The ID will be a GUI, an automatically created ID of 38 characters.
Code of Class Contact.vb
Imports System.Data.SqlClient
Imports System.Data
Imports System
Imports System.Web
Namespace demo
Public Class Contact
Private _City As String
Private _Address As String
Private _Name As String
Private _IDContact As String
Public Sub New()
MyBase.New()
End Sub
Public Property City() As String
Get
Return _City
End Get
Set(ByVal Value As String)
If Value = "" Then
_City = Nothing
Else
_City = Value
End If
End Set
End Property
Public Property Address() As String
Get
Return _Address
End Get
Set(ByVal Value As String)
If Value = "" Then
_Address = Nothing
Else
_Address = Value
End If
End Set
End Property
Public Property Name() As String
Get
Return _Name
End Get
Set(ByVal Value As String)
If Value = "" Then
_Name = Nothing
Else
_Name = Value
End If
End Set
End Property
Public Property IDContact() As String
Get
Return _IDContact
End Get
Set(ByVal Value As String)
If Value = "" Then
_IDContact = Nothing
Else
_IDContact = Value
End If
End Set
End Property
Public Overloads Function GetsContactAll() As SqlDataReader
Dim arParms() As SqlParameter = New SqlParameter(0) {}
Return SqlHelper.ExecuteReader(demo.Config.Connectionstring,
Data.CommandType.StoredProcedure, "O_GetS_ContactMaster", arParms)
End Function
End Class
End Namespace
The code in the Functions.vb Class will not be used as a
Class but as a module. It is easier to implement.
Suppose I need IDContact. What happens if IDContact is empty
or Nothing? I need an empty string.
Contact.IDContact = demo.Functions.NullSafeString(key)
will give it.
Code Class Funcions.vb
Namespace demo
Public Module Functions
Public Function NullSafeString(ByVal arg As Object, Optional ByVal
returnIfEmpty As String = "") As String
Dim returnValue As String
If (arg Is DBNull.Value) OrElse (arg Is Nothing) OrElse (arg Is
String.Empty) Then
returnValue = returnIfEmpty
Else
Try
returnValue = CStr(arg).Trim
Catch
returnValue = returnIfEmpty
End Try
End If
Return returnValue
End Function
End Module
End Namespace
Then the Config.vb Class. This is also a module. In the
Contact Class I want to get data from the SQL database through a connection
string.
demo.Config.Connectionstring
will give it.
Code Class Config.vb
Imports System.Web
Imports System.Configuration
Namespace demo
Public Module Config
Dim result As String
Public Function Connectionstring() As String
result = (ConfigurationManager.AppSettings("ConnectionItalie"))
Return result
End Function
End Module
End Namespace
The connection information itself is stored in the
config.web part.
Code for the Config.web.
<?xml version="1.0"?>
<configuration>
<system.web>
<compilation debug="true" strict="false" explicit="true" targetFramework="4.0" />
</system.web>
<appSettings>
<add key="ConnectionItalie"
value="Data Source='URL'; user ID='username';Password='Password';Initial
Catalog='DatabaseName';" />
</appSettings>
<system.serviceModel>
<behaviors>
<serviceBehaviors>
<behavior name="">
<serviceMetadata httpGetEnabled="true" />
<serviceDebug includeExceptionDetailInFaults="false" />
</behavior>
</serviceBehaviors>
</behaviors>
<serviceHostingEnvironment multipleSiteBindingsEnabled="true" />
</system.serviceModel>
</configuration>