我有一个页面,我试图根据下拉列表中的选择(cbLocation)填充gridview(gvClockings),以及从calendarextender(txtFrom和txtTo)输入文本框的from / to日期。
我已经测试了SQL语句,如果我在Access中运行它,它会得到预期的结果(并且我在此解决方案中使用类似的SQL与gridview在另一个表单上工作正常),但无论我选择什么,都在这个表单上在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
我已尽可能在本地调试(我在远程服务器上使用访问数据库,因此在本地运行时尝试填充数据集时会出现与连接字符串/权限相关的错误。我知道这是一个可怕的设置但这是我给予的工作,我不知道如何远程调试)并且据我所知,参数似乎正在接收正确的值(见下文)
我从数据库请求的数据肯定存在,所以我完全不知道为什么gridview没有填充。我们将非常感激地提供任何帮助。感谢
答案 0 :(得分: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)