- Here is great link from John Sheehan's blog on formatting strings.
- How to get the absolute value: Math.Abs(CInt(Textbox1.Text))
- How to include Find Combo from Standard Toolbar in Visual Studio 2008
In my Visual Studio IDE, the Find Combo box disappeared from my Standard Toolbar. Here is how you get it back.
1) Right Click the Standard Toolbar
2) Select Customize ...
3) Drag and drop the "Edit | Go To Find Combo" from the "Commands" tab to the Standard Toolbar
- Problem with DataGridView automatically adding columns from DataSource during design time.
Fix: Clear the DataSource property for the DataGridView. Add the DataSource back if you want to add columns. Check out this link for some background information. Here is where I found out about the fix.
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)
Private _ingredientsID As Integer
Private _ingredientName As String
Private _ingredientCategoryID As Integer
Public Property IngredientID() As Integer
Set(ByVal value As Integer)
_ingredientsID = value
Public Property IngredientName() As String
Set(ByVal value As String)
_ingredientName = value
Public Property IngredientCategoryID() As Integer
Set(ByVal value As Integer)
_ingredientCategoryID = value
Public Function CompareTo(ByVal other As BEIngredientList) As Integer Implements System.IComparable(Of BEIngredientList).CompareTo
- 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)
- 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
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
- How to toggle a boolean field in VB.Net: blnFlag = Not blnFlag
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.
At last night's Memphis .Net users group meeting,
Mark Mydland from Microsoft talked about the up and coming enhancements
to the testing portion of the next version of Visual Studio Team
System. One of the new features that I liked was the ability to record a
Another topic discussed was PEX. It is a new product from Microsoft Research. From the web page:
Right from the code editor,
Pex finds interesting input-output values of your methods,
which you can save as a small test suite with high code coverage.
Pex performs a systematic analysis,
hunting for boundary conditions,
exceptions and assertion failures,
which you can debug right away.
- Enterprise Library 3.1: Enterpise Library Configuation Editor Problem
An application I was working on needed to run on with the Entiprise 3.1 Library. When I tried to use the Enterprise Library Configuration editor (EntlibConfig.exe) on my app.config file, I was getting errors that it could not locate the specific libraries/.dll’s. The problem was that my libraries in my code where strongly named (PublicKeyToken=b77a5c561934e089) while the Enterprise Library Configuration Editory was looking for libraries with the PublicKeyToken set to null (PubliKeyToken=null). The fix was to use the RIGHT Enterprise Library Configuration editor. There are two of editors. One resides in C:\EntLib3Src\App Blocks\bin and the other resided in C:\Program Files\Microsoft Enterprise Library 3.1 - May 2007\Bin. The former works with non-signed libraries while the latter works with signed libraries. From the Visual Studio Development environment, I changed app.config editor to point to the C:\Program Files\Microsoft Enterprise Library 3.1 - May 2007\Bin editor.
For more information, check out this link from Tom Hollander on Avoiding Configuration pitfalls with incompatible copies of Enterprise Library.
- How do you debug a Unit Test in Visual Studio 2008?
1) Set a breakpoint in your test, and then from the Visual Studio menu: Test-->Debug-->Tests in Current Context.
2) Set a breakpoint in your test. Go to the Test Results window (from the Visual Studio menu: Test-->Windows-->Test Results). From the toolbar in the Test Results window, click on Debug drop down icon and then select Debug Checked Tests.
3) Try entering System.Diagnostics.Debugger.Launch() and System.Diagnostics.Debugger.Break() into your Unit Test code and then run your Unit Test You then will be prompted with a Visual Studio Just-In-Time Debugger dialog box. Select the default selection. You should then break into your Unit Test code.
- Here is a good link to the How Do I Video Series for Visual Basic.
- For the datagrid view control, if you want the NewRowNeeded event to fire, the VirtualMode property for the datagrid view control needs to be set to true.
- From Scott Mitchell's blog, I found out about a product called ELMAH that may come in handy down the road. ELMAH (Error Logging Modules and Handlers) is an application-wide error logging facility that is completely pluggable. It can be dynamically added to a running ASP.NET web application, or even all ASP.NET web applications on a machine, without any need for re-compilation or re-deployment.
- Have you ever wanted to share a common file among project? Check out Sara Ford's tip number #365 that shows how to link a file. (I wonder how this works with Team Foundation Server Source Control?). Tip number #364 is good too. Visual Studio comes with an image library.
- FromScott Gu's blog, new ASP.Net Chart control.
- The table designer in Microsoft SQL 2005 I find is lacking because I cannot find a way to add properties to the columns displayed. It only displays Column Name, Data Type, and Allow Nulls. From Rich Strahl's Web log, comes a work around. Use the Database Diagram Designer. Right mouse click on the Database Diagrams folder and click New Database Diagram. Next add your table. Right mouse click on the table-->Table View-->Modify Custom...
Here are two more Team System/.Net/SQL/ resources.
Team System User Group - Virtual Edition
Screen Cast A Day