SqlDataSource InsertCommand缺少必需参数

时间:2013-08-23 01:43:04

标签: c# asp.net sql oledb

我一直得到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#Code Behind:

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命令感到高兴。我相信这很简单,我很遗憾,但是非常感谢任何帮助,谢谢!

1 个答案:

答案 0 :(得分: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>