John Stagich's Blog

Microsoft .Net Developer

December 2008 Quick Hits

clock December 26, 2008 05:06 by author johnstagich

The key here is implementing the IComparable interface when creating your class (see link above for more information).  The IComparable interface has the CompareTo method that does the sorting. Once the interface is in place, you can use the sort method in the generic list: to sort your list (GenericList.Sort()).  Here is some sample code that implements the IComparable interface to an Ingredients Generic list.  To sort this list by IngredientID, instantiate the class (Dim objIngredientList as List(Of BEIngredientList = New List(Of BEIngredientList)}, popluate the list, and then excute the sort method objIngredientList.Sort().

  Public Class BEIngredientList 
   Implements IComparable(Of BEIngredientList)
#Region "Locals"
    Private _ingredientsID As Integer
    Private _ingredientName As String
    Private _ingredientCategoryID As Integer
#End Region
    Public Property IngredientID() As Integer
        Get
            Return _ingredientsID
        End Get
        Set(ByVal value As Integer)
            _ingredientsID = value
        End Set
    End Property
    Public Property IngredientName() As String
        Get
            Return _ingredientName
        End Get
        Set(ByVal value As String)
            _ingredientName = value
        End Set
    End Property
    Public Property IngredientCategoryID() As Integer
        Get
            Return _ingredientCategoryID
        End Get
        Set(ByVal value As Integer)
            _ingredientCategoryID = value
        End Set
    End Property
    Public Function CompareTo(ByVal other As BEIngredientList) As Integer Implements System.IComparable(Of BEIngredientList).CompareTo
        Return Me.IngredientID.CompareTo(other.IngredientID)
    End Function
End Class

 

  • Computed Columns with Microsoft SQL 2005

    Select the table to which you want to add a computed column. From the Design screen, select the column name (in the example below we use a column called PreparedDryWeight).  Next, in the Column Properties screen expand the Computed Column Specification.  Enter the formula  into the (Formula) field as shown in the example below.
     
    (case when [PreparedMoisturePercentage]>(0) then [PreparedNetWeight]-[PreparedNetWeight]*([PreparedMoisturePercentage]*(0.01)) else (0) end)

    Note: You cannot use another computed column in the calculation.  Check ou this link The Power of SQL Case Statements by Scott Mitchell

  • Refining a value from a Date data type field using the Value property .

    obj.DryingDate.Value.Date   -Get the Date as Date data type
    obj.DryingDate.Value.Year   -Get the Year as Integer data type

  • Return a DataSet object type with Enterprise Library.  The key is the database.ExecuteDataSet(dbCommand) method.  See sample code below.

    Public Function GetDistinctBatchProductDesc(ByVal statusID As Integer, _
                                                    ByVal receivingLocationID As Integer) As DataSet
            Dim ret As New DataSet
            Dim db As Database = DatabaseFactory.CreateDatabase("SQLDataAccess")
            Dim sqlCommand As String = "Inventory.GetDistinctBatchProductDesc"
            Dim dbCommand As Common.DbCommand = db.GetStoredProcCommand(sqlCommand)
            db.AddInParameter(dbCommand, "StatusID", DbType.Int32, statusID)
            db.AddInParameter(dbCommand, "ReceivingLocationID", DbType.Int32, receivingLocationID)
            ret = db.ExecuteDataSet(dbCommand)
            Return ret
        End Function

  • WPF Videos

  • Preventing Users from Leaving Invalid Controls in Windows Forms.

    In a Validating event handler, you can prevent users from leaving the control containing the invalid entry until they clear or fix the error. If you look back to the code that assigned the event handler to the control, you will note that it wrapped the method in a CancelEventHandler. That provides the flexibility to add this line in the handling routine.

    e.Cancel = true; 

    ' Sample code that checks to make sure a textbox control contains text.

    Private Sub txtTypeCode_Validating(ByVal sender As System.Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles txtTypeCode.Validating
            If Not Regex.IsMatch(txtTypeCode.Text, "\w") Then
     MessageBox.Show("Type Code CANNOT be blank", "Input Error", MessageBoxButtons.OK,           MessageBoxIcon.Error)
                ' Prevents users from leaving textbox until they fix it!
                e.Cancel = True
            Else
                txtTypeDesc.Focus()
            End If
    End Sub
     

    That line of code suppresses any events that would normally follow the Validating event.  To users, the code causes the cursor to remain stubbornly in the control; neither tabbing nor mousing will get it to budge until the user corrects the error.  For more information on the Validating event, see this MSDN article.

  • How to make input parameters optional in T-SQL. Place an "=" sign after the datatype, and then assign a DEFAULT value.

    Create Procedure OptionalParametersDemo
       @floatTest      float=Null,
       @varcharTest varchar(10)=Null,
       @dateTest     datetime=Null,
       @intTest        int=Null
    As ...

  • How to toggle a boolean field in VB.Net:  blnFlag = Not blnFlag

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5


Enterpirse Library Cryptography Error

clock November 22, 2008 02:39 by author johnstagich

I was using the Cryptography portion Enterprise Library 3.1 for the encryption and decryption of data in my Windows application.  Part of the configuration process creates a key file (*.key) necessary for the encryption/decryption to work.

 It was all working fine until I tried to publish and deploy the application. The cryptography piece would not work when the application was deployed to another machine. Here is why. The algorithm that builds the key file uses local machine information to build the key.  When the key file is placed on another machine, the machine information is different; consequently the cryptography fails with the following error message: "Key not valid for use in specified state. \r\n" Source="System.Security" 

 How do you fix this problem?

 

1)      Create a password protected text version of the key file (for example, AppKey.txt). Use the Enterprise Library Application Configuration tool to create a text version of the key file (for example, AppKey.txt).

2)      When deploying your application, make sure you deploy the text version of your key file (AppKey.txt) and not the key file (AppKey.key).

3)      In your application, at start-up, add the following code I found on codeplex (see DevLingo entry July 17, 2007).  The code reads the text file (AppKey.txt) and recreates the key file (AppKey.key).  It then updates the app.config file, to point to the location of the new AppKey.key file.

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5


Search

Calendar

<<  March 2010  >>
SuMoTuWeThFrSa
28123456
78910111213
14151617181920
21222324252627
28293031123
45678910

Archive

Tags

Categories


Blogroll

Disclaimer

The opinions expressed herein are my own personal opinions and do not represent my employer's view in anyway.

© Copyright 2010

Sign in