Wassup Jose Weblog nonsense at its best!

26Jul/090

WTF Code Candidate

I just stumbled on another WTF code candidate, so I have to post it. Developers, grab some tissue, because it's going to bring tears to your eyes. Yup, it's crappy code but I have seen worse (unbelievable, but true). Btw, the WordPress code tag still sucks!

  • Hard coded connection string [check].
  • Embedded SQL (with *, no less) [check].
  • Useless Try/Catch block [check].
  • Redundant counters and iterators [check].
  • Inconsistent naming scheme [check].
  • Spelling errors [check].
  • Logic errors [check].

Imports System.Data
Imports System.Data.SqlClient
Imports System.Data.OleDb

Partial Public Class _Default
Inherits System.Web.UI.Page

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Dim sqlConnString As String = "Initial Catalog=pubs:Integrated Sceurity=SSPI"

If Not Page.IsPostBack Then
Dim conn As New SqlConnection(sqlConnString)
Dim sql As String = "SELECT * FROM Authors"
Dim SQLcmd As New SqlCommand(sql, conn)
Dim dr As SqlDataReader

Try
conn.Open()
Catch ex As Exception
Response.Write(ex.Message)
End Try

' Read data into the DataReader
dr = SQLcmd.ExecuteReader(CommandBehavior.CloseConnection)

' Show all the records in the ListBox
Do While dr.Read
Dim Total As Integer = dr.FieldCount - 1

' Get the entire set of data at once.
Dim AllData(Total) As Object
dr.GetValues(AllData)

Dim i As Integer
Dim n As String

For i = 0 To Total ' Show one reconrd
n = dr.GetName(i)
ListBox1.Items.Add(n & ": " & AllData(i).ToString)
Next i

ListBox1.Items.Add("______________") ' line to divide records
Loop ' More to next record
End If
End Sub
End Class

Comments (0) Trackbacks (0)

No comments yet.


Leave a comment

(required)

 

No trackbacks yet.