我使用的是在这里找到的自定义ASP.NET控件:http://www.codeproject.com/Articles/5347/DataCalendar
我已经使用了源文件中的一个模板作为我的自定义日历的起点。我的目标是只显示当前用户创建的事件。为此,我创建了一个名为"userName“的会话变量,并在查询中将其参数化,如下所示:
Function GetEventData(startDate As DateTime, endDate As DateTime) As DataTable
'--read data from an Access query
Dim con As OleDbConnection = GetConnection()
Dim cmd As OleDbCommand = New OleDbCommand()
cmd.Connection = con
cmd.Parameters.AddWithValue("@currentUser", Session("currentuser"))
cmd.CommandText = String.Format("Select EventDate, CreatedBy, Count(*) From EventInfo Where (CreatedBy = @currentUser) and EventDate >= #{0}# And EventDate <= #{1}# Group By EventDate", _
startDate, endDate)
Dim ds As DataSet = New DataSet()
Dim da As OleDbDataAdapter = New OleDbDataAdapter(cmd)
da.Fill(ds)
con.Close()
Return ds.Tables(0)
End Function不幸的是,我收到了这个错误:
Parameter[0] '' has no default value.我已经确保我已经登录了,所以不存在User.Identity.Name没有值的问题(我不这么认为)。我在页面加载子模块中创建会话变量:
Sub Page_Load(o As Object, e As EventArgs)
Session("currentuser") = User.Identity.Name
End Sub那么,出了什么问题呢?
发布于 2013-05-02 19:40:54
来自MSDN:
当CommandType设置为Text时,
OLE DB.NET提供程序不支持使用命名参数将参数传递给OleDbCommand调用的SQL语句或存储过程。在本例中,问号(?)必须使用占位符。例如:
SELECT * FROM Customers WHERE CustomerID = ?因此,OleDbParameter对象添加到OleDbParameterCollection的顺序必须直接对应于参数的问号占位符的位置。
尝试:
cmd.CommandText = String.Format("Select EventDate, CreatedBy, Count(*) From EventInfo Where ([CreatedBy = ?]) and EventDate >= #{0}# And EventDate <= #{1}# Group By EventDate,CreatedBy", startDate, endDate)https://stackoverflow.com/questions/16336953
复制相似问题