Lightweight DataTable for your Silverlight applications

Source: Lightweight DataTable for your Silverlight Apps

Since there is no DataTable in Silverlight I’ve created small class that can be used to generate columns and rows in runtime in similar to the real DataTable way:

DataTable table = new DataTable();

table.Columns.Add(new DataColumn() { ColumnName = "ID", DataType = typeof(int) });
table.Columns.Add(new DataColumn() { ColumnName = "Name", DataType = typeof(string) });
table.Columns.Add(new DataColumn() { ColumnName = "UnitPrice", DataType = typeof(decimal) });
table.Columns.Add(new DataColumn() { ColumnName = "Date", DataType = typeof(DateTime) });
table.Columns.Add(new DataColumn() { ColumnName = "Discontinued", DataType = typeof(bool) });

for(var i = 0; i < 1000; i++)
{
    DataRow row = new DataRow();
    row["ID"] = i;
    row["Name"] = names[rnd.Next(9)];
    row["UnitPrice"] = prizes[rnd.Next(9)];
    row["Date"] = DateTime.Now.AddDays(i);
    row["Discontinued"] = bools[rnd.Next(9)];

    table.Rows.Add(row);
}

Class File is named Data.cs, c3 Version: 


Unfortunately, there’s no VB.Net version of this, haven't gotten to it yet.

Sending Generic.List collection back to WCF ObservableCollection

Found a good article and helped with a problem I was having, trying to send a list of collection back to a WCF service that was set to accept Observable Collections. 

Source: http://smehrozalam.wordpress.com/2009/01/18/ienumerabletoobservablecollection/

In WPF/Silverlight, binding UI objects such as DataGrid or ListBox to collections is typically done using an ObservableCollectioninstead of the generic List object. This way, our UI is automatically synchronized since the observable collection provides event notification to WPF data binding engine whenever items are added, removed, or when the whole list is refreshed. The LINQ extension methods that return a collection actually return IEnumerable<T>. The .NET framework for Silverlight provides built-in extension methods to convert IEnumerable<T> to List<T> and Array<T> but there’s no method available to convert the collection to ObservableCollection<T>(WPF developers can simply use this constructor overload) . So here’s one you may find useful:

  1. Public Static Class CollectionExtensions
  2. {
  3.     public static ObservableCollection<T> ToObservableCollection<T>(this IEnumerable<T> enumerableList)
  4.     {
  5.         if (enumerableList != null)
  6.         {
  7.             //create an emtpy observable collection object
  8.             var observableCollection = new ObservableCollection<T>();
  9.  
  10.             //loop through all the records and add to observable collection object
  11.             foreach (var item in enumerableList)
  12.                 observableCollection.Add(item);
  13.  
  14.             //return the populated observable collection
  15.             return observableCollection;
  16.         }
  17.         return null;
  18.     }
  19. }

Extension methods are very powerful and I am planning to post an example demonstrating their potential.

I’ve converted the above example to VB.Net version for you to copy.

Public Shared Function ToObservableCollection(Of T)(ByVal enumerableList As IEnumerable(Of T)) As ObservableCollection(Of T)
    If enumerableList IsNot Nothing Then
        'create an emtpy observable collection object
        Dim observableCollection As New ObservableCollection(Of T)()

        'loop through all the records and add to observable collection object
        For Each item As T In enumerableList
            observableCollection.Add(item)
        Next

        'return the populated observable collection
        Return observableCollection
    End If
    Return Nothing
End Function

Now you can call your List and send it back as a ObservableCollection

.Net Resetting Scroll Position When MaintainScrollPositionOnPostBack = True

Had a friend have an issue where he had select buttons on a gridview and on selected changed event he would update control at the top of his page.  However, he had MaintainScrollPositionOnPostBack set to True, so of course the browser would reload and scroll down to where he had clicked the Select button.

Found this article how to override the MaintainScrollPosition so it would go to the top of the page.  Works like a charm.

http://www.4guysfromrolla.com/articles/111407-1.aspx

On postback, the ASP.NET page adds a call to the client-side function WebForm_RestoreScrollPosition(), which returns the browser's scroll position to the X and Y coordinates specified by the __SCROLLPOSITIONX and __SCROLLPOSITIONY form fields (which were assigned the browser's scroll position before the postback ensued). Consequently, the browser's scroll position is maintained across postbacks. The diagram below illustrates this interaction graphically.

In order to reset the scroll position, we need to add the above JavaScript to the rendered page's output and then call it before the call to WebForm_RestoreScrollPosition(). The following code adds a function (ResetScrollPosition) to the outputted markup and then injects a line of script that invokes this function. The server-side code uses the RegisterClientScriptBlock method to register the ResetScrollPosition function, which adds the function to the top of the form. As noted earlier, RegisterStartupScript adds a call to ResetScrollPosition at the bottom of the page.

Private Sub ResetScrollPosition()
If Not ClientScript.IsClientScriptBlockRegistered(Me.GetType(), "CreateResetScrollPosition") Then
'Create the ResetScrollPosition() function
ClientScript.RegisterClientScriptBlock(Me.GetType(), "CreateResetScrollPosition", _
"function ResetScrollPosition() {" & vbCrLf & _
" var scrollX = document.getElementById('__SCROLLPOSITIONX');" & vbCrLf & _
" var scrollY = document.getElementById('__SCROLLPOSITIONY');" & vbCrLf & _
" if (scrollX && scrollY) {" & vbCrLf & _
" scrollX.value = 0;" & vbCrLf & _
" scrollY.value = 0;" & vbCrLf & _
" }" & vbCrLf & _
"}", True)
'Add the call to the ResetScrollPosition() function
ClientScript.RegisterStartupScript(Me.GetType(), "CallResetScrollPosition", "ResetScrollPosition();", True)
End If
End Sub

To reset scroll position on postback, simply call the server-side ResetScrollPosition() method from the appropriate event handler (be it the Click event handler of a Button, the PageIndexChanged event of the GridView, or some other event).

Rest of Article

Mapping a DataTable to an Object Class. Like a homemade Linq to SQL. Send over WCF to SilverLight

Application Foundation

An application I work has architecture in a way where we have a Class object that represents each table. So for example we might have a “Customer” table in the database. So our class would be something like..

Customer.vb class file…

Public Class Customer

    Private _FirstName As String = String.Empty
    Public Property FirstName() As String
        Get
            Return _FirstName
        End Get
        Set(ByVal value As String)
            _FirstName = value
        End Set
    End Property

    Private _LastName As String = String.Empty
    Public Property LastName() As String
        Get
            Return _LastName
        End Get
        Set(ByVal value As String)
            _LastName = value
        End Set
    End Property

End Class

In this class is a function to say list all customers, we call this “GetList” method. You might use another way to bring back a list of data. Ideally now we’d want to bring back Enumerable Lists, but the app was already structured for DataSets and DataTables. So I was trying to reuse what we had.

Customer.vb class file

Public Function GetList() As Data.DataTable
    Dim oDS As Data.DataSet
    Try
        oDS = DataAccess.SqlHelper.ExecuteDataset(_ConnectionString, Data.CommandType.StoredProcedure, "CustomerGetList")
        Return oDS.Tables(0)
    Catch ex As Exception
        Throw ex
    Finally
        oDS = Nothing
    End Try
End Function

So on our pages we might do something like this…

Default.aspx.vb

Protected Sub Button1_ClickSave()
    Dim oCustomer As New Customer
    oCustomer.FirstName = "Fred"
    oCustomer.LastName = "Mastro"
    oCustomer.SaveToDB()

    GridView1.DataSource = oCustomer.GetList()
    GridView1.DataBind()

    oCustomer = Nothing

End Sub

 

And this returns a nice DataTable and populates the grid. Great!

Problem Areas over WCF to SliverLight

I was working on a SilverLight application that would talk to an existing business application, to pull back and list records.  I had to use a Service, so I went with WCF since RIA.Net is not in production yet.  The problem was that DataTables can not be serialized and set over WCF to SilverLight.  So I tried messing with Linq to SQL to pull data back and populate classes I already had, but LinqTables are not serializable either, and instead of modifying all the Linq classes generated in my class I wanted to find an easier solution.

DataTable to List(Of Customer) Solution

I can across these two posts: Datatable to List of Objects Mapper and Creating datarow to object mapper and was amazed. Using reflection Chris Rock (lol) was able to read the properties of the object and map each column of the datatable to the desired property of the object. This is what I’m looking to do.  This would manually mimic Linq type functionality. The Bulk of the code is this

My Helper.vb class file

  1.  
  2. Public Class Helper
  3.  
  4.     Public Function MapDatarowToObject(Of T)(ByVal dr As DataRow) As T
  5.         Try
  6.  
  7.             If dr Is Nothing Then
  8.                 Return Nothing
  9.             End If
  10.  
  11.             Dim instance As T = Activator.CreateInstance(Of T)()
  12.  
  13.             Dim properties() As Reflection.PropertyInfo = instance.GetType().GetProperties()
  14.  
  15.             If (properties.Length > 0) Then
  16.                 For Each propertyObject As Reflection.PropertyInfo In properties
  17.  
  18.                     Dim valueSet As Boolean = False
  19.  
  20.                     For Each attributeObject As Object In propertyObject.GetCustomAttributes(False)
  21.  
  22.                         If attributeObject.GetType() Is GetType(MapperColumn) Then
  23.                             Dim columnAttributeObject As MapperColumn = CType(attributeObject, MapperColumn)
  24.  
  25.                             If (columnAttributeObject.ColumnName <> String.Empty) Then
  26.  
  27.                                 If dr.Table.Columns.Contains(columnAttributeObject.ColumnName) AndAlso Not dr(columnAttributeObject.ColumnName) Is DBNull.Value Then
  28.                                     propertyObject.SetValue(instance, dr(columnAttributeObject.ColumnName), Nothing)
  29.                                     valueSet = True
  30.  
  31.                                 End If
  32.  
  33.                             End If
  34.                         End If
  35.                     Next
  36.  
  37.                     If Not valueSet Then
  38.                         If dr.Table.Columns.Contains(propertyObject.Name) AndAlso Not dr(propertyObject.Name) Is DBNull.Value Then
  39.                             propertyObject.SetValue(instance, dr(propertyObject.Name), Nothing)
  40.                         End If
  41.                     End If
  42.  
  43.                 Next
  44.             End If
  45.  
  46.             Return instance
  47.         Catch ex As Exception
  48.             Throw ex
  49.         End Try
  50.     End Function
  51.  
  52.     Public Function MapDataTableToList(Of T)(ByVal dt As DataTable) As Collections.Generic.IList(Of T)
  53.         If dt Is Nothing Then
  54.             Return Nothing
  55.         End If
  56.  
  57.         Dim list As IList(Of T) = New List(Of T)
  58.  
  59.         For Each dr As DataRow In dt.Rows
  60.             list.Add(MapDatarowToObject(Of T)(dr))
  61.         Next
  62.  
  63.         Return list
  64.     End Function
  65.  
  66. End Class
  67.  
  68. <AttributeUsage(AttributeTargets.Property)> _
  69. Public Class MapperColumn
  70.     Inherits Attribute
  71.  
  72.     Private mColumnName As String
  73.  
  74.     Public Sub New(ByVal columnName As String)
  75.         mColumnName = columnName
  76.     End Sub
  77.  
  78.     Public Property ColumnName() As String
  79.         Get
  80.             Return mColumnName
  81.         End Get
  82.         Set(ByVal value As String)
  83.             mColumnName = value
  84.         End Set
  85.     End Property
  86.  
  87. End Class

Using Mapper in WCF

Now in my WCF Service methods I can just instantiate Customers like I always do in the normal application and apply the new Mapper method to get a List of the class objects.

WCFServiceMethods.vb file (which is code-behind to WCFServiceMethods.svc)

  1. Imports System.ServiceModel
  2. Imports System.ServiceModel.Activation
  3. Imports System.Runtime.Serialization 'For DataContract
  4. Imports System.Collections.Generic
  5.  
  6. <ServiceContract(Namespace:="")> _
  7. <ServiceBehavior(IncludeExceptionDetailInFaults:=True)> _
  8. <AspNetCompatibilityRequirements(RequirementsMode:=AspNetCompatibilityRequirementsMode.Allowed)> _
  9. Public Class WCFServiceMethods
  10.     <OperationContract()> _
  11.     Public Function GetCustomers() As Collections.Generic.List(Of Customer)
  12.         Dim oCustomer As New Customer
  13.  
  14.         Dim Customers As Collections.Generic.List(Of Customer)
  15.         Customers = Helper.MapDataTableToList(Of Customer)(oCustomer.GetList())
  16.  
  17.         oCustomer = Nothing
  18.         Return Customers
  19.  
  20.     End Function
  21. End Class

You’ll see in Line 15, I can take the DataTable and now create a List(Of Customer) object, which is serializable and send it over the wire.

Applying it in SilverLight or Other Application that uses your WCF Service

Assuming you referenced your WCF Service.. Now on my SilverLight app on a button click I can get the List of Customers and show it natively in a datagrid.

MainPage.xaml code-behind

Private Sub btnSearch_Click(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles btnSearch.Click
    Dim oWCFProxy As New WCFService.WCFServiceClient

    AddHandler oWCFProxy.GetCustomers, AddressOf oWCFProxy_GetCustomersCompleted
    oWCFProxy.GetCustomersAsync()

    oWCFProxy = Nothing

End Sub

Private Sub oWCFProxy_GetCustomersCompleted(ByVal sender As Object, ByVal e As WCFService.GetCustomersCompletedEventArgs)
    Dim Customers As Collections.ObjectModel.Collection(Of oWCFProxy.Customer)
    Customers = e.Result

    DataGrid1.ItemsSource = Customers
End Sub

Problem Area With Mapper Casting Types and Solution

So with all that above you should have enough to re-create all this. Read through Chris’ posts because it does a good job of explaining the mapper.  I ran into a problem with the mapper though on line 39. Problem is when it tries to map data from the database, it all comes back as a string, and you don’t know until run-time what System.Type you need, so you can’t CType or DirectCast by using System.Type.GetType(), so I had to create a crappy function to Throw ex check every possible combination. This the best way? I sure hope it isn’t. Hope to find a better solution for it but was pressed for time.

So what was my crappy solution? I don’t even want to post it haha. I swapped out line 39 and 28 with the a call to this method of 1531 lines of code.

Private Sub TryCastTypeToPropertyType(ByRef propertyObject As Reflection.PropertyInfo, ByRef instance As Object, ByRef dr As DataRow, ByRef ColumnName As String)

Try
'Try to Default Types
propertyObject.SetValue(instance, dr(ColumnName), Nothing)
Catch exDefault1 As InvalidCastException
Try
'Try to String
propertyObject.SetValue(instance, CType(dr(ColumnName), String), Nothing)
Catch exString1 As InvalidCastException
'Try to Integer
Try
propertyObject.SetValue(instance, CType(dr(ColumnName), Integer), Nothing)
Catch exInt32a As ArgumentException
'Try to DateTime
Try
propertyObject.SetValue(instance, CType(dr(ColumnName), DateTime), Nothing)
Catch exDate1 As ArgumentException
'Try to Decimal
Try
propertyObject.SetValue(instance, CType(dr(ColumnName), Decimal), Nothing)
Catch exDecimal1 As ArgumentException
'Try to Double
Try
propertyObject.SetValue(instance, CType(dr(ColumnName), Double), Nothing)
Catch exDouble1 As ArgumentException
'Try to Boolean
Try
propertyObject.SetValue(instance, CType(dr(ColumnName), Boolean), Nothing)
Catch exBoolean1 As ArgumentException
'Try To Date
Try
propertyObject.SetValue(instance, CType(dr(ColumnName), Date), Nothing)
Catch ex As Exception
Throw ex
End Try
Catch exBoolean2 As InvalidCastException
'Try To Date
Try
propertyObject.SetValue(instance, CType(dr(ColumnName), Date), Nothing)
Catch ex As Exception
Throw ex
End Try
End Try
Catch exDouble2 As InvalidCastException
'Try to Boolean
Try
propertyObject.SetValue(instance, CType(dr(ColumnName), Boolean), Nothing)
Catch exBoolean1 As ArgumentException
'Try To Date
Try
propertyObject.SetValue(instance, CType(dr(ColumnName), Date), Nothing)
Catch ex As Exception
Throw ex
End Try
Catch exBoolean2 As InvalidCastException
'Try To Date
Try
propertyObject.SetValue(instance, CType(dr(ColumnName), Date), Nothing)
Catch ex As Exception
Throw ex
End Try
End Try
End Try
Catch exDecimal2 As InvalidCastException
'Try to Double
Try
propertyObject.SetValue(instance, CType(dr(ColumnName), Double), Nothing)
Catch exDouble1 As ArgumentException
'Try to Boolean
Try
propertyObject.SetValue(instance, CType(dr(ColumnName), Boolean), Nothing)
Catch exBoolean1 As ArgumentException
'Try To Date
Try
propertyObject.SetValue(instance, CType(dr(ColumnName), Date), Nothing)
Catch ex As Exception
Throw ex
End Try
Catch exBoolean2 As InvalidCastException
'Try To Date
Try
propertyObject.SetValue(instance, CType(dr(ColumnName), Date), Nothing)
Catch ex As Exception
Throw ex
End Try
End Try
Catch exDouble2 As InvalidCastException
'Try to Boolean
Try
propertyObject.SetValue(instance, CType(dr(ColumnName), Boolean), Nothing)
Catch exBoolean1 As ArgumentException
'Try To Date
Try
propertyObject.SetValue(instance, CType(dr(ColumnName), Date), Nothing)
Catch ex As Exception
Throw ex
End Try
Catch exBoolean2 As InvalidCastException
'Try To Date
Try
propertyObject.SetValue(instance, CType(dr(ColumnName), Date), Nothing)
Catch ex As Exception
Throw ex
End Try
End Try
End Try
End Try
Catch exDate2 As InvalidCastException
'Try to Decimal
Try
propertyObject.SetValue(instance, CType(dr(ColumnName), Decimal), Nothing)
Catch exDecimal1 As ArgumentException
'Try to Double
Try
propertyObject.SetValue(instance, CType(dr(ColumnName), Double), Nothing)
Catch exDouble1 As ArgumentException
'Try to Boolean
Try
propertyObject.SetValue(instance, CType(dr(ColumnName), Boolean), Nothing)
Catch exBoolean1 As ArgumentException
'Try To Date
Try
propertyObject.SetValue(instance, CType(dr(ColumnName), Date), Nothing)
Catch ex As Exception
Throw ex
End Try
Catch exBoolean2 As InvalidCastException
'Try To Date
Try
propertyObject.SetValue(instance, CType(dr(ColumnName), Date), Nothing)
Catch ex As Exception
Throw ex
End Try
End Try
Catch exDouble2 As InvalidCastException
'Try to Boolean
Try
propertyObject.SetValue(instance, CType(dr(ColumnName), Boolean), Nothing)
Catch exBoolean1 As ArgumentException
'Try To Date
Try
propertyObject.SetValue(instance, CType(dr(ColumnName), Date), Nothing)
Catch ex As Exception
Throw ex
End Try
Catch exBoolean2 As InvalidCastException
'Try To Date
Try
propertyObject.SetValue(instance, CType(dr(ColumnName), Date), Nothing)
Catch ex As Exception
Throw ex
End Try
End Try
End Try
Catch exDecimal2 As InvalidCastException
'Try to Double
Try
propertyObject.SetValue(instance, CType(dr(ColumnName), Double), Nothing)
Catch exDouble1 As ArgumentException
'Try to Boolean
Try
propertyObject.SetValue(instance, CType(dr(ColumnName), Boolean), Nothing)
Catch exBoolean1 As ArgumentException
'Try To Date
Try
propertyObject.SetValue(instance, CType(dr(ColumnName), Date), Nothing)
Catch ex As Exception
Throw ex
End Try
Catch exBoolean2 As InvalidCastException
'Try To Date
Try
propertyObject.SetValue(instance, CType(dr(ColumnName), Date), Nothing)
Catch ex As Exception
Throw ex
End Try
End Try
Catch exDouble2 As InvalidCastException
'Try to Boolean
Try
propertyObject.SetValue(instance, CType(dr(ColumnName), Boolean), Nothing)
Catch exBoolean1 As ArgumentException
'Try To Date
Try
propertyObject.SetValue(instance, CType(dr(ColumnName), Date), Nothing)
Catch ex As Exception
Throw ex
End Try
Catch exBoolean2 As InvalidCastException
'Try To Date
Try
propertyObject.SetValue(instance, CType(dr(ColumnName), Date), Nothing)
Catch ex As Exception
Throw ex
End Try
End Try
End Try
End Try
End Try
Catch exInt32b As InvalidCastException
'Try to DateTime
Try
propertyObject.SetValue(instance, CType(dr(ColumnName), DateTime), Nothing)
Catch exDate1 As ArgumentException
'Try to Decimal
Try
propertyObject.SetValue(instance, CType(dr(ColumnName), Decimal), Nothing)
Catch exDecimal1 As ArgumentException
'Try to Double
Try
propertyObject.SetValue(instance, CType(dr(ColumnName), Double), Nothing)
Catch exDouble1 As ArgumentException
'Try to Boolean
Try
propertyObject.SetValue(instance, CType(dr(ColumnName), Boolean), Nothing)
Catch exBoolean1 As ArgumentException
'Try To Date
Try
propertyObject.SetValue(instance, CType(dr(ColumnName), Date), Nothing)
Catch ex As Exception
Throw ex
End Try
Catch exBoolean2 As InvalidCastException
'Try To Date
Try
propertyObject.SetValue(instance, CType(dr(ColumnName), Date), Nothing)
Catch ex As Exception
Throw ex
End Try
End Try
Catch exDouble2 As InvalidCastException
'Try to Boolean
Try
propertyObject.SetValue(instance, CType(dr(ColumnName), Boolean), Nothing)
Catch exBoolean1 As ArgumentException
'Try To Date
Try
propertyObject.SetValue(instance, CType(dr(ColumnName), Date), Nothing)
Catch ex As Exception
Throw ex
End Try
Catch exBoolean2 As InvalidCastException
'Try To Date
Try
propertyObject.SetValue(instance, CType(dr(ColumnName), Date), Nothing)
Catch ex As Exception
Throw ex
End Try
End Try
End Try
Catch exDecimal2 As InvalidCastException
'Try to Double
Try
propertyObject.SetValue(instance, CType(dr(ColumnName), Double), Nothing)
Catch exDouble1 As ArgumentException
'Try to Boolean
Try
propertyObject.SetValue(instance, CType(dr(ColumnName), Boolean), Nothing)
Catch exBoolean1 As ArgumentException
'Try To Date
Try
propertyObject.SetValue(instance, CType(dr(ColumnName), Date), Nothing)
Catch ex As Exception
Throw ex
End Try
Catch exBoolean2 As InvalidCastException
'Try To Date
Try
propertyObject.SetValue(instance, CType(dr(ColumnName), Date), Nothing)
Catch ex As Exception
Throw ex
End Try
End Try
Catch exDouble2 As InvalidCastException
'Try to Boolean
Try
propertyObject.SetValue(instance, CType(dr(ColumnName), Boolean), Nothing)
Catch exBoolean1 As ArgumentException
'Try To Date
Try
propertyObject.SetValue(instance, CType(dr(ColumnName), Date), Nothing)
Catch ex As Exception
Throw ex
End Try
Catch exBoolean2 As InvalidCastException
'Try To Date
Try
propertyObject.SetValue(instance, CType(dr(ColumnName), Date), Nothing)
Catch ex As Exception
Throw ex
End Try
End Try
End Try
End Try
Catch exDate2 As InvalidCastException
'Try to Decimal
Try
propertyObject.SetValue(instance, CType(dr(ColumnName), Decimal), Nothing)
Catch exDecimal1 As ArgumentException
'Try to Double
Try
propertyObject.SetValue(instance, CType(dr(ColumnName), Double), Nothing)
Catch exDouble1 As ArgumentException
'Try to Boolean
Try
propertyObject.SetValue(instance, CType(dr(ColumnName), Boolean), Nothing)
Catch exBoolean1 As ArgumentException
'Try To Date
Try
propertyObject.SetValue(instance, CType(dr(ColumnName), Date), Nothing)
Catch ex As Exception
Throw ex
End Try
Catch exBoolean2 As InvalidCastException
'Try To Date
Try
propertyObject.SetValue(instance, CType(dr(ColumnName), Date), Nothing)
Catch ex As Exception
Throw ex
End Try
End Try
Catch exDouble2 As InvalidCastException
'Try to Boolean
Try
propertyObject.SetValue(instance, CType(dr(ColumnName), Boolean), Nothing)
Catch exBoolean1 As ArgumentException
'Try To Date
Try
propertyObject.SetValue(instance, CType(dr(ColumnName), Date), Nothing)
Catch ex As Exception
Throw ex
End Try
Catch exBoolean2 As InvalidCastException
'Try To Date
Try
propertyObject.SetValue(instance, CType(dr(ColumnName), Date), Nothing)
Catch ex As Exception
Throw ex
End Try
End Try
End Try
Catch exDecimal2 As InvalidCastException
'Try to Double
Try
propertyObject.SetValue(instance, CType(dr(ColumnName), Double), Nothing)
Catch exDouble1 As ArgumentException
'Try to Boolean
Try
propertyObject.SetValue(instance, CType(dr(ColumnName), Boolean), Nothing)
Catch exBoolean1 As ArgumentException
'Try To Date
Try
propertyObject.SetValue(instance, CType(dr(ColumnName), Date), Nothing)
Catch ex As Exception
Throw ex
End Try
Catch exBoolean2 As InvalidCastException
'Try To Date
Try
propertyObject.SetValue(instance, CType(dr(ColumnName), Date), Nothing)
Catch ex As Exception
Throw ex
End Try
End Try
Catch exDouble2 As InvalidCastException
'Try to Boolean
Try
propertyObject.SetValue(instance, CType(dr(ColumnName), Boolean), Nothing)
Catch exBoolean1 As ArgumentException
'Try To Date
Try
propertyObject.SetValue(instance, CType(dr(ColumnName), Date), Nothing)
Catch ex As Exception
Throw ex
End Try
Catch exBoolean2 As InvalidCastException
'Try To Date
Try
propertyObject.SetValue(instance, CType(dr(ColumnName), Date), Nothing)
Catch ex As Exception
Throw ex
End Try
End Try
End Try
End Try
End Try
End Try
Catch exString2 As ArgumentException
'Try to Integer
Try
propertyObject.SetValue(instance, CType(dr(ColumnName), Integer), Nothing)
Catch exInt32a As ArgumentException
'Try to DateTime
Try
propertyObject.SetValue(instance, CType(dr(ColumnName), DateTime), Nothing)
Catch exDate1 As ArgumentException
'Try to Decimal
Try
propertyObject.SetValue(instance, CType(dr(ColumnName), Decimal), Nothing)
Catch exDecimal1 As ArgumentException
'Try to Double
Try
propertyObject.SetValue(instance, CType(dr(ColumnName), Double), Nothing)
Catch exDouble1 As ArgumentException
'Try to Boolean
Try
propertyObject.SetValue(instance, CType(dr(ColumnName), Boolean), Nothing)
Catch exBoolean1 As ArgumentException
'Try To Date
Try
propertyObject.SetValue(instance, CType(dr(ColumnName), Date), Nothing)
Catch ex As Exception
Throw ex
End Try
Catch exBoolean2 As InvalidCastException
'Try To Date
Try
propertyObject.SetValue(instance, CType(dr(ColumnName), Date), Nothing)
Catch ex As Exception
Throw ex
End Try
End Try
Catch exDouble2 As InvalidCastException
'Try to Boolean
Try
propertyObject.SetValue(instance, CType(dr(ColumnName), Boolean), Nothing)
Catch exBoolean1 As ArgumentException
'Try To Date
Try
propertyObject.SetValue(instance, CType(dr(ColumnName), Date), Nothing)
Catch ex As Exception
Throw ex
End Try
Catch exBoolean2 As InvalidCastException
'Try To Date
Try
propertyObject.SetValue(instance, CType(dr(ColumnName), Date), Nothing)
Catch ex As Exception
Throw ex
End Try
End Try
End Try
Catch exDecimal2 As InvalidCastException
'Try to Double
Try
propertyObject.SetValue(instance, CType(dr(ColumnName), Double), Nothing)
Catch exDouble1 As ArgumentException
'Try to Boolean
Try
propertyObject.SetValue(instance, CType(dr(ColumnName), Boolean), Nothing)
Catch exBoolean1 As ArgumentException
'Try To Date
Try
propertyObject.SetValue(instance, CType(dr(ColumnName), Date), Nothing)
Catch ex As Exception
Throw ex
End Try
Catch exBoolean2 As InvalidCastException
'Try To Date
Try
propertyObject.SetValue(instance, CType(dr(ColumnName), Date), Nothing)
Catch ex As Exception
Throw ex
End Try
End Try
Catch exDouble2 As InvalidCastException
'Try to Boolean
Try
propertyObject.SetValue(instance, CType(dr(ColumnName), Boolean), Nothing)
Catch exBoolean1 As ArgumentException
'Try To Date
Try
propertyObject.SetValue(instance, CType(dr(ColumnName), Date), Nothing)
Catch ex As Exception
Throw ex
End Try
Catch exBoolean2 As InvalidCastException
'Try To Date
Try
propertyObject.SetValue(instance, CType(dr(ColumnName), Date), Nothing)
Catch ex As Exception
Throw ex
End Try
End Try
End Try
End Try
Catch exDate2 As InvalidCastException
'Try to Decimal
Try
propertyObject.SetValue(instance, CType(dr(ColumnName), Decimal), Nothing)
Catch exDecimal1 As ArgumentException
'Try to Double
Try
propertyObject.SetValue(instance, CType(dr(ColumnName), Double), Nothing)
Catch exDouble1 As ArgumentException
'Try to Boolean
Try
propertyObject.SetValue(instance, CType(dr(ColumnName), Boolean), Nothing)
Catch exBoolean1 As ArgumentException
'Try To Date
Try
propertyObject.SetValue(instance, CType(dr(ColumnName), Date), Nothing)
Catch ex As Exception
Throw ex
End Try
Catch exBoolean2 As InvalidCastException
'Try To Date
Try
propertyObject.SetValue(instance, CType(dr(ColumnName), Date), Nothing)
Catch ex As Exception
Throw ex
End Try
End Try
Catch exDouble2 As InvalidCastException
'Try to Boolean
Try
propertyObject.SetValue(instance, CType(dr(ColumnName), Boolean), Nothing)
Catch exBoolean1 As ArgumentException
'Try To Date
Try
propertyObject.SetValue(instance, CType(dr(ColumnName), Date), Nothing)
Catch ex As Exception
Throw ex
End Try
Catch exBoolean2 As InvalidCastException
'Try To Date
Try
propertyObject.SetValue(instance, CType(dr(ColumnName), Date), Nothing)
Catch ex As Exception
Throw ex
End Try
End Try
End Try
Catch exDecimal2 As InvalidCastException
'Try to Double
Try
propertyObject.SetValue(instance, CType(dr(ColumnName), Double), Nothing)
Catch exDouble1 As ArgumentException
'Try to Boolean
Try
propertyObject.SetValue(instance, CType(dr(ColumnName), Boolean), Nothing)
Catch exBoolean1 As ArgumentException
'Try To Date
Try
propertyObject.SetValue(instance, CType(dr(ColumnName), Date), Nothing)
Catch ex As Exception
Throw ex
End Try
Catch exBoolean2 As InvalidCastException
'Try To Date
Try
propertyObject.SetValue(instance, CType(dr(ColumnName), Date), Nothing)
Catch ex As Exception
Throw ex
End Try
End Try
Catch exDouble2 As InvalidCastException
'Try to Boolean
Try
propertyObject.SetValue(instance, CType(dr(ColumnName), Boolean), Nothing)
Catch exBoolean1 As ArgumentException
'Try To Date
Try
propertyObject.SetValue(instance, CType(dr(ColumnName), Date), Nothing)
Catch ex As Exception
Throw ex
End Try
Catch exBoolean2 As InvalidCastException
'Try To Date
Try
propertyObject.SetValue(instance, CType(dr(ColumnName), Date), Nothing)
Catch ex As Exception
Throw ex
End Try
End Try
End Try
End Try
End Try
Catch exInt32b As InvalidCastException
'Try to DateTime
Try
propertyObject.SetValue(instance, CType(dr(ColumnName), DateTime), Nothing)
Catch exDate1 As ArgumentException
'Try to Decimal
Try
propertyObject.SetValue(instance, CType(dr(ColumnName), Decimal), Nothing)
Catch exDecimal1 As ArgumentException
'Try to Double
Try
propertyObject.SetValue(instance, CType(dr(ColumnName), Double), Nothing)
Catch exDouble1 As ArgumentException
'Try to Boolean
Try
propertyObject.SetValue(instance, CType(dr(ColumnName), Boolean), Nothing)
Catch exBoolean1 As ArgumentException
'Try To Date
Try
propertyObject.SetValue(instance, CType(dr(ColumnName), Date), Nothing)
Catch ex As Exception
Throw ex
End Try
Catch exBoolean2 As InvalidCastException
'Try To Date
Try
propertyObject.SetValue(instance, CType(dr(ColumnName), Date), Nothing)
Catch ex As Exception
Throw ex
End Try
End Try
Catch exDouble2 As InvalidCastException
'Try to Boolean
Try
propertyObject.SetValue(instance, CType(dr(ColumnName), Boolean), Nothing)
Catch exBoolean1 As ArgumentException
'Try To Date
Try
propertyObject.SetValue(instance, CType(dr(ColumnName), Date), Nothing)
Catch ex As Exception
Throw ex
End Try
Catch exBoolean2 As InvalidCastException
'Try To Date
Try
propertyObject.SetValue(instance, CType(dr(ColumnName), Date), Nothing)
Catch ex As Exception
Throw ex
End Try
End Try
End Try
Catch exDecimal2 As InvalidCastException
'Try to Double
Try
propertyObject.SetValue(instance, CType(dr(ColumnName), Double), Nothing)
Catch exDouble1 As ArgumentException
'Try to Boolean
Try
propertyObject.SetValue(instance, CType(dr(ColumnName), Boolean), Nothing)
Catch exBoolean1 As ArgumentException
'Try To Date
Try
propertyObject.SetValue(instance, CType(dr(ColumnName), Date), Nothing)
Catch ex As Exception
Throw ex
End Try
Catch exBoolean2 As InvalidCastException
'Try To Date
Try
propertyObject.SetValue(instance, CType(dr(ColumnName), Date), Nothing)
Catch ex As Exception
Throw ex
End Try
End Try
Catch exDouble2 As InvalidCastException
'Try to Boolean
Try
propertyObject.SetValue(instance, CType(dr(ColumnName), Boolean), Nothing)
Catch exBoolean1 As ArgumentException
'Try To Date
Try
propertyObject.SetValue(instance, CType(dr(ColumnName), Date), Nothing)
Catch ex As Exception
Throw ex
End Try
Catch exBoolean2 As InvalidCastException
'Try To Date
Try
propertyObject.SetValue(instance, CType(dr(ColumnName), Date), Nothing)
Catch ex As Exception
Throw ex
End Try
End Try
End Try
End Try
Catch exDate2 As InvalidCastException
'Try to Decimal
Try
propertyObject.SetValue(instance, CType(dr(ColumnName), Decimal), Nothing)
Catch exDecimal1 As ArgumentException
'Try to Double
Try
propertyObject.SetValue(instance, CType(dr(ColumnName), Double), Nothing)
Catch exDouble1 As ArgumentException
'Try to Boolean
Try
propertyObject.SetValue(instance, CType(dr(ColumnName), Boolean), Nothing)
Catch exBoolean1 As ArgumentException
'Try To Date
Try
propertyObject.SetValue(instance, CType(dr(ColumnName), Date), Nothing)
Catch ex As Exception
Throw ex
End Try
Catch exBoolean2 As InvalidCastException
'Try To Date
Try
propertyObject.SetValue(instance, CType(dr(ColumnName), Date), Nothing)
Catch ex As Exception
Throw ex
End Try
End Try
Catch exDouble2 As InvalidCastException
'Try to Boolean
Try
propertyObject.SetValue(instance, CType(dr(ColumnName), Boolean), Nothing)
Catch exBoolean1 As ArgumentException
'Try To Date
Try
propertyObject.SetValue(instance, CType(dr(ColumnName), Date), Nothing)
Catch ex As Exception
Throw ex
End Try
Catch exBoolean2 As InvalidCastException
'Try To Date
Try
propertyObject.SetValue(instance, CType(dr(ColumnName), Date), Nothing)
Catch ex As Exception
Throw ex
End Try
End Try
End Try
Catch exDecimal2 As InvalidCastException
'Try to Double
Try
propertyObject.SetValue(instance, CType(dr(ColumnName), Double), Nothing)
Catch exDouble1 As ArgumentException
'Try to Boolean
Try
propertyObject.SetValue(instance, CType(dr(ColumnName), Boolean), Nothing)
Catch exBoolean1 As ArgumentException
'Try To Date
Try
propertyObject.SetValue(instance, CType(dr(ColumnName), Date), Nothing)
Catch ex As Exception
Throw ex
End Try
Catch exBoolean2 As InvalidCastException
'Try To Date
Try
propertyObject.SetValue(instance, CType(dr(ColumnName), Date), Nothing)
Catch ex As Exception
Throw ex
End Try
End Try
Catch exDouble2 As InvalidCastException
'Try to Boolean
Try
propertyObject.SetValue(instance, CType(dr(ColumnName), Boolean), Nothing)
Catch exBoolean1 As ArgumentException
'Try To Date
Try
propertyObject.SetValue(instance, CType(dr(ColumnName), Date), Nothing)
Catch ex As Exception
Throw ex
End Try
Catch exBoolean2 As InvalidCastException
'Try To Date
Try
propertyObject.SetValue(instance, CType(dr(ColumnName), Date), Nothing)
Catch ex As Exception
Throw ex
End Try
End Try
End Try
End Try
End Try
End Try
End Try
Catch exDefault2 As ArgumentException
Try
'Try to String
propertyObject.SetValue(instance, CType(dr(ColumnName), String), Nothing)
Catch exString1 As InvalidCastException
'Try to Integer
Try
propertyObject.SetValue(instance, CType(dr(ColumnName), Integer), Nothing)
Catch exInt32a As ArgumentException
'Try to DateTime
Try
propertyObject.SetValue(instance, CType(dr(ColumnName), DateTime), Nothing)
Catch exDate1 As ArgumentException
'Try to Decimal
Try
propertyObject.SetValue(instance, CType(dr(ColumnName), Decimal), Nothing)
Catch exDecimal1 As ArgumentException
'Try to Double
Try
propertyObject.SetValue(instance, CType(dr(ColumnName), Double), Nothing)
Catch exDouble1 As ArgumentException
'Try to Boolean
Try
propertyObject.SetValue(instance, CType(dr(ColumnName), Boolean), Nothing)
Catch exBoolean1 As ArgumentException
'Try To Date
Try
propertyObject.SetValue(instance, CType(dr(ColumnName), Date), Nothing)
Catch ex As Exception
Throw ex
End Try
Catch exBoolean2 As InvalidCastException
'Try To Date
Try
propertyObject.SetValue(instance, CType(dr(ColumnName), Date), Nothing)
Catch ex As Exception
Throw ex
End Try
End Try
Catch exDouble2 As InvalidCastException
'Try to Boolean
Try
propertyObject.SetValue(instance, CType(dr(ColumnName), Boolean), Nothing)
Catch exBoolean1 As ArgumentException
'Try To Date
Try
propertyObject.SetValue(instance, CType(dr(ColumnName), Date), Nothing)
Catch ex As Exception
Throw ex
End Try
Catch exBoolean2 As InvalidCastException
'Try To Date
Try
propertyObject.SetValue(instance, CType(dr(ColumnName), Date), Nothing)
Catch ex As Exception
Throw ex
End Try
End Try
End Try
Catch exDecimal2 As InvalidCastException
'Try to Double
Try
propertyObject.SetValue(instance, CType(dr(ColumnName), Double), Nothing)
Catch exDouble1 As ArgumentException
'Try to Boolean
Try
propertyObject.SetValue(instance, CType(dr(ColumnName), Boolean), Nothing)
Catch exBoolean1 As ArgumentException
'Try To Date
Try
propertyObject.SetValue(instance, CType(dr(ColumnName), Date), Nothing)
Catch ex As Exception
Throw ex
End Try
Catch exBoolean2 As InvalidCastException
'Try To Date
Try
propertyObject.SetValue(instance, CType(dr(ColumnName), Date), Nothing)
Catch ex As Exception
Throw ex
End Try
End Try
Catch exDouble2 As InvalidCastException
'Try to Boolean
Try
propertyObject.SetValue(instance, CType(dr(ColumnName), Boolean), Nothing)
Catch exBoolean1 As ArgumentException
'Try To Date
Try
propertyObject.SetValue(instance, CType(dr(ColumnName), Date), Nothing)
Catch ex As Exception
Throw ex
End Try
Catch exBoolean2 As InvalidCastException
'Try To Date
Try
propertyObject.SetValue(instance, CType(dr(ColumnName), Date), Nothing)
Catch ex As Exception
Throw ex
End Try
End Try
End Try
End Try
Catch exDate2 As InvalidCastException
'Try to Decimal
Try
propertyObject.SetValue(instance, CType(dr(ColumnName), Decimal), Nothing)
Catch exDecimal1 As ArgumentException
'Try to Double
Try
propertyObject.SetValue(instance, CType(dr(ColumnName), Double), Nothing)
Catch exDouble1 As ArgumentException
'Try to Boolean
Try
propertyObject.SetValue(instance, CType(dr(ColumnName), Boolean), Nothing)
Catch exBoolean1 As ArgumentException
'Try To Date
Try
propertyObject.SetValue(instance, CType(dr(ColumnName), Date), Nothing)
Catch ex As Exception
Throw ex
End Try
Catch exBoolean2 As InvalidCastException
'Try To Date
Try
propertyObject.SetValue(instance, CType(dr(ColumnName), Date), Nothing)
Catch ex As Exception
Throw ex
End Try
End Try
Catch exDouble2 As InvalidCastException
'Try to Boolean
Try
propertyObject.SetValue(instance, CType(dr(ColumnName), Boolean), Nothing)
Catch exBoolean1 As ArgumentException
'Try To Date
Try
propertyObject.SetValue(instance, CType(dr(ColumnName), Date), Nothing)
Catch ex As Exception
Throw ex
End Try
Catch exBoolean2 As InvalidCastException
'Try To Date
Try
propertyObject.SetValue(instance, CType(dr(ColumnName), Date), Nothing)
Catch ex As Exception
Throw ex
End Try
End Try
End Try
Catch exDecimal2 As InvalidCastException
'Try to Double
Try
propertyObject.SetValue(instance, CType(dr(ColumnName), Double), Nothing)
Catch exDouble1 As ArgumentException
'Try to Boolean
Try
propertyObject.SetValue(instance, CType(dr(ColumnName), Boolean), Nothing)
Catch exBoolean1 As ArgumentException
'Try To Date
Try
propertyObject.SetValue(instance, CType(dr(ColumnName), Date), Nothing)
Catch ex As Exception
Throw ex
End Try
Catch exBoolean2 As InvalidCastException
'Try To Date
Try
propertyObject.SetValue(instance, CType(dr(ColumnName), Date), Nothing)
Catch ex As Exception
Throw ex
End Try
End Try
Catch exDouble2 As InvalidCastException
'Try to Boolean
Try
propertyObject.SetValue(instance, CType(dr(ColumnName), Boolean), Nothing)
Catch exBoolean1 As ArgumentException
'Try To Date
Try
propertyObject.SetValue(instance, CType(dr(ColumnName), Date), Nothing)
Catch ex As Exception
Throw ex
End Try
Catch exBoolean2 As InvalidCastException
'Try To Date
Try
propertyObject.SetValue(instance, CType(dr(ColumnName), Date), Nothing)
Catch ex As Exception
Throw ex
End Try
End Try
End Try
End Try
End Try
Catch exInt32b As InvalidCastException
'Try to DateTime
Try
propertyObject.SetValue(instance, CType(dr(ColumnName), DateTime), Nothing)
Catch exDate1 As ArgumentException
'Try to Decimal
Try
propertyObject.SetValue(instance, CType(dr(ColumnName), Decimal), Nothing)
Catch exDecimal1 As ArgumentException
'Try to Double
Try
propertyObject.SetValue(instance, CType(dr(ColumnName), Double), Nothing)
Catch exDouble1 As ArgumentException
'Try to Boolean
Try
propertyObject.SetValue(instance, CType(dr(ColumnName), Boolean), Nothing)
Catch exBoolean1 As ArgumentException
'Try To Date
Try
propertyObject.SetValue(instance, CType(dr(ColumnName), Date), Nothing)
Catch ex As Exception
Throw ex
End Try
Catch exBoolean2 As InvalidCastException
'Try To Date
Try
propertyObject.SetValue(instance, CType(dr(ColumnName), Date), Nothing)
Catch ex As Exception
Throw ex
End Try
End Try
Catch exDouble2 As InvalidCastException
'Try to Boolean
Try
propertyObject.SetValue(instance, CType(dr(ColumnName), Boolean), Nothing)
Catch exBoolean1 As ArgumentException
'Try To Date
Try
propertyObject.SetValue(instance, CType(dr(ColumnName), Date), Nothing)
Catch ex As Exception
Throw ex
End Try
Catch exBoolean2 As InvalidCastException
'Try To Date
Try
propertyObject.SetValue(instance, CType(dr(ColumnName), Date), Nothing)
Catch ex As Exception
Throw ex
End Try
End Try
End Try
Catch exDecimal2 As InvalidCastException
'Try to Double
Try
propertyObject.SetValue(instance, CType(dr(ColumnName), Double), Nothing)
Catch exDouble1 As ArgumentException
'Try to Boolean
Try
propertyObject.SetValue(instance, CType(dr(ColumnName), Boolean), Nothing)
Catch exBoolean1 As ArgumentException
'Try To Date
Try
propertyObject.SetValue(instance, CType(dr(ColumnName), Date), Nothing)
Catch ex As Exception
Throw ex
End Try
Catch exBoolean2 As InvalidCastException
'Try To Date
Try
propertyObject.SetValue(instance, CType(dr(ColumnName), Date), Nothing)
Catch ex As Exception
Throw ex
End Try
End Try
Catch exDouble2 As InvalidCastException
'Try to Boolean
Try
propertyObject.SetValue(instance, CType(dr(ColumnName), Boolean), Nothing)
Catch exBoolean1 As ArgumentException
'Try To Date
Try
propertyObject.SetValue(instance, CType(dr(ColumnName), Date), Nothing)
Catch ex As Exception
Throw ex
End Try
Catch exBoolean2 As InvalidCastException
'Try To Date
Try
propertyObject.SetValue(instance, CType(dr(ColumnName), Date), Nothing)
Catch ex As Exception
Throw ex
End Try
End Try
End Try
End Try
Catch exDate2 As InvalidCastException
'Try to Decimal
Try
propertyObject.SetValue(instance, CType(dr(ColumnName), Decimal), Nothing)
Catch exDecimal1 As ArgumentException
'Try to Double
Try
propertyObject.SetValue(instance, CType(dr(ColumnName), Double), Nothing)
Catch exDouble1 As ArgumentException
'Try to Boolean
Try
propertyObject.SetValue(instance, CType(dr(ColumnName), Boolean), Nothing)
Catch exBoolean1 As ArgumentException
'Try To Date
Try
propertyObject.SetValue(instance, CType(dr(ColumnName), Date), Nothing)
Catch ex As Exception
Throw ex
End Try
Catch exBoolean2 As InvalidCastException
'Try To Date
Try
propertyObject.SetValue(instance, CType(dr(ColumnName), Date), Nothing)
Catch ex As Exception
Throw ex
End Try
End Try
Catch exDouble2 As InvalidCastException
'Try to Boolean
Try
propertyObject.SetValue(instance, CType(dr(ColumnName), Boolean), Nothing)
Catch exBoolean1 As ArgumentException
'Try To Date
Try
propertyObject.SetValue(instance, CType(dr(ColumnName), Date), Nothing)
Catch ex As Exception
Throw ex
End Try
Catch exBoolean2 As InvalidCastException
'Try To Date
Try
propertyObject.SetValue(instance, CType(dr(ColumnName), Date), Nothing)
Catch ex As Exception
Throw ex
End Try
End Try
End Try
Catch exDecimal2 As InvalidCastException
'Try to Double
Try
propertyObject.SetValue(instance, CType(dr(ColumnName), Double), Nothing)
Catch exDouble1 As ArgumentException
'Try to Boolean
Try
propertyObject.SetValue(instance, CType(dr(ColumnName), Boolean), Nothing)
Catch exBoolean1 As ArgumentException
'Try To Date
Try
propertyObject.SetValue(instance, CType(dr(ColumnName), Date), Nothing)
Catch ex As Exception
Throw ex
End Try
Catch exBoolean2 As InvalidCastException
'Try To Date
Try
propertyObject.SetValue(instance, CType(dr(ColumnName), Date), Nothing)
Catch ex As Exception
Throw ex
End Try
End Try
Catch exDouble2 As InvalidCastException
'Try to Boolean
Try
propertyObject.SetValue(instance, CType(dr(ColumnName), Boolean), Nothing)
Catch exBoolean1 As ArgumentException
'Try To Date
Try
propertyObject.SetValue(instance, CType(dr(ColumnName), Date), Nothing)
Catch ex As Exception
Throw ex
End Try
Catch exBoolean2 As InvalidCastException
'Try To Date
Try
propertyObject.SetValue(instance, CType(dr(ColumnName), Date), Nothing)
Catch ex As Exception
Throw ex
End Try
End Try
End Try
End Try
End Try
End Try
Catch exString2 As ArgumentException
'Try to Integer
Try
propertyObject.SetValue(instance, CType(dr(ColumnName), Integer), Nothing)
Catch exInt32a As ArgumentException
'Try to DateTime
Try
propertyObject.SetValue(instance, CType(dr(ColumnName), DateTime), Nothing)
Catch exDate1 As ArgumentException
'Try to Decimal
Try
propertyObject.SetValue(instance, CType(dr(ColumnName), Decimal), Nothing)
Catch exDecimal1 As ArgumentException
'Try to Double
Try
propertyObject.SetValue(instance, CType(dr(ColumnName), Double), Nothing)
Catch exDouble1 As ArgumentException
'Try to Boolean
Try
propertyObject.SetValue(instance, CType(dr(ColumnName), Boolean), Nothing)
Catch exBoolean1 As ArgumentException
'Try To Date
Try
propertyObject.SetValue(instance, CType(dr(ColumnName), Date), Nothing)
Catch ex As Exception
Throw ex
End Try
Catch exBoolean2 As InvalidCastException
'Try To Date
Try
propertyObject.SetValue(instance, CType(dr(ColumnName), Date), Nothing)
Catch ex As Exception
Throw ex
End Try
End Try
Catch exDouble2 As InvalidCastException
'Try to Boolean
Try
propertyObject.SetValue(instance, CType(dr(ColumnName), Boolean), Nothing)
Catch exBoolean1 As ArgumentException
'Try To Date
Try
propertyObject.SetValue(instance, CType(dr(ColumnName), Date), Nothing)
Catch ex As Exception
Throw ex
End Try
Catch exBoolean2 As InvalidCastException
'Try To Date
Try
propertyObject.SetValue(instance, CType(dr(ColumnName), Date), Nothing)
Catch ex As Exception
Throw ex
End Try
End Try
End Try
Catch exDecimal2 As InvalidCastException
'Try to Double
Try
propertyObject.SetValue(instance, CType(dr(ColumnName), Double), Nothing)
Catch exDouble1 As ArgumentException
'Try to Boolean
Try
propertyObject.SetValue(instance, CType(dr(ColumnName), Boolean), Nothing)
Catch exBoolean1 As ArgumentException
'Try To Date
Try
propertyObject.SetValue(instance, CType(dr(ColumnName), Date), Nothing)
Catch ex As Exception
Throw ex
End Try
Catch exBoolean2 As InvalidCastException
'Try To Date
Try
propertyObject.SetValue(instance, CType(dr(ColumnName), Date), Nothing)
Catch ex As Exception
Throw ex
End Try
End Try
Catch exDouble2 As InvalidCastException
'Try to Boolean
Try
propertyObject.SetValue(instance, CType(dr(ColumnName), Boolean), Nothing)
Catch exBoolean1 As ArgumentException
'Try To Date
Try
propertyObject.SetValue(instance, CType(dr(ColumnName), Date), Nothing)
Catch ex As Exception
Throw ex
End Try
Catch exBoolean2 As InvalidCastException
'Try To Date
Try
propertyObject.SetValue(instance, CType(dr(ColumnName), Date), Nothing)
Catch ex As Exception
Throw ex
End Try
End Try
End Try
End Try
Catch exDate2 As InvalidCastException
'Try to Decimal
Try
propertyObject.SetValue(instance, CType(dr(ColumnName), Decimal), Nothing)
Catch exDecimal1 As ArgumentException
'Try to Double
Try
propertyObject.SetValue(instance, CType(dr(ColumnName), Double), Nothing)
Catch exDouble1 As ArgumentException
'Try to Boolean
Try
propertyObject.SetValue(instance, CType(dr(ColumnName), Boolean), Nothing)
Catch exBoolean1 As ArgumentException
'Try To Date
Try
propertyObject.SetValue(instance, CType(dr(ColumnName), Date), Nothing)
Catch ex As Exception
Throw ex
End Try
Catch exBoolean2 As InvalidCastException
'Try To Date
Try
propertyObject.SetValue(instance, CType(dr(ColumnName), Date), Nothing)
Catch ex As Exception
Throw ex
End Try
End Try
Catch exDouble2 As InvalidCastException
'Try to Boolean
Try
propertyObject.SetValue(instance, CType(dr(ColumnName), Boolean), Nothing)
Catch exBoolean1 As ArgumentException
'Try To Date
Try
propertyObject.SetValue(instance, CType(dr(ColumnName), Date), Nothing)
Catch ex As Exception
Throw ex
End Try
Catch exBoolean2 As InvalidCastException
'Try To Date
Try
propertyObject.SetValue(instance, CType(dr(ColumnName), Date), Nothing)
Catch ex As Exception
Throw ex
End Try
End Try
End Try
Catch exDecimal2 As InvalidCastException
'Try to Double
Try
propertyObject.SetValue(instance, CType(dr(ColumnName), Double), Nothing)
Catch exDouble1 As ArgumentException
'Try to Boolean
Try
propertyObject.SetValue(instance, CType(dr(ColumnName), Boolean), Nothing)
Catch exBoolean1 As ArgumentException
'Try To Date
Try
propertyObject.SetValue(instance, CType(dr(ColumnName), Date), Nothing)
Catch ex As Exception
Throw ex
End Try
Catch exBoolean2 As InvalidCastException
'Try To Date
Try
propertyObject.SetValue(instance, CType(dr(ColumnName), Date), Nothing)
Catch ex As Exception
Throw ex
End Try
End Try
Catch exDouble2 As InvalidCastException
'Try to Boolean
Try
propertyObject.SetValue(instance, CType(dr(ColumnName), Boolean), Nothing)
Catch exBoolean1 As ArgumentException
'Try To Date
Try
propertyObject.SetValue(instance, CType(dr(ColumnName), Date), Nothing)
Catch ex As Exception
Throw ex
End Try
Catch exBoolean2 As InvalidCastException
'Try To Date
Try
propertyObject.SetValue(instance, CType(dr(ColumnName), Date), Nothing)
Catch ex As Exception
Throw ex
End Try
End Try
End Try
End Try
End Try
Catch exInt32b As InvalidCastException
'Try to DateTime
Try
propertyObject.SetValue(instance, CType(dr(ColumnName), DateTime), Nothing)
Catch exDate1 As ArgumentException
'Try to Decimal
Try
propertyObject.SetValue(instance, CType(dr(ColumnName), Decimal), Nothing)
Catch exDecimal1 As ArgumentException
'Try to Double
Try
propertyObject.SetValue(instance, CType(dr(ColumnName), Double), Nothing)
Catch exDouble1 As ArgumentException
'Try to Boolean
Try
propertyObject.SetValue(instance, CType(dr(ColumnName), Boolean), Nothing)
Catch exBoolean1 As ArgumentException
'Try To Date
Try
propertyObject.SetValue(instance, CType(dr(ColumnName), Date), Nothing)
Catch ex As Exception
Throw ex
End Try
Catch exBoolean2 As InvalidCastException
'Try To Date
Try
propertyObject.SetValue(instance, CType(dr(ColumnName), Date), Nothing)
Catch ex As Exception
Throw ex
End Try
End Try
Catch exDouble2 As InvalidCastException
'Try to Boolean
Try
propertyObject.SetValue(instance, CType(dr(ColumnName), Boolean), Nothing)
Catch exBoolean1 As ArgumentException
'Try To Date
Try
propertyObject.SetValue(instance, CType(dr(ColumnName), Date), Nothing)
Catch ex As Exception
Throw ex
End Try
Catch exBoolean2 As InvalidCastException
'Try To Date
Try
propertyObject.SetValue(instance, CType(dr(ColumnName), Date), Nothing)
Catch ex As Exception
Throw ex
End Try
End Try
End Try
Catch exDecimal2 As InvalidCastException
'Try to Double
Try
propertyObject.SetValue(instance, CType(dr(ColumnName), Double), Nothing)
Catch exDouble1 As ArgumentException
'Try to Boolean
Try
propertyObject.SetValue(instance, CType(dr(ColumnName), Boolean), Nothing)
Catch exBoolean1 As ArgumentException
'Try To Date
Try
propertyObject.SetValue(instance, CType(dr(ColumnName), Date), Nothing)
Catch ex As Exception
Throw ex
End Try
Catch exBoolean2 As InvalidCastException
'Try To Date
Try
propertyObject.SetValue(instance, CType(dr(ColumnName), Date), Nothing)
Catch ex As Exception
Throw ex
End Try
End Try
Catch exDouble2 As InvalidCastException
'Try to Boolean
Try
propertyObject.SetValue(instance, CType(dr(ColumnName), Boolean), Nothing)
Catch exBoolean1 As ArgumentException
'Try To Date
Try
propertyObject.SetValue(instance, CType(dr(ColumnName), Date), Nothing)
Catch ex As Exception
Throw ex
End Try
Catch exBoolean2 As InvalidCastException
'Try To Date
Try
propertyObject.SetValue(instance, CType(dr(ColumnName), Date), Nothing)
Catch ex As Exception
Throw ex
End Try
End Try
End Try
End Try
Catch exDate2 As InvalidCastException
'Try to Decimal
Try
propertyObject.SetValue(instance, CType(dr(ColumnName), Decimal), Nothing)
Catch exDecimal1 As ArgumentException
'Try to Double
Try
propertyObject.SetValue(instance, CType(dr(ColumnName), Double), Nothing)
Catch exDouble1 As ArgumentException
'Try to Boolean
Try
propertyObject.SetValue(instance, CType(dr(ColumnName), Boolean), Nothing)
Catch exBoolean1 As ArgumentException
'Try To Date
Try
propertyObject.SetValue(instance, CType(dr(ColumnName), Date), Nothing)
Catch ex As Exception
Throw ex
End Try
Catch exBoolean2 As InvalidCastException
'Try To Date
Try
propertyObject.SetValue(instance, CType(dr(ColumnName), Date), Nothing)
Catch ex As Exception
Throw ex
End Try
End Try
Catch exDouble2 As InvalidCastException
'Try to Boolean
Try
propertyObject.SetValue(instance, CType(dr(ColumnName), Boolean), Nothing)
Catch exBoolean1 As ArgumentException
'Try To Date
Try
propertyObject.SetValue(instance, CType(dr(ColumnName), Date), Nothing)
Catch ex As Exception
Throw ex
End Try
Catch exBoolean2 As InvalidCastException
'Try To Date
Try
propertyObject.SetValue(instance, CType(dr(ColumnName), Date), Nothing)
Catch ex As Exception
Throw ex
End Try
End Try
End Try
Catch exDecimal2 As InvalidCastException
'Try to Double
Try
propertyObject.SetValue(instance, CType(dr(ColumnName), Double), Nothing)
Catch exDouble1 As ArgumentException
'Try to Boolean
Try
propertyObject.SetValue(instance, CType(dr(ColumnName), Boolean), Nothing)
Catch exBoolean1 As ArgumentException
'Try To Date
Try
propertyObject.SetValue(instance, CType(dr(ColumnName), Date), Nothing)
Catch ex As Exception
Throw ex
End Try
Catch exBoolean2 As InvalidCastException
'Try To Date
Try
propertyObject.SetValue(instance, CType(dr(ColumnName), Date), Nothing)
Catch ex As Exception
Throw ex
End Try
End Try
Catch exDouble2 As InvalidCastException
'Try to Boolean
Try
propertyObject.SetValue(instance, CType(dr(ColumnName), Boolean), Nothing)
Catch exBoolean1 As ArgumentException
'Try To Date
Try
propertyObject.SetValue(instance, CType(dr(ColumnName), Date), Nothing)
Catch ex As Exception
Throw ex
End Try
Catch exBoolean2 As InvalidCastException
'Try To Date
Try
propertyObject.SetValue(instance, CType(dr(ColumnName), Date), Nothing)
Catch ex As Exception
Throw ex
End Try
End Try
End Try
End Try
End Try
End Try
End Try
End Try
End Sub

Maybe I’ll get lucky and someone will post up a “Oh you can just do this, which is new in .Net 3.5 or 4.0.. “ and I can get rid of this thing. But hey it works!

Resource: How to Consume WCF and ASP.Net Web Services in Silverlight

Casting Types in VB.Net and More

Source: Cheat Sheet - Casting in VB.NET and C#

Great little find, and since I’m a VB.Net whore, I’ll just post up the VB.Net version :)

Casting in VB.NET

  1. By default in VB, casting is automatically done for you when you assign objects to variables. The objects are then automatically casted to the variables' type.

    This behavior can be influenced by an option line on top of your code file:

    Option Strict On
    Option Strict Off

    When on, casting is strict and not automatic.

  2. Explicit casting can be done with the cast operator CType() or DirectCast():

    textbox = CType(obj, TextBox)
    textbox = DirectCast(obj, TextBox)


    The difference between the two keywords is that CType succeeds as long as there is a valid conversion defined between the expression and the type, whereas DirectCast requires the run-time type of an object variable to be the same as the specified type. If the specified type and the run-time type of the expression are the same, however, the run-time performance of DirectCast is better than that of CType. DirectCast throws an InvalidCastException error if the argument types do not match.

  3. Testing if an object is of a particular type, can be done with the TypeOf...Is operator:

    If TypeOf obj Is TextBox Then...
  4. Obtaining a System.Type object for a given type can be done with the GetType operator:

    Dim t As System.Type
    t = GetType(String)
    MessageBox.Show(t.FullName)
  5. Obtaining a System.Type object for a given object can be done with the GetType method:

    Dim t as System.Type
    t = obj.GetType()
    MessageBox.Show(t.FullName)

Now to build on and add one more of my own…

Get the type based on an arbitrary string.

System.Type.GetType("System.String")

Problem with this though is you can not use it to cast using CType or DirectCast. Looking at the CType Function..

typename - Any expression that is legal within an As clause in a Dim statement, that is, the name of any data type, object, structure, class, or interface.

You can only use types that are compiled and can’t use types that show up at runtime. So trying to use CType(obj, System.Type.GetType(“System.String”)) will only get you an error.  Array bounds cannot appear in type specifiers.

How to get around this? I have no freaking idea, yet. I’m think I might could create a custom type of my own and do something so that it converts to the proper method but for now I’ll just catch a few ArgumentExceptions and try different cast types then.

Set the DefaultButton inside a LoginView Control in a MasterPage

Ok if you google this you’ll get some solutions that say set the DefaultButton to ParentControlID$ButtonID

However, this didn’t work for me. I kept getting the same error no matter what I tried.

The DefaultButton of '<PanelID>' must be the ID of a control of type IButtonControl.

So I tried another approach that worked great.

On page load I found the Panel Control and found the button control and set the default button programmatically.

<asp:LoginView ID="LoginView1" runat="server">
    <AnonymousTemplate>
        <asp:Panel ID="pnlLogin" runat="server">
            <telerik:RadTextBox ID="tbUsername" runat="server" EmptyMessage="Username" />&nbsp;
            <telerik:RadTextBox ID="tbPassword" runat="server" EmptyMessage="Password" TextMode="Password" />&nbsp;
            <div id="SignIn" style="position: relative; z-index: 1; float: right; bottom: 21px;">
                 <asp:LinkButton ID="lbSignIn" runat="server" Font-Bold="true" Font-Size="Small" Text="SIGN IN" OnClick="lbSignIn_Click" />
            </div>
            <div id="divLoginLinks" style="position:relative; clear:both">
                <span class="GrayFooterFont"><asp:HyperLink ID="hlRegister" runat="server" Text="Register" NavigateUrl="~/Registration.aspx" /> | <asp:HyperLink ID="hlForgot" runat="server" Text="Forgot Password" NavigateUrl="~/ForgotPassword.aspx" /></span>
            </div>
        </asp:Panel>
    </AnonymousTemplate>
    <LoggedInTemplate>
        <small><asp:Label ID="lblLoggedInUsername" runat="server" /> | <asp:LinkButton ID="lbSignOut" runat="server" Text="SIGN OUT" OnClick="lbSignOut_Click" /></small>
        <asp:LinkButton ID="lbSignIn" runat="server" Font-Bold="true" Font-Size="Small" Text="SIGN IN" OnClick="lbSignIn_Click" CssClass="hidden" />
    </LoggedInTemplate>
</asp:LoginView>

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    'If User is not logged in do this        
    Dim pnlLogin As Panel
    Dim lbSignIn As LinkButton
    pnlLogin = CType(LoginView1.Controls(0).FindControl("pnlLogin"), Panel)
    lbSignIn = CType(LoginView1.Controls(0).FindControl("lbSignIn"), LinkButton)
    pnlLogin.DefaultButton = lbSignIn.ID
    pnlLogin = Nothing
    lbSignIn = Nothing
End Sub

I’m sure I could have just hardcoded the lblSignIn ID to the pnlLogin, but no idea why I didn’t.

SilverLight Reference Controls

Reference a control that’s is outside your View, or on your MainPage if you’re using NavigationFrame or your “Parent” window.

Assumption: MainPage UserControl page is your starting page and has a NavigationFrame and a View is loaded in a content window.

Method 1: (Assuming “MainPage” is the name of your MainPage or the x:Class it uses, x:Class="<AppName>.MainPage" and the TextBlock named “lblStep1”)

Dim Main As MainPage = CType(<AppName>.App.Current.RootVisual, MainPage)
        CType(Main.FindName("lblStep1"), TextBlock).Text = "Hello Test123"

Or Method 2:

CType(CType(<AppName>.App.Current.RootVisual, MainPage).FindName("lblStep1"), TextBlock).Text = "Updated Text"
 

 

Reference a Control in a User Control that’s on the same view.

  1. CType(siTest.FindName("lblStep1"), TextBlock).Text = "Testing"

Call your Win Service from your Web Application

Some quick code snippets from my brother.  If you have a running Windows Service, you can call it from your Web Application to run a custom command. UPDATE: There’s some better VB Sample Code here and quick and easy of both the Windows Service and the ASP.Net

So you have your Win Service named “Encoder” and when you say, upload a video, you then call the Service and send it an argument value of 10.  Then in your web service when it see��s the value 10 come in, it knows to start encoding any videos uploaded. 

Here’s some sample code:

Web application must have full trust. Partial trust not allowed.

In your application: (add reference to System.ServiceProcess.dll)

System.ServiceProcess.ServiceController controller = new System.ServiceProcess.ServiceController(“serviceName”)
controller.ExecuteCommand(commandinteger);


In your Win service here is your code:

protected override void OnCustomCommand(int commandinteger){
 
switch(commandinteger){
case 129:
//do something
break;
case 130:
//do something else
break;
}
 
}

commandinteger values must be between 128 and 256.

128 and below are system reserved.

Check the link below for msdn description
http://msdn.microsoft.com/en-us/library/system.serviceprocess.servicebase.oncustomcommand.aspx

http://msdn.microsoft.com/en-us/library/system.serviceprocess.servicebase.oncustomcommand(VS.80).aspx

Found this blog post with more info, cause I’m lazy :p

http://blogs.msdn.com/kaevans/archive/2005/03/17/397505.aspx

Updated Blog Post On This Topic

State DropDown List

My list for project when I need a State DropDownList

Full State Names using asp:DropDownList

<asp:DropDownList ID="ddlStates" runat="server">
    <asp:ListItem Value="AL">Alabama</asp:ListItem>
    <asp:ListItem Value="AK">Alaska</asp:ListItem>
    <asp:ListItem Value="AZ">Arizona</asp:ListItem>
    <asp:ListItem Value="AR">Arkansas</asp:ListItem>
    <asp:ListItem Value="CA">California</asp:ListItem>
    <asp:ListItem Value="CO">Colorado</asp:ListItem>
    <asp:ListItem Value="CT">Connecticut</asp:ListItem>
    <asp:ListItem Value="DC">District of Columbia</asp:ListItem>
    <asp:ListItem Value="DE">Delaware</asp:ListItem>
    <asp:ListItem Value="FL">Florida</asp:ListItem>
    <asp:ListItem Value="GA">Georgia</asp:ListItem>
    <asp:ListItem Value="HI">Hawaii</asp:ListItem>
    <asp:ListItem Value="ID">Idaho</asp:ListItem>
    <asp:ListItem Value="IL">Illinois</asp:ListItem>
    <asp:ListItem Value="IN">Indiana</asp:ListItem>
    <asp:ListItem Value="IA">Iowa</asp:ListItem>
    <asp:ListItem Value="KS">Kansas</asp:ListItem>
    <asp:ListItem Value="KY">Kentucky</asp:ListItem>
    <asp:ListItem Value="LA">Louisiana</asp:ListItem>
    <asp:ListItem Value="ME">Maine</asp:ListItem>
    <asp:ListItem Value="MD">Maryland</asp:ListItem>
    <asp:ListItem Value="MA">Massachusetts</asp:ListItem>
    <asp:ListItem Value="MI">Michigan</asp:ListItem>
    <asp:ListItem Value="MN">Minnesota</asp:ListItem>
    <asp:ListItem Value="MS">Mississippi</asp:ListItem>
    <asp:ListItem Value="MO">Missouri</asp:ListItem>
    <asp:ListItem Value="MT">Montana</asp:ListItem>
    <asp:ListItem Value="NE">Nebraska</asp:ListItem>
    <asp:ListItem Value="NV">Nevada</asp:ListItem>
    <asp:ListItem Value="NH">New Hampshire</asp:ListItem>
    <asp:ListItem Value="NJ">New Jersey</asp:ListItem>
    <asp:ListItem Value="NM">New Mexico</asp:ListItem>
    <asp:ListItem Value="NY">New York</asp:ListItem>
    <asp:ListItem Value="NC">North Carolina</asp:ListItem>
    <asp:ListItem Value="ND">North Dakota</asp:ListItem>
    <asp:ListItem Value="OH">Ohio</asp:ListItem>
    <asp:ListItem Value="OK">Oklahoma</asp:ListItem>
    <asp:ListItem Value="OR">Oregon</asp:ListItem>
    <asp:ListItem Value="PA">Pennsylvania</asp:ListItem>
    <asp:ListItem Value="RI">Rhode Island</asp:ListItem>
    <asp:ListItem Value="SC">South Carolina</asp:ListItem>
    <asp:ListItem Value="SD">South Dakota</asp:ListItem>
    <asp:ListItem Value="TN">Tennessee</asp:ListItem>
    <asp:ListItem Value="TX">Texas</asp:ListItem>
    <asp:ListItem Value="UT">Utah</asp:ListItem>
    <asp:ListItem Value="VT">Vermont</asp:ListItem>
    <asp:ListItem Value="VA">Virginia</asp:ListItem>
    <asp:ListItem Value="WA">Washington</asp:ListItem>
    <asp:ListItem Value="WV">West Virginia</asp:ListItem>
    <asp:ListItem Value="WI">Wisconsin</asp:ListItem>
    <asp:ListItem Value="WY">Wyoming</asp:ListItem>
</asp:DropDownList>

asp:DropDownList with Abbreviate States

<asp:DropDownList ID="ddlStates" runat="server">
    <asp:ListItem value="AK">AK</asp:ListItem>
    <asp:ListItem value="AL">AL</asp:ListItem>
    <asp:ListItem value="AR">AR</asp:ListItem>
    <asp:ListItem value="AZ">AZ</asp:ListItem>
    <asp:ListItem value="CA">CA</asp:ListItem>
    <asp:ListItem value="CO">CO</asp:ListItem>
    <asp:ListItem value="CT">CT</asp:ListItem>
    <asp:ListItem value="DC">DC</asp:ListItem>
    <asp:ListItem value="DE">DE</asp:ListItem>
    <asp:ListItem value="FL">FL</asp:ListItem>
    <asp:ListItem value="GA">GA</asp:ListItem>
    <asp:ListItem value="HI">HI</asp:ListItem>
    <asp:ListItem value="IA">IA</asp:ListItem>
    <asp:ListItem value="ID">ID</asp:ListItem>
    <asp:ListItem value="IL">IL</asp:ListItem>
    <asp:ListItem value="IN">IN</asp:ListItem>
    <asp:ListItem value="KS">KS</asp:ListItem>
    <asp:ListItem value="KY">KY</asp:ListItem>
    <asp:ListItem value="LA">LA</asp:ListItem>
    <asp:ListItem value="MA">MA</asp:ListItem>
    <asp:ListItem value="MD">MD</asp:ListItem>
    <asp:ListItem value="ME">ME</asp:ListItem>
    <asp:ListItem value="MI">MI</asp:ListItem>
    <asp:ListItem value="MN">MN</asp:ListItem>
    <asp:ListItem value="MO">MO</asp:ListItem>
    <asp:ListItem value="MS">MS</asp:ListItem>
    <asp:ListItem value="MT">MT</asp:ListItem>
    <asp:ListItem value="NC">NC</asp:ListItem>
    <asp:ListItem value="ND">ND</asp:ListItem>
    <asp:ListItem value="NE">NE</asp:ListItem>
    <asp:ListItem value="NH">NH</asp:ListItem>
    <asp:ListItem value="NJ">NJ</asp:ListItem>
    <asp:ListItem value="NM">NM</asp:ListItem>
    <asp:ListItem value="NV">NV</asp:ListItem>
    <asp:ListItem value="NY">NY</asp:ListItem>
    <asp:ListItem value="OH">OH</asp:ListItem>
    <asp:ListItem value="OK">OK</asp:ListItem>
    <asp:ListItem value="OR">OR</asp:ListItem>
    <asp:ListItem value="PA">PA</asp:ListItem>
    <asp:ListItem value="RI">RI</asp:ListItem>
    <asp:ListItem value="SC">SC</asp:ListItem>
    <asp:ListItem value="SD">SD</asp:ListItem>
    <asp:ListItem value="TN">TN</asp:ListItem>
    <asp:ListItem value="TX">TX</asp:ListItem>
    <asp:ListItem value="UT">UT</asp:ListItem>
    <asp:ListItem value="VA">VA</asp:ListItem>
    <asp:ListItem value="VT">VT</asp:ListItem>
    <asp:ListItem value="WA">WA</asp:ListItem>
    <asp:ListItem value="WI">WI</asp:ListItem>
    <asp:ListItem value="WV">WV</asp:ListItem>
    <asp:ListItem value="WY">WY</asp:ListItem>
</asp:DropDownList>

Or if you use Telerik controls like I do… then the RadComboBox is for you

<telerik:RadComboBox ID="RadComboBox1" runat="server">
    <Items>
        <telerik:RadComboBoxItem value="AK" Text="AK" />
        <telerik:RadComboBoxItem value="AL" Text="AL" />
        <telerik:RadComboBoxItem value="AR" Text="AR" />
        <telerik:RadComboBoxItem value="AZ" Text="AZ" />
        <telerik:RadComboBoxItem value="CA" Text="CA" />
        <telerik:RadComboBoxItem value="CO" Text="CO" />
        <telerik:RadComboBoxItem value="CT" Text="CT" />
        <telerik:RadComboBoxItem value="DC" Text="DC" />
        <telerik:RadComboBoxItem value="DE" Text="DE" />
        <telerik:RadComboBoxItem value="FL" Text="FL" />
        <telerik:RadComboBoxItem value="GA" Text="GA" />
        <telerik:RadComboBoxItem value="HI" Text="HI" />
        <telerik:RadComboBoxItem value="IA" Text="IA" />
        <telerik:RadComboBoxItem value="ID" Text="ID" />
        <telerik:RadComboBoxItem value="IL" Text="IL" />
        <telerik:RadComboBoxItem value="IN" Text="IN" />
        <telerik:RadComboBoxItem value="KS" Text="KS" />
        <telerik:RadComboBoxItem value="KY" Text="KY" />
        <telerik:RadComboBoxItem value="LA" Text="LA" />
        <telerik:RadComboBoxItem value="MA" Text="MA" />
        <telerik:RadComboBoxItem value="MD" Text="MD" />
        <telerik:RadComboBoxItem value="ME" Text="ME" />
        <telerik:RadComboBoxItem value="MI" Text="MI" />
        <telerik:RadComboBoxItem value="MN" Text="MN" />
        <telerik:RadComboBoxItem value="MO" Text="MO" />
        <telerik:RadComboBoxItem value="MS" Text="MS" />
        <telerik:RadComboBoxItem value="MT" Text="MT" />
        <telerik:RadComboBoxItem value="NC" Text="NC" />
        <telerik:RadComboBoxItem value="ND" Text="ND" />
        <telerik:RadComboBoxItem value="NE" Text="NE" />
        <telerik:RadComboBoxItem value="NH" Text="NH" />
        <telerik:RadComboBoxItem value="NJ" Text="NJ" />
        <telerik:RadComboBoxItem value="NM" Text="NM" />
        <telerik:RadComboBoxItem value="NV" Text="NV" />
        <telerik:RadComboBoxItem value="NY" Text="NY" />
        <telerik:RadComboBoxItem value="OH" Text="OH" />
        <telerik:RadComboBoxItem value="OK" Text="OK" />
        <telerik:RadComboBoxItem value="OR" Text="OR" />
        <telerik:RadComboBoxItem value="PA" Text="PA" />
        <telerik:RadComboBoxItem value="RI" Text="RI" />
        <telerik:RadComboBoxItem value="SC" Text="SC" />
        <telerik:RadComboBoxItem value="SD" Text="SD" />
        <telerik:RadComboBoxItem value="TN" Text="TN" />
        <telerik:RadComboBoxItem value="TX" Text="TX" />
        <telerik:RadComboBoxItem value="UT" Text="UT" />
        <telerik:RadComboBoxItem value="VA" Text="VA" />
        <telerik:RadComboBoxItem value="VT" Text="VT" />
        <telerik:RadComboBoxItem value="WA" Text="WA" />
        <telerik:RadComboBoxItem value="WI" Text="WI" />
        <telerik:RadComboBoxItem value="WV" Text="WV" />
        <telerik:RadComboBoxItem value="WY" Text="WY" />
    </Items>
</telerik:RadComboBox>