System.Data.OleDb.OleDbException:没有给出一个或多个必需参数的值

时间:2010-03-07 12:42:53

标签: asp.net

我使用Visual Studio 2008 ASP.NET AccessDataSource向导生成更新命令。 gridview编辑按钮有效。但当我点击Gridview中的更新链接时,我收到此消息:

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>

3 个答案:

答案 0 :(得分:2)

根据我的经验,该错误消息通常表示输入的列名称不正确。

我建议您逐步调试代码,并尝试在执行参数替换之后检查为每​​个命令执行之前生成的SQL。

答案 1 :(得分: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列是主键,我不确定为什么向导像它一样创建它,但不需要比较那些其他列。

答案 2 :(得分:1)

也可能因为列名错误。 例如:

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

由于列名错误,将始终报告“System.Data.OleDb.OleDbException:没有为一个或多个必需参数指定值”错误。但是发现正确的列名是“LabelPrinted”(但不是“LabelPrint”)!

因此,看起来MS Access可能会将错误的列名解释为参数,结果 - 报告错误...