0
我想添加不同的字段以及所有选择的项目从列表框到使用存储过程和@@标识的数据库。从asp列表框中插入所有选定的项目到数据库
但只有第一个选定的值被插入到表中。当我尝试使用for循环时,它提供了存储过程中参数太多的错误。
我是新来的asp.net,将不胜感激任何可能的帮助。
存储过程:
ALTER Procedure [dbo].[InsertProcessUnit]
(
@ProcessUnitName Varchar(50),
@ProcessUnitNumber int,
@ProcessUnitInspected bit,
@RefineryAreaName Varchar(50),
@System_id int
)
As
Begin
BEGIN TRANSACTION
INSERT INTO Process_Units (Process_Unit_Name, Process_Unit_Number, Process_Unit_Inspected, Refinery_Area_id)
VALUES (@ProcessUnitName, @ProcessUnitNumber,@ProcessUnitInspected,(SELECT Refinery_Area_id from Refinery_Area where Refinery_Area_Name Like @RefineryAreaName))
DECLARE @DataID int;
SET @DataID = @@IDENTITY
INSERT INTO PU_Systems (Process_Unit_id, System_id) VALUES (@DataID, @System_id)
COMMIT
End
Aspx.cs code:
try
{
connection.Open();
SqlCommand command = new SqlCommand();
command.Connection = connection;
command.CommandType = CommandType.StoredProcedure;
command.CommandText = "InsertProcessUnit";
command.Parameters.AddWithValue("@ProcessUnitNumber", PUNumberTxtbox.Text);
command.Parameters.AddWithValue("@ProcessUnitName", PUNameDdl.SelectedItem.Value);
command.Parameters.AddWithValue("@ProcessUnitInspected", PUInspectedDdl.SelectedItem.Value);
command.Parameters.Add("@RefineryAreaName", SqlDbType.Text).Value = RefAreaNameTxtbox.Text;
command.Parameters.AddWithValue("@System_id", ListBox1.SelectedItem.Value);
command.ExecuteNonQuery();
connection.Close();
Label1.Text = "Successfully Inserted !!";
}
似乎你需要清除每个迭代的'SqlParameter'(使用for或foreach循环)。由于列表框不会迭代,所以默认情况下它会检索第一个可用数据以便处理到数据库中。 –