开发者

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

I have used Visual Studio 2008 ASP.NET AccessDataSource Wizard to generate the update command. The gridview edit button works. But when I click on Update Link in Gridview, I get this message:

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

Is the wizard bugged ?

This is the source code it generated:

<%@ 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开发者_如何学Go (([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>


In my experience, that error message usually signals an incorrectly typed column name.

I suggest you step-debug your code and try to examine the SQL that is generated for each of the commands just before they are executed, once parameter substitution has occurred.


Also could be because of wrong column name. Example:

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

Will always report the "System.Data.OleDb.OleDbException: No value given for one or more required parameters" error because of wrong column name. But as discovered the correct column name was "LabelPrinted" (but not "LabelPrint")!

So, it looks like MS Access may interpret wrong column name as a parameter, as result - reporting that error...


Change your UpdateParamters section like this:

    <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>

And your UpdateCommand to this (just remove line breaks):

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

This assumes your Id column is the primary key, I'm not sure why the wizard created it like it did, but no need to compare those other columns.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜