SqlDataSource InsertCommand缺少必需参数
本文关键字:参数 InsertCommand SqlDataSource | 更新日期: 2023-09-27 18:28:46
我一直得到一个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命令满意。我确信我缺少的是一些简单的东西,但任何帮助都将不胜感激,谢谢!
这是用于更新:
<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>