I think i have been looking at this to long.. I have 2 gridviews with data. I want to loop through them and send each item to a stored procedure. That stored procedure checks for duplicates and sends back 1 if they exists and 0 if they don't. Well when I do the for each loop.. I get stuck. I have tried moving things around with no luck.. Here is my code:
public int IsExists()
{
foreach (GridViewRow row in gvSerials.Rows)
{
using (SqlConnection con = new SqlConnection(strConnString))
{
using (SqlCommand cmd = new SqlCommand("usp_InsertReceiptSerials", con))
{
cmd.CommandType = CommandType.StoredProcedure;
con.Open();
cmd.Parameters.Add("@ITEMNMBR", SqlDbType.Char).Value = OpenDescription.SelectedRow.Cells[5].Text.Trim();
cmd.Parameters.Add("@RecLineID", SqlDbType.Int).Value = int.Parse(OpenDescription.SelectedRow.Cells[1].Text.Trim());
cmd.Parameters.Add("@RCPTLNNM", SqlDbType.Int).Value = int.Parse(OpenDescription.SelectedRow.Cells[8].Text.Trim());
cmd.Parameters.Add("@POPRCTNM", SqlDbType.Char).Value = OpenDescription.SelectedRow.Cells[4].Text.Trim();
cmd.Parameters.Add("@SERLTNUM", SqlDbType.Char).Value = row.Cells[0].Text.Trim();
SqlParameter parm = new SqlParameter("@IsExists", SqlDbType.Int);
parm.Direction = ParameterDirection.Output;
cmd.Parameters.Add(parm);
cmd.ExecuteNonQuery();
con.Close();
}
}
}
return IsExists();
if (IsExists() == 1) {
MessageBox.Show("Serials Already Exists!!");
Response.Redirect("Index.aspx"); }
else if (IsExists() == 0) {
MessageBox.Show("Serial Numbers have been updated.", "Important Message", MessageBoxButtons.OK, MessageBoxIcon.Information);
Response.Redirect("Index.aspx"); }
}
Here is the stored proc:
ALTER PROCEDURE [dbo].[usp_InsertReceiptSerials]
@POPRCTNM CHAR(17), @ITEMNMBR CHAR(31), @SERLTNUM CHAR(21), @RecLineID INT, @RCPTLNNM INT, @IsExists INT OUTPUT
AS
BEGIN
SET NOCOUNT ON;
DECLARE @RecCount INT
-- DECLARE @IsExists INT
IF EXISTS (SELECT * FROM dbo.vwSerialNumbers WHERE SERLNMBR = Right(@SERLTNUM,20) AND ITEMNMBR = @ITEMNMBR)
BEGIN
SET @IsExists = 1
END
ELSE
BEGIN
INSERT INTO dbo.usr_ReceiptSerials(POPRCTNM, ITEMNMBR, SERLTNUM, FullSerialNumber, EntryDate, RecLineID, RCPTLNNM)
VALUES (@POPRCTNM, @ITEMNMBR, Right(@SERLTNUM,20), @SERLTNUM, GetDate(), @RecLineID, @RCPTLNNM)
SET @RecCount = (SELECT COUNT(*) FROM dbo.usr_ReceiptSerials WHERE RecLineID = @RecLineID)
UPDATE dbo.usr_ReceiptLine SET QTYSHPPD = @RecCount
WHERE RecLineID = @RecLineID
SET @IsExists = 0
END
END