首先发布在这里,所以我感谢您提前耐心等待我。我正在为我的网络应用程序执行插入存储过程而旋转轮胎,我希望更多的眼睛可以帮助。
首先是我的存储过程。
CREATE PROCEDURE dbo.InsertCurrencyRecord
@SubCategoryId INT, @PortfolioOwnerId INT,
@OwnerId INT, @ContactId INT,
@LocationId INT,
@Model VARCHAR(50), @Version VARCHAR(15) = NULL,
@Vendor VARCHAR(50), @AvailableDate DATE,
@EndOfProduction DATE, @EndOfSupport DATE,
@NumOfDevices VARCHAR(50), @Costs MONEY,
@UpgradeDuration INT,
@Description VARCHAR(250), @SupportingComments VARCHAR(250)
AS
INSERT INTO dbo.Currency (SubCategoryId, PortfolioOwnerId, OwnerId, ContactId, LocationId, Model, Version, Vendor, AvailableDate, EndOfProduction, EndOfSupport, NumOfDevices, Costs, UpgradeDuration, Description, SupportingComments, ModifiedDate)
VALUES (@SubCategoryId, @PortfolioOwnerId, @OwnerId, @ContactId, @LocationId, @Model, @Version, @Vendor, @AvailableDate, @EndOfProduction, @EndOfSupport, @NumOfDevices, @Costs, @UpgradeDuration, @Description, @SupportingComments, GETDATE());
GO
第二个是asp.dropdownlists,其中我的一些参数应该来自。 SubcategoryId似乎是提供的,因为它是第一个参数,第二个参数是PortfolioId不是,虽然两者之间似乎没有什么不同。
<td style="text-align: right">SubCategory:</td>
<td>
<%-- Confirm from database what max length is --%>
<asp:DropDownList ID="DDSubCategory" runat="server" Height="20px" Width="175px" AutoPostBack="True">
</asp:DropDownList>
<asp:RequiredFieldValidator ID="SubCategoryValidator" runat="server" ControlToValidate="DDSubCategory" Display="Dynamic" ErrorMessage="SubCategory Required" ValidationGroup="AllValidators" ForeColor="Red">*</asp:RequiredFieldValidator>
</td>
<td style="text-align: right; width: 181px;">Portfolio Owner:</td>
<td>
<asp:DropDownList ID="DDPortfolioOwner" runat="server" Height="20px" Width="175px" AutoPostBack="True">
</asp:DropDownList>
<asp:RequiredFieldValidator ID="PortfolioOwnerValidator" runat="server" ControlToValidate="DDPortfolioOwner" Display="Dynamic" ErrorMessage="Portfolio Owner Required" ValidationGroup="AllValidators" ForeColor="Red">*</asp:RequiredFieldValidator>
</td>
最后将是我的c#代码,显示执行存储过程的提交按钮单击方法。
protected void SubmitBtn_Click(object sender, EventArgs e) {
if (Page.IsValid) {
string constr = ConfigurationManager.ConnectionStrings["CurrencyDb"].ConnectionString;
SqlConnection con = new SqlConnection(constr);
SqlCommand cmd = new SqlCommand("dbo.InsertCurrencyRecord", con);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@SubCategoryId", DDSubCategory.SelectedIndex);
cmd.Parameters.AddWithValue("@PorfolioOwnerId", DDPortfolioOwner.SelectedIndex);
cmd.Parameters.AddWithValue("@OwnerId", DDTechOwner.SelectedIndex);
cmd.Parameters.AddWithValue("@ContactId", DDTechContact.SelectedIndex);
cmd.Parameters.AddWithValue("@LocationId", DDBranch.SelectedIndex);
cmd.Parameters.AddWithValue("@Model", ModelBox.Text);
cmd.Parameters.AddWithValue("@Version", VersionBox.Text);
cmd.Parameters.AddWithValue("@Vendor", VendorBox.Text);
cmd.Parameters.AddWithValue("@AvailableDate", ProductAvailableBox.Text);
cmd.Parameters.AddWithValue("@EndOfProduction", EndProductionBox.Text);
cmd.Parameters.AddWithValue("@EndOfSupport", EndOfSupportBox.Text);
cmd.Parameters.AddWithValue("@NumOfDevices", NumDevicesBox.Text);
cmd.Parameters.AddWithValue("@Costs", UpgradeCostBox.Text);
cmd.Parameters.AddWithValue("@UpgradeDuration", UpgradeDurationBox.Text);
cmd.Parameters.AddWithValue("@Description", DescriptionBox.Text);
cmd.Parameters.AddWithValue("@SupportingComments", NotesBox.Text);
try {
con.Open();
cmd.ExecuteNonQuery();
labelMessage.Text = "Your request has been submitted for review.";
}
catch (Exception ex) {
throw new Exception("Error adding record. " + ex.Message);
}
finally {
if (con != null)
con.Close();
}
}
答案 0 :(得分:2)
这里
cmd.Parameters.AddWithValue("@PorfolioOwnerId", DDPortfolioOwner.SelectedIndex)
你错过了 t 中的t
folioOwnerId
因此,'InsertCurrencyRecord'过程不会收到预期的参数'@PortfolioOwnerId'