只能看到SqlDataReader的最后一行

时间:2014-03-10 21:00:19

标签: sql vb.net loops sqldatareader

所以我有一个gridview,显示员工计划工作的时间。网格限制为12周,仅显示所选项目的小时数。网格是存储过程的一种产品,它创建一个数据透视表,工作周作为透视列。

我为网格中的每个单元格添加了一个工具提示,显示每周计划的总小时数(显示任何项目的计划小时数,而不仅仅是所选项目)。如果员工只安排了一个项目的小时数,则工具提示会按预期显示。但是,当存在多个项目时,只有查询的最后一个项目(最后一行)才会进入工具提示。

我可以看到发生了什么,我正在为每一行重新创建strTooltip变量,这就是为什么我只显示最后一行的原因。但我似乎无法巧妙地设计出有效读取数据的方法。

以下是在网格的rowdatabound事件中调用的:

        Using con As New SqlConnection(cnSQLLive)

            con.Open()

            Dim cmd As New SqlCommand()
            cmd.CommandType = CommandType.StoredProcedure
            cmd.CommandText = "spJobForecastingGetEmployeeProjectBreakdown"
            cmd.Connection = con

            cmd.Parameters.Add("@alias", SqlDbType.VarChar)
            cmd.Parameters.Add("@startdate", SqlDbType.DateTime)

            cmd.Parameters("@alias").Value = e.Row.Cells(4).Text
            cmd.Parameters("@startdate").Value = datStartDate

            Dim reader As SqlDataReader = cmd.ExecuteReader()

            Do While reader.Read()
                For i As Integer = 5 To e.Row.Cells.Count - 1

                    Dim strTooltip As String = ""
                    Dim intTotalWeekHours As Integer = 0
                    Dim strWorkWeek As String = GridView1.HeaderRow.Cells(i).Text
                    Dim tempTextboxAs TextBox = DirectCast(e.Row.Cells(i).Controls(0), TextBox)

                    If Not IsDBNull(reader(strWorkWeek)) Then

                        strTooltip += reader(strWorkWeek) & " Hours -- " & reader("Project") & "<br />" & vbCrLf
                        intTotalWeekHours += reader(strWorkWeek)

                    End If

                    strTooltip += "-------------" & "<br />" & intTotalWeekHours & " Total Hours This Week"
                    tempTextbox.ToolTip = strTooltip

                Next

            Loop

        End Using

提前致谢!

1 个答案:

答案 0 :(得分:0)

我通过在循环外创建数组来解决这个问题,如下所示:

        Dim TooltipArray() As String = {"", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", ""}
        Dim TotalHoursArray() As Integer = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}

        Using con As New SqlConnection(cnSQLLive)

            con.Open()

            Dim cmd As New SqlCommand()
            cmd.CommandType = CommandType.StoredProcedure
            cmd.CommandText = "spJobForecastingGetEmployeeProjectBreakdown"
            cmd.Connection = con

            cmd.Parameters.Add("@alias", SqlDbType.VarChar)
            cmd.Parameters.Add("@startdate", SqlDbType.DateTime)

            cmd.Parameters("@alias").Value = e.Row.Cells(4).Text
            cmd.Parameters("@startdate").Value = HiddenFieldDate.Value

            Dim reader As SqlDataReader = cmd.ExecuteReader()

            Do While reader.Read()
                For i As Integer = 5 To e.Row.Cells.Count - 1

                    Dim strTooltip As String = ""
                    Dim intTotalWeekHours As Integer = 0
                    Dim strWorkWeek As String = GridViewProjectEntry.HeaderRow.Cells(i).Text

                    Try

                        If Not IsDBNull(reader(strWorkWeek)) Then
                            If reader("Project") = " - " Then
                                TooltipArray(i) += reader(strWorkWeek) & " Hours -- Department Overhead" & "<br />" & vbCrLf
                            Else
                                TooltipArray(i) += reader(strWorkWeek) & " Hours -- " & reader("Project") & "<br />" & vbCrLf
                            End If

                            TotalHoursArray(i) += reader(strWorkWeek)
                        End If
                    Catch ex As Exception

                    End Try

                Next

            Loop

        End Using

希望能帮到别人!