2013-05-02 28 views
0

我使用一个自定义的ASP.NET控件,我发现这里:http://www.codeproject.com/Articles/5347/DataCalendar添加会话变量作为OleDbParameter - 运行到错误“

我已经使用了从上面的页面源文件下载,能够在一个模板作为我的自定义日历的起点,我的目标是只显示当前用户创建的事件,我通过创建一个名为“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 

那么,怎么回事?

回答

1

从MSDN:

The OLE DB.NET Provider does not support named parameters for passing parameters to an SQL Statement or a stored procedure called by an OleDbCommand when CommandType is set to Text. In this case, the question mark (?) placeholder must be used. For example:

SELECT * FROM Customers WHERE CustomerID = ? 

Therefore, the order in which OleDbParameter objects are added to the OleDbParameterCollection must directly correspond to the position of the question mark placeholder for the parameter.

尝试:

cmd.CommandText = String.Format("Select EventDate, CreatedBy, Count(*) From EventInfo Where ([CreatedBy = ?]) and EventDate >= #{0}# And EventDate <= #{1}# Group By EventDate,CreatedBy", startDate, endDate)