Click here to Skip to main content
15,901,035 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a form based application where after selecting a SKU from drop down box, many different text boxes on the forms get populated by reading data from SQL. In my Sql datareader I have a condition to read different queries. The condition is a switch of 1 or 2 based on user triggers through the form. By default if the user just selects SKU from drop down box then Dim ShiftSnapShot as Integer = 1 gets applied. But if the user clicks on a button on the form after selecting a SKU from drop down box then ShiftSnapShot = 2.

Based on this I have the following Datareader subroutine

VB
Try

        Dim strSQL As String
        Dim connstring As String
        connstring = "Data Source=PLP01DB004R;Initial Catalog=MMS;Integrated Security=True"
        If ShiftSnapShot = 1 Then
            strSQL = "select top 1 CountPerPound from PostBlendGrading where CountPerPound is not null order by entrydatetime desc"
        Else
            strSQL = "select avg(CountPerPound) as CPP from PostBlendGrading where CountPerPound is not null and entrydatetime between DATEADD(Hour, -8, EntryDateTime) and EntryDateTime"
        End If
        Dim sqlconn As New SqlConnection
        Dim sqlcmd As New SqlCommand
        Dim sqldr As SqlDataReader

        sqlconn.ConnectionString = connstring
        sqlconn.Open()

        sqlcmd.Connection = sqlconn
        sqlcmd.CommandText = strSQL
        sqldr = sqlcmd.ExecuteReader
        Return sqldr
        sqlconn.Close()

    Catch ex As Exception

        Dim msg As String = "Error retrieving data from PostBlendGradingTbl"
        MsgBox(msg & vbLf & "The log file is written in " & vbLf & ex.Message & vbLf & ex.StackTrace)
        ErrorLog(msg, ex.StackTrace, "PostBlendGradingTbl Data Extraction - Error")

    End Try

End Function


This data reader is used to populate textbox with the help of following subroutine

VB
Public Sub PopulateDataFromPostBlendGradingTbl()
        Try
            Dim PostBlendGradingTblDataReader As SqlDataReader
            Dim CurrentDate As Date = Today.Date
            PostBlendGradingTblDataReader = GetDataFromPostBlendGradingTbl()

        Dim PostBlendGradingTbl_GravityDataReader As SqlDataReader
        PostBlendGradingTbl_GravityDataReader = GetDataFromPostBlendGradingTbl_Gravity()

        If PostBlendGradingTblDataReader Is Nothing Then
            MsgBox("PostBlendGradingTbl empty for the given Month = " & Str(CurrentDate.Month) & " Year = " & Str(CurrentDate.Year))
        Else
            While PostBlendGradingTblDataReader.Read
                If PostBlendGradingTblDataReader("CountPerPound").ToString <> "" And ShiftSnapShot = 1 Then
                    Me.txtCPP.Text = CType(PostBlendGradingTblDataReader("CountPerPound").ToString, Double)
                ElseIf PostBlendGradingTblDataReader("CPP").ToString <> "" And ShiftSnapShot = 2 Then
                    Me.txtCPP.Text = CType(PostBlendGradingTblDataReader("CPP").ToString, Double)
                Else
                    Me.txtCPP.Text = 0.0
                End If
            End While

        End If

        If PostBlendGradingTbl_GravityDataReader Is Nothing Then
            MsgBox("PostBlendGradingTbl empty for the given Month = " & Str(CurrentDate.Month) & " Year = " & Str(CurrentDate.Year))
        Else
            While PostBlendGradingTbl_GravityDataReader.Read
                If PostBlendGradingTbl_GravityDataReader("AverageSpecificGravity").ToString <> "" Then
                    Me.txtSpGravity.Text = CType(PostBlendGradingTbl_GravityDataReader("AverageSpecificGravity").ToString, Double)
                Else
                    Me.txtSpGravity.Text = 0.0
                End If
            End While
            'Call UpdateForm1Object()

        End If

    Catch ex As Exception
        Dim msg As String = "Error Populating from the PostBlendGradingTbl"
        MsgBox(msg & vbLf & "The log file is written in " & vbLf & ex.Message & vbLf & ex.StackTrace)
        ErrorLog(msg, ex.StackTrace, "PostBlendGradingTbl - Error")
    End Try
End Sub


However when I have ShiftSnapShot = 2 the PopulateDataFromPostBlendGradingTbl throws providerbase.fieldnamelookup.getordinal(string fieldname) error. the problem is something related to the way I have been using avg(CountPerPound) in my query.. If I select CountPerPound instead of select avg(CountPerPound) then it works fine and does not give me an error. for some reason the datareader is not able to identofy my column CPP which is a defined column based on avg(CountPerPound) as CPP.. Please Help
Posted
Updated 2-Mar-13 13:43pm
v2

1 solution

It's because your 1st query is returning a field called CountPerPount and your 2nd query is returning a field called CPP
You then have a section of code ...
VB
If PostBlendGradingTblDataReader("CountPerPound").ToString <> "" And ShiftSnapShot = 1 Then
                    Me.txtCPP.Text = CType(PostBlendGradingTblDataReader("CountPerPound").ToString, Double)
                ElseIf PostBlendGradingTblDataReader("CPP").ToString <> "" And ShiftSnapShot = 2 Then
                    Me.txtCPP.Text = CType(PostBlendGradingTblDataReader("CPP").ToString, Double)

But if ShiftSnapShot = 2 then there is no column called CountPerPount returned - and the code will throw an error.

I'm not sure how VB.NET evaluates an if statement - if it fails when the first condition fails and does not attempt to evaluate the next condition, then simply check the value of ShiftSnapShot first and then the returned column ...e.g.
If ShiftSnapShot = 1 And PostBlendGradingTblDataReader"CountPerPound").ToString <> ""

Alternatively use the same column name in your sql i.e.
select avg(CountPerPound) as CountPerPound 
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900