0

我不断收到 OleDbException :“没有为一个或多个必需参数提供值” - 不是很具体。

我没有运气在网上找到解决我的问题的方法。我尝试了许多不同的方法,例如不在表单中声明参数并在运行时在后面的代码中创建它们。

这是我丑陋的代码:

<asp:sqldatasource  ID="datasource" runat="server" ConnectionString="<%$ ConnectionStrings:ConnectionString %>" ProviderName="<%$ ConnectionStrings:ConnectionString.ProviderName %>" 
    SelectCommand="SELECT [ID], [Test Data] AS Test_Data, [More Data] AS More_Data FROM [Main]" 
    UpdateCommand="UPDATE [Main] SET [Test Data]=@TestData, [More Data]=@MoreData WHERE [ID]=@ID" 
    InsertCommand="INSERT INTO [Main] ([ID], [Test Data], [More Data]) VALUES (@InsertID, @InsertTestData, @InsertMoreData)" 
    DeleteCommand="" >
    <UpdateParameters>
        <asp:ControlParameter Name="TestData" ControlId="updateTest" PropertyName="Text" />
        <asp:ControlParameter Name="MoreData" ControlId="updateMore" PropertyName="Text" />
        <asp:ControlParameter Name="InsertTestData" ControlId="insertTest" PropertyName="Text" />
        <asp:ControlParameter Name="InsertID" ControlId="insertIDD" PropertyName="SelectedValue" />
        <asp:ControlParameter Name="InsertMoreData" ControlId="insertMore" PropertyName="Text" />
        <asp:ControlParameter Name="ID" ControlId="DropDownList2" PropertyName="SelectedValue" />
    </UpdateParameters>
</asp:sqldatasource>

    <asp:GridView ID="GridView1" runat="server" AllowSorting="true" DataSourceID="datasource"></asp:GridView>
    <br />
    <asp:DropDownList ID="insertIDD" DataSourceID="datasource" DataTextField="ID" runat="server" Font-Bold="False"></asp:DropDownList>
    <asp:TextBox ID="insertTest"  runat="server"></asp:TextBox>
    <asp:TextBox ID="insertMore" runat="server"></asp:TextBox>
    <asp:Button ID="Button1" runat="server" Text="Insert" OnClick="Insert" />
    <br />
    <asp:DropDownList ID="DropDownList2" runat="server" DataSourceID="datasource" DataTextField="ID" OnTextChanged="populateDrop" AutoPostBack="true"></asp:DropDownList>
    <asp:TextBox ID="updateTest"  runat="server"></asp:TextBox>
    <asp:TextBox ID="updateMore" runat="server"></asp:TextBox>
    <asp:Button ID="Button2" runat="server" Text="Update" OnClick="Update" />
    <br />
    <asp:Button ID="Button3" runat="server" Text="Delete" OnClick="Delete" />**

以及背后的 C# 代码:

namespace WebApplication2
{
public partial class WebForm1 : System.Web.UI.Page
{
    DataView dv = new DataView();
    protected void Page_Load(object sender, EventArgs e)
    {
        dv = (DataView)datasource.Select(DataSourceSelectArguments.Empty);
        updateTest.Text = dv[0]["Test_Data"].ToString();
        updateMore.Text = dv[0]["More_Data"].ToString();
    }

    protected void Insert(object sender, EventArgs e) 
    {
        datasource.Insert();
    }

    protected void Update(object sender, EventArgs e) 
    {
        datasource.Update();
        updateTest.Text = "";
        updateMore.Text = "";
    }

SELECT 和 UPDATE 工作没有任何问题。无论我尝试什么,我都无法让 INSERT INTO 命令感到高兴。我确信这是我缺少的一些简单的东西,但任何帮助将不胜感激,谢谢!

4

1 回答 1

1

这是为了更新:

    <UpdateParameters>
    <asp:ControlParameter Name="TestData" ControlId="updateTest" PropertyName="Text" />
    <asp:ControlParameter Name="MoreData" ControlId="updateMore" PropertyName="Text" />
    <asp:ControlParameter Name="ID" ControlId="DropDownList2" PropertyName="SelectedValue" />
</UpdateParameters>

这是用于插入

<InsertParameters>
<asp:ControlParameter Name="InsertTestData" ControlId="insertTest" PropertyName="Text" />
    <asp:ControlParameter Name="InsertID" ControlId="insertIDD" PropertyName="SelectedValue" />
    <asp:ControlParameter Name="InsertMoreData" ControlId="insertMore" PropertyName="Text" />
</InsertParameters>

在最后:

<asp:sqldatasource  ID="datasource" runat="server" ConnectionString="<%$ ConnectionStrings:ConnectionString %>" ProviderName="<%$ ConnectionStrings:ConnectionString.ProviderName %>" 
SelectCommand="SELECT [ID], [Test Data] AS Test_Data, [More Data] AS More_Data FROM [Main]" 
UpdateCommand="UPDATE [Main] SET [Test Data]=@TestData, [More Data]=@MoreData WHERE [ID]=@ID" 
InsertCommand="INSERT INTO [Main] ([ID], [Test Data], [More Data]) VALUES (@InsertID, @InsertTestData, @InsertMoreData)" 
DeleteCommand="" >
<UpdateParameters>
    <asp:ControlParameter Name="TestData" ControlId="updateTest" PropertyName="Text" />
    <asp:ControlParameter Name="MoreData" ControlId="updateMore" PropertyName="Text" />
    <asp:ControlParameter Name="ID" ControlId="DropDownList2" PropertyName="SelectedValue" />
</UpdateParameters>
<InsertParameters>
<asp:ControlParameter Name="InsertTestData" ControlId="insertTest" PropertyName="Text" />
    <asp:ControlParameter Name="InsertID" ControlId="insertIDD" PropertyName="SelectedValue" />
    <asp:ControlParameter Name="InsertMoreData" ControlId="insertMore" PropertyName="Text" />
</InsertParameters>
</asp:sqldatasource>
于 2013-08-23T03:18:26.500 回答