Converting a sentence into CamelCase format in Microsoft VB.NET & ASP.NET


Microsoft VB.NET, ASP.NET and C#.NET lacks the function to allow programmers to convert a string, or even a whole sentence into Camel Case format. Therefore, i have written a microsoft .net functions to do just that. This comes very handy especially in converting a list of names into Camel Case format, or converting a string of addresses and many other applications. You name it. If you do not know what a Camel Case format look like, see the example below:

For instance, we are going to convert the following files into Camel Case format.

Dim Sentence1 As String = "is john smith working in microsoft?"
Dim Sentence2 As String = "michael t. ashby is a GREAT programmer. he is young."
Dim Output1, Output2 As String

Output1 = CCase(Sentence1)
Output2 = CCase(Sentence2)

The converted output will be
Output1 : "Is John Smith Working In Microsoft?"
Output2 : "Michael T. Ashby Is A Great Programmer. He Is Young."



Simply copy and paste the following codes below into your microsoft vb.net, asp.net programming code and you can use it right away.


Public Function CCase(ByVal iString As String) As String
'This function converts a string into "Camel Case" format

Dim oString As String = "" 'initialise output string
Dim iLength As Integer = Len(iString) 'the length of input string
Dim P As Integer = 1 'set pointer to start at character 1
Dim iChar As String 'individual stripped character
Dim UpperFlag As Boolean 'flag to mark upper case is required or not

iString = LCase(iString) 'convert to lower case string
While P <= iLength ' while not end of the string (pointer)

iChar = Mid(iString, P, 1) 'strip each character out of the string one by one
If P = 1 Then 'first character

UpperFlag = True 'change first character to upper case
End If
If iChar = " " Or iChar = "-" Then 'blank field encountered.

UpperFlag = True 'raise the flag to mark the next character as upper case
Else 'non-blank field encountered
If UpperFlag = True Then 'if the flag is raised
iChar = UCase$(iChar) 'change the character to uppercase
UpperFlag = False 'undo raising the flag
End If
End If
oString &= iChar 'reassemble characters back into a string as output

P += 1 'next pointer
End While

Return (oString) 'return the reassembled output string
End Function

Applying Absolute Value In VB.NET with ABS



VB.NET allows the conversion of figures (both positive and negative) into absolute figures easily. You can use the Mathematical Function as shown below

Math.ABS


For example, to change a figure value of -34167 into absolute figures, we can apply the codes as follows :


Dim myValue as Integer

myValue = Math.ABS(-34167)

MsgBox(myValue) 'The output now becomes 34167



That illustrates the use of the in-built Math.Abs function in VB.NET

VB.NET : Sleep

Posted by Fluffy | 8:53 AM | , | 0 comments »

Putting application to sleep for a specified miliseconds in VB.NET


In VB.NET, you can make an application idle ( or sleep ) for a specified miliseconds. For example, in your programming code, you might want to let your program to be idle for 500 miliseconds after executing a certain functions and then execute the next function.
Apply the code below between functions:


System.Threading.Thread.Sleep(500)


Note : The number used is in miliseconds
* 1 sec = 1000ms

Accessing SQL Server Compact Edition Database using VB.NET


SQL Server Compact Edition, or simply SQL Server CE, allows a very convenient way of fetching data stored locally as cache. It is usually used in situations where commonly used data are stored locally and accessed directly instead of making queries to fetch the data again and again from the main database server. Accessing data stored locally can be many times faster compared to accessing data stored in the main database located somewhere on the network. Hence, by implementing and taking the full advantage of SQL Server CE, you can make your application runs faster.


Firstly, add the following reference at the top of your module programming code


Imports System.Data.SqlServerCe


Next, add the following as global variables so that you can call them anytime, anywhere in your code.


Friend SQLcommCE As SqlCeCommand 'SQL command
Friend daCE As SqlCeDataAdapter 'SQL adapter
Friend SQLConnCETempDB As SqlCeConnection 'SQL connection linking your local database


Then, add the following codes to establish the database connection to where your application loads when it runs. Data Source points to the path where your SQL Server CE database extension *.sdf is stored, and provides the password (if any) in the codes shown below.


SQLConnCETempDB = New SqlCeConnection("Data Source=C:\TempDB.sdf;Password='123'")
SQLConnCETempDB.Open()




Finally, in the programming code where you want to perform query to the database, you can simply use the codes shown as follows.


Dim SQLStm as String

SQLStm = "DELETE FROM LocalTable WHERE MYTABLE_ID = '123'"
SQLcommCE = New SqlCeCommand(SQLStm, SQLConnCETempDB) 'execute the query
SQLcommCE.ExecuteNonQuery()


That will delete the records with MYTABLE_ID = '123' in your LocalTable.
For INSERT INTO, UPDATE, DELETE or any other NonQuery database access, you can use

.ExecuteNonQuery

If you intend to query the table using the SELECT statement, you have to use the following code instead

.CommandType = CommandType.Text


See the example below:


Dim SQLStm as String
Dim ds as DataSet

Dim Rank, Name as String

SQLStm = "SELECT Rank, Name FROM LocalTable WHERE MYTABLE_ID = '123'"
SQLcommCE = New SqlCeCommand(SQLStm, SQLConnCETempDB)
'SQL query
SQLcommCE.CommandType = CommandType.Text

daCE = New SqlCeDataAdapter(SQLcommCE) 'Retrieve data from SQL Server CE
ds = New DataSet
daCE.Fill(ds) 'Populate dataset with retrieved data.


If ds.Tables(0).Rows.Count > 0 Then ' If record exists in LocalTable
Rank = ds.Tables(0).Rows(0).Item("Rank").ToString
Name = ds.Tables(0).Rows(0).Item("Name").ToString
Else 'record not exists in LocalTable
Rank = ""
Name = ""
End If


Now you can start implementing SQL Server CE in your applications