Categories
Geeky/Programming

Enum or Lookup Table (Or Both?)

Well, here is a dilemma. Should you use an Enum in code or a Lookup Table in the DB for static lookup data. Or should you use both and make sure they match up? And if you do, should you make the tables have an identity INT column?

Depends on the situation.

If you have a lookup table for something that is static (or relatively static – changes maybe once every year or more), then an Enum to Lookup table relationship might be what you need. But, if your list is more than say 10-15 items, I don’t know if it would be best to even have an enum. If you have a lookup table that will get added to a lot (more than once a year) then I don’t think you want to add an enum as you would have to change it and recompile your code on every addition. With the case of frequently changing data or a lot of items in your list, a cached dataset would probably work the best. I think in practice you should lay out ground rules before the start of a project on what business rules have data assumptions and where they will be (data layer, business layer, presentation layer) – that way, there will be no confusion on lookup/seed/static data throughout your project.

If .NET had some way to make sure that your enum and lookup table matched or if you could create the enum from a data table, it would be great. You can dynamically create enums using reflection, but I don’t think that would be the best solution in this scenario.

Here are some other blog posts on the subject

Enums and Lookup Tables

Data Modeling and Enums

Categories
Geeky/Programming

How to Grab RSS feed using XMLDataSource in the Personal Web Site Starter Kit

So, I like I said earlier, I have been playing with the Personal Web Site Starter Kit (PWSK) in Visual Studio 2005 Beta 2. I read these two articles:

· Introduction to the Personal Web Site Starter Kit

· Extending the Personal Web Site Starter Kit

I wanted to see if I could get my blog feed on the main site under the “What’s New Lately� section on the main page (default.aspx). Well first, I took the atom.xml file from my blog, and got the URL. In the process of doing this exercise, I noticed I couldn’t get the Atom feed to work, so I found a site (FeedJumbler) to convert the Atom feed to straight RSS feed, which works.

Then I dragged the XmlDataSource item from the toolbox to the page under the ObjectDataSource that is there by default. If you set the datasource to the URL and the XPath to “rss/channel/item�

In the HTML code for default.aspx, if you add this code

<h4>My Blog Latest Posts</h4>

<asp:DataList ID=”DataList2″ runat=”server”

DataSourceID=”XmlDataSource1″>

<ItemTemplate>

<p>

<h5><%#XPath(“title”)%></h5>

<p>

<%#XPath(“description”)%>

</p>

Posted by <%#XPath(“author”)%> @ <%#XPath(“pubDate”)%>

<a href=”<%#XPath(“link”)%>“><i>Link</i></a>

<hr/>

</ItemTemplate>

</asp:DataList>

Then you should see your blog posts on your PWSK front page!

Categories
Geeky/Programming

How To Make Your Own Syslog Sever in VB.NET

In networks all over, many devices can send Syslogs to a syslog server. You can download Syslog Servers (like Kiwi) to capture and process the syslogs, or you can create your own server to catch all the syslogs on your network. Then you can parse them to a database and write your own reports of them, having full control of everything.

First, in VB.Net, you need to import some namespaces.

Imports System.IO
Imports System.Net.Sockets
Imports System.Net
Imports System.Text

Then, from you Main procedure, call a procedure called ListenForSyslogs

Private Sub ListenForSyslogs()

Dim ipeRemoteIpEndPoint As New IPEndPoint(IPAddress.Any, 0)
Dim udpcUDPClient As New UdpClient(514)
Dim sDataRecieve As String
Dim bBytesRecieved() As Byte
Dim sFromIP As String

Try
While True
bBytesRecieved = udpcUDPClient.Receive(ipeRemoteIpEndPoint)
sDataRecieve = Encoding.ASCII.GetString(bBytesRecieved)
sFromIP = ipeRemoteIpEndPoint.Address.ToString

FillLog(sDataRecieve, sFromIP)

Console.WriteLine(sDataRecieve)
sDataRecieve = ""
End While
Catch e As Exception
' just ignore for now
End Try
End Sub

If you analyze this code, it just sets up a endpoint on the IP you are running the program, and listens on port 514, the default syslog port. It will just run and run, and keep listening. Whenever you recieve data, then call FillLog procedure

Private Sub FillLog(ByVal sSyslog As String, ByVal sFromIp As String)

Dim sPriority As String
Dim sPath As String = System.Environment.CurrentDirectory & "Unprocessed"

sSyslog = sSyslog.Replace(vbCrLf, "")
sSyslog = Mid(sSyslog, InStr(sSyslog, ">") + 1, Len(sSyslog))
sSyslog = Trim(sSyslog)

sPriority = GetSyslogPriority(sSyslog)

Dim swWriter As New StreamWriter(sPath & "syslog" & Now.Month & Now.Day & Now.Year & Now.Minute & ".txt", True)
swWriter.WriteLine(sFromIp & "," & Now & "," & sPriority & "," & sSyslog)
swWriter.Flush()
swWriter.Close()

End Sub

What FillLog does it look at data recieved, and parses it out, removing line feeds, etc.

Then it gets the priority from a function GetSyslogPriority(). Then it writes out the info to a comma seperated txt file (for easy parsing later), that is named pathsyslogmonthdayyearminute.txt so for example, c:unprocessedsyslog0408200529.txt

It will append to that txt for for the minute it gets syslogs for. So you should probably have another process that will consume that txt file before the next hour rolls around.

Finally, the function that gets the priority:

Private Function GetSyslogPriority(ByVal sSyslog As String) As String
Dim sResult As String

If InStr(sSyslog, "-0-") Then
sResult = "Emergency (0)"
End If

If InStr(sSyslog, "-1-") Then
sResult = "Alert (1)"
End If

If InStr(sSyslog, "-2-") Then
sResult = "Critical (2)"
End If

If InStr(sSyslog, "-3-") Then
sResult = "Error (3)"
End If

If InStr(sSyslog, "-4-") Then
sResult = "Warning (4)"
End If

If InStr(sSyslog, "-5-") Then
sResult = "Notice (5)"
End If

If InStr(sSyslog, "-6-") Then
sResult = "Info (6)"
End If

If InStr(sSyslog, "-7-") Then
sResult = "Debug (7)"
End If

If sResult = "" Then
sResult = "UNKNOWN"
End If
Return sResult

End Function

To summarize, you can capture syslogs from your network to text files, and then create another program to read in the text files to a database and write reports. Creating the UDP listener on port 514, you can setup your network devices to dump syslogs to your box where you are running the syslog server you created. To troubleshoot network issues, syslogs will give you a good idea of what is getting denied, etc, and you can create your own homegrown Syslog Server using VB.NET in a few simple steps.

Enjoy!

Categories
Geeky/Programming

.NET – Changing Screen Resolution

Last night I was working a project, and a feature was requested to have the program change the screen resolution to 1024×768, and then back to what the user had before they started the program. I figured, hey, .NET, it should be easy, right? First rule of computers…nothing can every be easy.

First order of business. Get the screen size of the screen before changing it, so I know what to change it back to.


&nbsp iOrigialScreenWidth = Screen.PrimaryScreen.Bounds.Width()
&nbsp iOrigialScreenHeight = Screen.PrimaryScreen.Bounds.Height()

Well you think you would be able to set those values as well as get them? Come on, that would be to easy.

The only way I could figure out how to change it was to revisit the good ol’ API. I found a class that someone has wrapped around the API calls to change the resolution, so I referenced that in my project, and was ready to go. At the top of the code “Imports Resolution” – Resolution was the name of the class.


&nbsp ‘ change to 1024×768 so the program fills screen
&nbsp Dim ChangeRes As CResolution
&nbsp ChangeRes = New Resolution.CResolution(1024, 768)

and to change it back again, on close of the program:

&nbsp ‘ change to original resolution
&nbsp Dim ChangeRes As CResolution
&nbsp ChangeRes = New Resolution.CResolution(iOrigialScreenWidth , iOrigialScreenHeight)

Well, anyway, here is the API Class (in C#). You would need to reference the Assembly in your project.

Source Code

So, the moral of the story, instead of using 6 lines of code to change the resolution back and forth, it takes 6 lines and referencing the API. Fun!

Categories
Geeky/Programming

SQL Exception Handling in .NET

In .NET , Microsoft introduced the Try, Catch, Finally exception handling. In all books and examples I have seen, you catch an exception like so:

Try
…some code
Catch ex as Exception
…some error handling code
End Try

Well, what happens when you are catching a SQL Exception?

Try
…some code
Catch sEx as SqlException
…some error handling code
End Try

Is Microsoft trying to tell us something here??

Categories
Geeky/Programming

Microsoft Office and .NET

I know Microsoft is trying to make everything .NET, and they have there is a Visual Studio .NET for Microsoft Office thing, but I mean, it is a PITA if you want to try to make managed code work with MS Office. I would like to make some add-in’s for Outlook, but I want it to be totally managed code, and you can’t do that because you have to add in the COM objects for outlook. I have a book that I have been reading for my own edification titled “Programming Microsoft Outlook and Microsoft Exchange 2003“, by Thomas Rizzo, and it is a good read, but I just wish I could use all .NET assemblies and stuff instead of the the COM Objects. Maybe in the next version of Office you will be able to use totally managed code to create add-ins. I know in Whidbey, they added in the WebBrowser control, a managed code version of the Browser Control in VB 6.0.

Visual Studio .NET and Office Development

Categories
Geeky/Programming

Cisco Netflow

Wow..Cisco Netflow. What a pain in the a$$. Anyways, I think I am going
to have the only .NET Netflow Capture and Parsing program in the
world. Hopefully. It really is tough to parse the packets and decode
the correctly, but I am on the right track. Ethereal helped me a bit in
that department. Ethereal has a built in netflow decoder (CFLOW) which allowed me to view the packet part by part, to they byte level and then see how the packets are set up.

Cisco Netflow

Ethereal

Categories
Geeky/Programming

MSDN Event : Application Code Blocks

Application Code Blocks are little “parts” of programs that Microsoft has created that you can install, and then include the assemblies in your program. The code has been tested and was created with best practices in mind.

We went over:
1) Configuration Management Block : Allows you to set up app.config files easily and encrypt and sign them. This is really cool so you can have settings in a file for your program and keep them secure, while not having tons of code to manage

2) Application Updater Block : Have you ever ran an application that popped up with a box that said there was a new version and that you could click yes to update it? This code block makes this about 100 times easier. You can include the code block, and set up a manifest on a web server, and your program will auto update!

Microsoft Application Code Blocks