2010-03-07 121 views
0

我已經使用Visual Studio 2008 ASP.NET AccessDataSource嚮導來生成更新命令。 GridView編輯按鈕的作品。但是,當我點擊Gridview中的更新鏈接時,我收到以下消息:System.Data.OleDb.OleDbException:沒有給出一個或多個所需參數的值

System.Data.OleDb.OleDbException: No value given for one or more required parameters 

嚮導是否有錯誤?

這是它生成的源代碼:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %> 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 

<html xmlns="http://www.w3.org/1999/xhtml"> 
<head runat="server"> 
    <title>Untitled Page</title> 
</head> 
<body> 
    <form id="form1" runat="server"> 
    <div> 

    </div> 
    <asp:GridView ID="GridView1" runat="server" AllowPaging="True" 
     AllowSorting="True" AutoGenerateColumns="False" AutoGenerateDeleteButton="True" 
     AutoGenerateEditButton="True" AutoGenerateSelectButton="True" DataKeyNames="Id" 
     DataSourceID="AccessDataSource1" PageSize="1"> 
     <Columns> 
      <asp:BoundField DataField="Id" HeaderText="Id" InsertVisible="False" 
       ReadOnly="True" SortExpression="Id" /> 
      <asp:BoundField DataField="TaskName" HeaderText="TaskName" 
       SortExpression="TaskName" /> 
      <asp:BoundField DataField="TaskDescription" HeaderText="TaskDescription" 
       SortExpression="TaskDescription" /> 
      <asp:BoundField DataField="TaskPriority" HeaderText="TaskPriority" 
       SortExpression="TaskPriority" /> 
     </Columns> 
    </asp:GridView> 
    <asp:AccessDataSource ID="AccessDataSource1" runat="server" 
     ConflictDetection="CompareAllValues" DataFile="~/App_Data/tasks.mdb" 
     DeleteCommand="DELETE FROM [Tasks] WHERE [Id] = ? AND (([TaskName] = ?) OR ([TaskName] IS NULL AND ? IS NULL)) AND (([TaskDescription] = ?) OR ([TaskDescription] IS NULL AND ? IS NULL)) AND (([TaskPriority] = ?) OR ([TaskPriority] IS NULL AND ? IS NULL))" 
     InsertCommand="INSERT INTO [Tasks] ([Id], [TaskName], [TaskDescription], [TaskPriority]) VALUES (?, ?, ?, ?)" 
     OldValuesParameterFormatString="original_{0}" 
     SelectCommand="SELECT * FROM [Tasks]" 
     UpdateCommand="UPDATE [Tasks] SET [TaskName] = ?, [TaskDescription] = ?, [TaskPriority] = ? WHERE [Id] = ? AND (([TaskName] = ?) OR ([TaskName] IS NULL AND ? IS NULL)) AND (([TaskDescription] = ?) OR ([TaskDescription] IS NULL AND ? IS NULL)) AND (([TaskPriority] = ?) OR ([TaskPriority] IS NULL AND ? IS NULL))"> 
     <DeleteParameters> 
      <asp:Parameter Name="original_Id" Type="Int32" /> 
      <asp:Parameter Name="original_TaskName" Type="String" /> 
      <asp:Parameter Name="original_TaskDescription" Type="String" /> 
      <asp:Parameter Name="original_TaskPriority" Type="Single" /> 
     </DeleteParameters> 
     <UpdateParameters> 
      <asp:Parameter Name="TaskName" Type="String" /> 
      <asp:Parameter Name="TaskDescription" Type="String" /> 
      <asp:Parameter Name="TaskPriority" Type="Single" /> 
      <asp:Parameter Name="original_Id" Type="Int32" /> 
      <asp:Parameter Name="original_TaskName" Type="String" /> 
      <asp:Parameter Name="original_TaskDescription" Type="String" /> 
      <asp:Parameter Name="original_TaskPriority" Type="Single" /> 
     </UpdateParameters> 
     <InsertParameters> 
      <asp:Parameter Name="Id" Type="Int32" /> 
      <asp:Parameter Name="TaskName" Type="String" /> 
      <asp:Parameter Name="TaskDescription" Type="String" /> 
      <asp:Parameter Name="TaskPriority" Type="Single" /> 
     </InsertParameters> 
    </asp:AccessDataSource>  
    </form> 
</body> 
</html> 

回答

1

更改UpdateParamters部分是這樣的:

<UpdateParameters> 
     <asp:Parameter Name="TaskName" Type="String" /> 
     <asp:Parameter Name="TaskDescription" Type="String" /> 
     <asp:Parameter Name="TaskPriority" Type="Single" /> 
     <asp:Parameter Name="original_Id" Type="Int32" /> 
    </UpdateParameters> 

而且你UpdateCommand這個(只是刪除換行符):

UPDATE [Tasks] 
SET [TaskName] = ?, [TaskDescription] = ?, [TaskPriority] = ? 
WHERE [Id] = ? 

這裏假設你的ID列是主鍵,我不知道爲什麼嚮導像創建它一樣,但不需要比較其他列。

+0

它似乎產生這一點,因爲我檢查了選項「使用樂觀併發」。如果我沒有選中它會產生你說的話,否則它會起作用。 – user310291 2010-03-07 14:19:13

1

在我的經驗,該錯誤消息通常信號類型不正確的列名。

我建議你對代碼進行逐步調試,並嘗試檢查一次執行參數替換時每個命令生成的SQL。

+0

感謝嗨,我終於找到了原因,雖然這很煩人,它不能管理樂觀併發。 – user310291 2010-03-07 14:19:48

1

也可能是因爲列名錯誤。 例子:

cmd.CommandText = "" 
    + "UPDATE Marriage " 
    + " SET labelprint = ? " 
    + " WHERE model = ? AND plant = ? AND seq = ? " 
    + ""; 

將始終報告「System.Data.OleDb.OleDbException:沒有爲一個或多個必需的參數給定值」,因爲錯誤的列名錯誤。但正如發現正確的列名是「LabelPrinted」(但不是「LabelPrint」)!

因此,它看起來像MS Access可能會解釋錯誤的列名作爲參數,如結果 - 報告的錯誤......

相關問題