Archive

Archive for the ‘VB .Net’ Category

Right Click Menu

October 12th, 2009 No comments

Each component in Syteline form can have its own right click menu, and this right

Syteline screen shoot

click menu is customizable.

See the sample, the “Ship To” field has a standard right click menu, with “Add”, “Detail”, “Find” and “Help” menu items.

If you look into the component property of “Ship To” field, you can see the “Right Click Menu” property is blank.   But this property is inheriting from the component class: CustSeq.  In there, the right click menu defined to be StdDetailsAddFind.

There are list of the pre-exist right click menu you can use, such as “StdAddFind”, “StdHelp” and such.

You can also modify or create a new right click menu by go into the “Shortcut Menu Properties” dialog box.

image

Over there, you just need to define the “Menu Item/Caption” and the “Event to Generate” for each of this menu item.

image

Working with null values in the .NET Framework

June 17th, 2009 1 comment

One of the trickier aspects of application development is dealing with null or nonexistent data. It gets complicated when the application expects a data value and gets nothing or unexpected values; that’s when you must perform coding that properly handles these situations to avoid application failure. This requires a close examination of the data wherever and whenever it’s needed. Let’s take a closer look at working with null values within the .NET Framework.

Null and void

A null value is a value that doesn’t refer to any object. It’s the default value of reference-type variables (e.g., class, interface, delegate).

These items are called reference types because they’re objects that store references to the actual data. String, object, and struct are reference type examples. The null keyword is a literal that represents a null reference with the C# .NET environment. You may use the null keyword to check or assign the value of an object. For example, the following C# code determines if a string value is null:

string sTest = “Test”;
if (sTest == null) {
Console.WriteLine(“sTest is Null”)
}

This code works without any problems with C#, but there’s no VB.NET equivalent of the null keyword. Instead, VB.NET uses the Nothing keyword. The following code demonstrates its use:

Dim sTest As String
If (sTest Is Nothing) Then
Console.WriteLine(“sTest is Null”)
End If

Another area of confusion stems from the fact that VB.NET doesn’t treat null and Nothing as equals; consequently, a VB.NET programmer may have to check for both values.

The variation in support may cause confusion, but a developer rarely develops in both languages simultaneously. On the other hand, Microsoft provides a uniform method for working with null values: The base System.Convert namespace includes the DBNull object.

DBNull

The use of the DBNull class indicates the absence of a known value. While the Microsoft documentation says it’s typically in a database application, you may also use it with any type of data.

In database applications, a null object is a valid value for a field. This class differentiates between a null value (a null object) and an uninitialized value (the DBNull.Value instance). For example, a table can have records with uninitialized fields. By default, these uninitialized fields have the DBNull value.

You can utilize the DBNull object by way of its Value property. This represents a null value, which is never equal to anything. The following C# snippet revises the previous example to utilize the DBNull class:

if (sTest.Equals(System.DBNull.Value)) {
Console.WriteLine(“sTest is null.”);

Notice how this code utilizes the string class’s equality method. The VB.NET sample may utilize the same code, like this:

If (sTest.Equals(System.DBNull.Value) Then
Console.WriteLine(“sTest is Null”)
End  If

You may also use the DBNull.Value constant on the right-hand of an assignment operation, thus storing a null value within a reference type. In addition, the .NET Framework provides the IsDBNull function (in the System.Convert namespace), which is a shorthand approach for determining if a reference type contains a null value. The following VB.NET uses IsDBNull to check an object’s value:

Dim oTest As Object = DBNull.Value
If ((IsDBNull(oTest))) Then
Console.WriteLine(“oTest is Null”)
End If

Even if you check for null values at every conceivable location within your code, they still may creep into the application. Fortunately, the try/catch exception block provides another line of defense; it allows you to catch any unhandled exceptions that null values cause. The System namespace provides the NullReferenceException for these situations. See how the following C# code uses NullReferenceException.

try {
if (sTest.Equals(System.DBNull.Value)) {
Console.WriteLine(“sTest is null.”);
}

} catch (System.NullReferenceException e) {
Console.WriteLine(“Error: ” + e.ToString());
}

Handle your data with care

Applications don’t always receive the type of data they expect; for instance, an app may be looking for integer values and receive string or null values. Therefore, since data is the backbone of an application, you must be extremely careful when working with it.

Categories: Development, SQL, VB .Net Tags: ,

Convert a String to a Date / Time in VB

February 4th, 2009 No comments

Use CDate to convert a string to a date/time.

Dim d1 As Date
Dim d2 As Date
Dim d3 As Date

d1 = CDate("August 12, 2004")
d2 = CDate("2:07:30 PM")
d3 = CDate("August 12, 2004 2:07:30 PM")

Print d1  ' prints 8/12/2004
Print d2  ' prints 2:07:30 PM
Print d3  ' prints 8/12/2004 2:07:30 PM

Today’s Date

January 24th, 2009 No comments

It is very often that you need to get current date/time in your development.

T-SQL

To get today date without time as string variable use following script:

Select CONVERT( CHAR(8), GetDate(), 112)

To get datetime variable of today date without time use following script:

Select CAST( CONVERT( CHAR(8), GetDate(), 112) AS DATETIME)

VB

System.DateTime.Now.ToString(“yyyyMMddhhmmss”)

Now()

To initiate a date field as today’s date in Syteline form, need to use CURDATE().

How to block “PO Box” being entered into Customer Ship To Address

January 23rd, 2009 No comments

Problem:

Many business would not allow product shipped to a “PO Box” address.  So would like to block any “PO Box” address being entered into any ship to address.

Solution:

1)      Enter design mode of “Customer Ship Tos” Form.

2)      We are going to first create a script.  Go to “Menu -> Edit -> Script”, a Script window should open.  Click “New”, enter Script Name called “NoPOBox”, then click OK.

NoPOBox1

Put in the following script:

Option Explicit On

Option Strict On

Imports System

Imports Microsoft.VisualBasic

Imports Mongoose.IDO.Protocol

Imports Mongoose.Scripting

Namespace SyteLine.GlobalScripts

Public class NoPOBox

Inherits GlobalScript

Sub Main()

Dim strValue As String

Dim logicYN as boolean

Dim logicYN1 as boolean

Dim logicYN2 as boolean

Dim logicYN3 as boolean

Dim logicYN4 as boolean

strValue = GetParameter(0).ToUpper()

logicYN = strValue like “*PO BOX*”

logicYN1 = strValue like “*P.O BOX*”

logicYN2 = strValue like “*P.O.BOX*”

logicYN3 = strValue like “*PO. BOX*”

logicYN4 = strValue like “*P.O. BOX*”

If logicYN or logicYN1 or logicYN2 or logicYN3 or logicYN4

ReturnValue = “1”

else

ReturnValue = “0”

end if

End Sub

End Class

End Namespace

Then “OK” to save the Script, and “Done” to close the Script window.

3)      Create a Validator.

While we are still in the design mode of “Ship Tos” Form, from menu, go to “Edit -> Validator”.  A “Validator” window open, click “New”.  In the “Validator Properties” window, enter “NoPOBox” as name, “Run Script” as type.  Select “NoPOBox” as script name from the pull-down list, that is the script we just created in step 2).

NoPOBox2

Then, create Error Message: “PO Box Is Not Valid Address”.

NoPOBox3

Click OK all the way back, we now have created a validator called “NoPOBox”, and we can apply it to the fields that we want to validate.

4)      Apply validator

NoPOBox4

Click to enter PV(Addr_1), that means property value of Addr_1, as parameters.

NoPOBox5

5) Now save the change and exit out of design mode, we should be able to see the validation is working:

NoPOBox6