Posts Tagged ‘Visual Basic’

Error Logging in VB and C#

Thursday, December 17th, 2009

Alright, so lately I have been working on a couple of ASP.NET(C#) projects.  One thing I have seen is nothing is as useful as having a good error logger.  Now, since I am still in the learning phases of .NET programming, I have been looking to a coworker, Joe Young, sort of as a mentor.  He provided me with some code that has just immensely helped me.  I have attached 2 files.  one in VB, on in C#, they are Joe’s code.  These are your App_Code files.  They will create a detailed log of any errors in a file in a directory you specify.  You specify the file in the attached files.  To use them, you will insert the following code. (For the below VB examples, I used a website to convert it from my C#, as I was being lazy)

C#:

private void LogError(Exception errMessage)
{
errorlogger objLog = new errorlogger();
objLog.logerror(errMessage);
}

VB:

Private Sub LogError(ByVal errMessage As Exception)
Dim objLog As New errorlogger()
objLog.logerror(errMessage)
End Sub

This allows you to call the external errorlogger.

Now, to call it. The most useful place I have put these are in SQL connections/procedure calls. Here is an example from one of my programs.

C#

try
{
string strSQLSetCheckedOut = “exec CP_setCheckedOutTrue ” + HttpContext.Current.Session["sName"].ToString();
SqlCommand conSQLCommand = new SqlCommand(strSQLSetCheckedOut, conSQLConnection);
conSQLCommand.CommandTimeout = 120;
conSQLConnection.Open();
conSQLCommand.ExecuteNonQuery();
}
catch (Exception ex)
{
LogError(ex);
}
finally
{
conSQLConnection.Close();
}

VB:

Try
Dim strSQLSetCheckedOut As String = “exec CP_setCheckedOutTrue ” & HttpContext.Current.Session(”sName”).ToString()
Dim conSQLCommand As New SqlCommand(strSQLSetCheckedOut, conSQLConnection)
conSQLCommand.CommandTimeout = 120
conSQLConnection.Open()
conSQLCommand.ExecuteNonQuery()
Catch ex As Exception
LogError(ex)
Finally
conSQLConnection.Close()
End Try

Hopefully this little bit of code out there will help someone else out.

C# ErrorLogger Code

VB ErrorLogger Code

Project Euler – Problems 1 & 2

Thursday, August 27th, 2009

So lately I got a little bored and stumbled upon Project Euler. It is basically a site with 250+ complex math/programming problems. It has been quite a bit of fun, even though I am only on the 3rd problem. I thought that i would post my VB(.NET 3.5) code that I used for the first 2 problems. If you don’t want to know the solutions, dont read further. Although, it is helpful to see the algorithm in other languages to make it work in your own code. For me, I think knowing what the final solution is would be best, so i can verify my algorithms worked before submitting. Anyway, below is problem 1


Public Class Form1

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim i, ii, Multiples(0) As Integer
Dim X, Y As Double
X = 0
i = 0

Do Until X = 1000
If X / 3 = CInt(X / 3) Or X / 5 = CInt(X / 5) Then
ReDim Preserve Multiples(i)
Multiples(i) = X
Me.ListBox1.Items.Add(X)
i = i + 1
End If
X = X + 1
Loop

Y = 0
For ii = 0 To UBound(Multiples)
X = Multiples(ii)
Y = Y + X
Next
Me.Label1.Text = CStr(Y)
If Me.Label1.Text = "233168" Then
Me.Button2.Visible = True
End If
End Sub

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Form2.WindowState = FormWindowState.Normal
Form2.Visible = True
Me.Visible = False
End Sub

Private Sub ExitToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ExitToolStripMenuItem.Click
Me.Close()
End Sub
End Class

And for Problem 2:


Public Class Form2

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim X, Y, Z, Numbers(1), i, ii, Evens(0), p As Long
Dim EvenString As String
Dim Q As Double
X = 1
Y = 2
Z = 0
i = 2
p = 0
Numbers(0) = X
Numbers(1) = Y
EvenString = ""

Do Until Z >= 4000000
Z = X + Y
X = Y
Y = Z
If Z >= 4000000 Then
Exit Do
End If
ReDim Preserve Numbers(i)
Numbers(i) = Z
i = i + 1
Loop
For ii = 0 To UBound(Numbers)
Me.ListBox1.Items.Add(CStr(Numbers(ii)))
Next
For ii = 0 To UBound(Numbers)
X = Numbers(ii)
Q = X / 2
If Q = CInt(Q) Then
ReDim Preserve Evens(p)
Evens(p) = Numbers(ii)
Me.ListBox2.Items.Add(CStr(Numbers(ii)))
p = p + 1
End If
Next
X = 0
Y = 0
For ii = 0 To UBound(Evens)
X = Evens(ii)
Y = Y + X
Next
Me.Label5.Text = CStr(Y)
If Me.Label5.Text = "4613732" Then
Me.Button2.Visible = True
End If
End Sub

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Form3.WindowState = FormWindowState.Normal
Form3.Visible = True
Me.Visible = False
End Sub

Private Sub Problem1ToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Problem1ToolStripMenuItem.Click
Form1.WindowState = FormWindowState.Normal
Me.Visible = False
Form1.Visible = True
End Sub

Private Sub ExitToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ExitToolStripMenuItem.Click
Me.Close()
Form1.Close()
End Sub
End Class

More to come… Enjoy!