0

我有一个页面,我试图根据下拉列表(cbLocation)中的选择和从日历扩展器(txtFrom和txtTo)输入到文本框中的开始/结束日期来填充按钮单击时的gridview(gvClockings)。

我已经测试了 SQL 语句,如果我在 Access 中运行它,它会得到预期的结果(在这个解决方案中,我有类似的 SQL 可以在不同的表单上正常工作),但是在这个表单上,无论我在 3 中选择什么控件,gridview 不会显示任何记录。

cbLocation 填充在 Page_Load 中,如下所示:

If Not Page.IsPostBack Then
        Dim ddl As DropDownList = cbLocation
        Dim ir As ListItem = New ListItem
        Dim ee As ListItem = New ListItem
        ir.Text = "IR Room"
        ir.Value = "IR Room"
        ee.Text = "Employee Exit"
        ee.Value = "Employee Exit"
        ddl.Items.Add(ir)
        ddl.Items.Add(ee)
    End If

然后我有下面的代码来获取数据并填充gridview:

    Private Sub GetClockings(ByVal LLName As String, ByVal FromDate As String, ByVal ToDate As String)

    Dim conclock As OleDbConnection = New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0; DATA SOURCE=\\xxx.xx.xxx.xx\AccuFace\pspcms.mdb;Jet OLEDB:Database Password=xxxxx;")
    Dim daClocking As OleDbDataAdapter = New OleDbDataAdapter
    Dim selectSQL As String = "SELECT efrLog.LFirstName, efrLog.LLastName, efrLog.LDepartment, efrLog.LDate, efrLog.LTime, efrLog.LType, efrLog.LLName FROM efrLog WHERE (((efrLog.LDate) Between [@FROMDATE] And [@TODATE]) AND ((efrLog.LLName)=[@LLName])) ORDER BY efrLog.LDepartment, efrLog.LTime"
    Dim selectCMD As OleDbCommand = New OleDbCommand(selectSQL, conclock)
    daClocking.SelectCommand = selectCMD

    selectCMD.Parameters.AddWithValue("@LLName", LLName)
    selectCMD.Parameters.AddWithValue("@FROMDATE", FromDate)
    selectCMD.Parameters.AddWithValue("@TODATE", ToDate)

    Dim dsClockings As DataSet = New DataSet
    daClocking.Fill(dsClockings, "efrLog")

    If dsClockings.Tables("efrLog").Rows.Count > 0 Then
        gvClockings.DataSource = dsClockings.Tables("efrLog")
        gvClockings.DataBind()
    Else
        lblNotFound.Text = "No clockings found for this location"
        lblNotFound.Visible = True
        Exit Sub
    End If
End Sub

上面的代码是从 Button_click 调用的,如下所示:

Dim LLName As String
    Dim FromDate As String = txtFrom.Text
    Dim ToDate As String = txtTo.Text


    If cbLocation.SelectedValue = "IR Room" Then
        LLName = "IR Room"
        GetClockings(LLName, FromDate, ToDate)
    End If

    If cbLocation.SelectedValue = "Employee Exit" Then
        LLName = "Employee Exit"
        GetClockings(LLName, FromDate, ToDate)
    End If

    lblLocation.Text = "Clocking report for " & cbLocation.SelectedValue.ToString() & " generated on " & Date.Now
    lblLocation.Visible = True

我已尽我所能在本地进行调试(我在 IIS 的远程服务器上使用访问数据库,因此在本地运行时尝试填充数据集时会出现与连接字符串/权限相关的错误。我知道这是一个可怕的设置,但它就是我已经被安排工作,但我不知道如何远程调试),据我所知,参数似乎正在接收正确的值(见下文)

截屏

我从数据库请求的数据肯定存在,所以我完全不知道为什么 gridview 没有填充。任何帮助将不胜感激。谢谢

4

1 回答 1

4

OleDb 提供者不通过名称识别参数,而是通过它们的位置。
您以错误的顺序添加参数值,因为它们出现在命令字符串中。
可能这会导致错误或不返回任何内容...

尽量以这种方式尊重命令文本中参数的定义顺序

Dim selectSQL As String = "SELECT .....FROM ... WHERE " & _
                          "(((efrLog.LDate) Between [@FROMDATE] And [@TODATE]) AND " & _
                          "((efrLog.LLName)=[@LLName])) " & _
                          "ORDER BY efrLog.LDepartment, efrLog.LTime"

selectCMD.Parameters.AddWithValue("@FROMDATE", FromDate)
selectCMD.Parameters.AddWithValue("@TODATE", ToDate)
selectCMD.Parameters.AddWithValue("@LLName", LLName)
于 2013-05-24T14:33:30.590 回答