RSS

Tag Archives: VB

A workaround to use SQL IN clause in TableAdapters

This article is a simple implementation on how to use parameterized SQL query with IN clause or unknown number of parameters in TableAdapter.

When modifying those ancient database access codes, SQL query strings appear in the form of concatenated string very often. These codes are hard to read, maintain and prone to SQL Injection. Using DataSet Designer can easily improve it by accessing database with parameterized commands. But DataSet Designer does not support automatically generating commands with parameterized IN clause.

The idea of this article is originated from Extending TableAdapters for Dynamic SQL on The Code Project. The article demonstrate a way to change the command text programmatically by extending the TableAdapter which is defined as a partial class from the DataSet Designer. What’s not covered is how to parameterized the command.
Read the rest of this entry »

Advertisements
 
Leave a comment

Posted by on May 22, 2012 in .NET, ADO.NET, Oracle, SQL, VB

 

Tags: , , , , , ,

Not to select the first row in DataGridView by default

DataGridView makes the first row selected after DataBinding by default. I tried using DataGridView.ClearSelection() in the Form.Load event to unselect it with no avail. The problem is that data hasn’t been bond to the DataGridView in the Form.Load event. The method should be placed in the DataBindingComplete event of the DataGridView:

Private Sub DataGridView1_DataBindingComplete(ByVal sender As System.Object, _
    ByVal e As System.Windows.Forms.DataGridViewBindingCompleteEventArgs) _
    Handles DataGridView1.DataBindingComplete

    Dim gridView As DataGridView
    gridView = CType(sender, DataGridView)
    gridView.ClearSelection()
End Sub

Reference: C# DataGridView: First row is being selected by default

 
11 Comments

Posted by on May 14, 2012 in .NET, VB, Winform

 

Tags:

Insert delimiters between each character of a string

For example, to make the string “abcde” becomes “a,b,c,d,e” or “a b c d e”. What I’m going to do is to convert the string to a char array and then use String.Join to join the chars with the specific delimiters. There are different ways to accomplish it base on different versions of .NET Framework.

.NET 2.0
First using String.ToCharArray to convert the original string into a char array. Since String.Join takes a string array as the 2nd parameter, the char array needs to be converted into a string array. In .NET 2.0 we could use Array.ConverAll:

Read the rest of this entry »

 
Leave a comment

Posted by on May 9, 2012 in .NET, Lambda, LINQ, VB

 

Tags:

Read from text file with multiple line formats using TextFieldParser

When using TextFieldParser to read fixed field width text file with optional fields, MalformedLineExceptions could happen. It happens because the imported line length is shorter than expected.

For example:

reader.SetFieldWidths(5, 8, -1)

' Exception will be thrown when the line is only 12 characters long or less
' because the 3rd field will be regarded as missing

We could prevent the exception by determining which format to use according to the line length.
Solution:

Read the rest of this entry »

 
Leave a comment

Posted by on May 4, 2012 in .NET, VB

 

Tags:

Get the first/last day of the month with VB.NET

Getting the first and last day of the month is pretty simple with .NET DateTime object constructor. The functions below are rewritten from Getting the First and Last day of the month with .NET C# in VB.NET.

'Get the first day of the month
Public Function FirstDayOfMonth(ByVal sourceDate As DateTime) As DateTime    
    Return New DateTime(sourceDate.Year, sourceDate.Month, 1)
End Function

'Get the last day of the month
Public Function LastDayOfMonth(ByVal sourceDate As DateTime) As DateTime
    Dim lastDay As DateTime = New DateTime(sourceDate.Year, sourceDate.Month, 1)
    Return lastDay.AddMonths(1).AddDays(-1)
End Function
 
Leave a comment

Posted by on May 3, 2012 in .NET, VB

 

Tags:

How to validate Windows Form user input

Here are a couple notes taken from the MSDN User Input Validation in Windows Forms article.

Where to place the validation logic :
Validating event. If the Control.CausesValidation property is set to false, the Validating and Validated events of that control will not fire.

When does the validating event execute :
1. By Form.AutoValidate property. It specifies whether the control is validated on focus change by the AutoValidate enumerated value. (Implicit validation)
2. By calling Validate or ValidateChildren. (Explicit validation)

How to close the form when validation failed using AutoValidate :
There’s no problem closing the form if AutoValidate is set to Disable or EnableAllowFocusChange. If not, set e.Cancel to false in the Form.Closing event.

Read the rest of this entry »

 
Leave a comment

Posted by on April 5, 2012 in .NET, VB, Winform

 

Tags:

Custom Dialog Box example : ChooseDateDialog

Here’s a complete example on how to show dialog box and retrieve user input. More details on Dialog Box from Dialog Boxes in Windows Forms. The ChooseDateDialog is a simple custom dialog box which is used to prompt for a date. Its functionality is like this:

First, use a simple button to call ChooseDateDialog:

Read the rest of this entry »

 
Leave a comment

Posted by on April 5, 2012 in .NET, VB, Winform

 

Tags: