2013-05-02 34 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)