Click here to Skip to main content
15,887,267 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i've been stuck on how to do two insertions in one button click event. what i am trying to do is one insertion, then call the newly inserted id value so that i can insert it into another table using a different method.

so basically:

button click - call bll and bo - send data to dal and stored procedure.
then
stored procedure sends id back to dal - stuck here...

i know for sure that the stored procedure works. when i execute it, it returns the value of the newly inserted id which i double checked with the table.

so the first part of the insertion works...it's going from the first stored procedure back to the dal to the bll and back to the code behind so that i can start the second insertion that's messing me up and i would like to try to learn the n-tier architecture.

i'm new to programming in general so i'm learning as i go. chances are the fix is simple for an experienced coder...which i'm not :P

thank you so much for any help offered.

here's what i've got so far....

code behind:
C#
protected void submitButton_Click(object sender, EventArgs e)
    {
        if (!Page.IsValid)
            return;

        int intResult = 0;

        sBLL scBLL = new sBLL();
        
        sBO scBO = new sBO();
        
        sDAL scdal = new sDAL(); //I suspect I shouldn't even have this line in here in the first place

        scBO.sName = sNameText.Text;
        scBO.orgID = 1;

        try
        {
            intResult = scBLL.Insert(scBO);
            if (intResult > 0)
            {
                lblMessage.Text = "New record inserted successfully.";
            int sID = intResult;               //now trying to insert the new ID into another table...
            mascidBLL masciBLL = new mascidBLL();
            mascidBO masciBO = new mascidBO();
            foreach (ListItem li in CheckBoxList1.Items)
                {
                    if (li.Selected)
                    {
                        masciBO.sID = sID;
                        masciBO.mID = int.Parse(li.Value); //mID belongs to another table.
                        masciBLL.Insert(masciBO);
                    }
                }
            }

            else
                lblMessage.Text = "failed";

        }
        catch (Exception ee)
        {
            lblMessage.Text = ee.Message.ToString();
        }
        finally
        {
            scBO = null;
            scBLL = null;
        }
    }


bal:
C#
public int Insert(sBO scBO)
    {
        sDAL scDAL = new sDAL();
        try
        {
            scDAL.Insert(scBO);
            return scBO.sID;
        }
        catch
        {
            throw;
        }
        finally
        {
            scDAL = null;
        }
    }


bo:
C#
public int sID
    {
        get
        {
            return sID_BO;
        }
        set
        {
            sID_BO = value;
        }
    }

    public string sName
    {
        get
        {
            return sName_BO;
        }
        set
        {
           sName_BO = value;
        }
    }

    public int orgID
    {
        get
        {
            return orgID_BO;
        }
        set
        {
            orgID_BO = value;
        }
    }


dal:
C#
public int Insert(sBO scBO)
    {
        SqlConnection conn = new SqlConnection(connSTR);
        conn.Open();
        SqlCommand scIns = new SqlCommand("Insert", conn);
        scIns .CommandType = CommandType.StoredProcedure;
        try
        {
            scIns .Parameters.AddWithValue("@sName", scBO.sName);
            scIns .Parameters.AddWithValue("@orgID", scBO.orgID);
            scIns.Parameters.Add("@sID", SqlDbType.Int, 0, "sID");
            scIns.Parameters["@sID"].Direction = ParameterDirection.Output;
            
            return sBO.sID;
        }
        catch
        {
            throw;
        }
        finally
        {
            scIns.Dispose();
            conn.Close();
            conn.Dispose();
        }
    }


stored procedure:
SQL
ALTER PROCEDURE Insert
	(
		@sName varchar(50),
		@orgID int
	)
AS
	/* SET NOCOUNT ON */
	DECLARE @Count int
	DECLARE @sID int

		
	select @Count = Count(sName) from s WHERE sName = @sName
	
	IF @Count = 0
		BEGIN
				INSERT INTO s (sName, orgID)
				VALUES (@sName, @orgID)
										 

			SET @sID =  SCOPE_IDENTITY()
			END

									 
	RETURN @sID
Posted

1 solution

Can't you do both insertions from the same stored procedure? That way you only need one trip to the database and let the stored procedure handle everything.

You're already getting the newly created key: use that to insert the new record in another table.

If you do go this way I'd also wrap the whole in a transaction and return success/failure to the calling method so you can take some action depending on the result.
 
Share this answer
 
Comments
[no name] 9-May-13 20:00pm    
i thought about that. the problem is...how do i send multiple values from the checkbox list into the stored procedure?

lets say the user checks box1, box2, box3, etc...how do i send all of those values through the bll/bo/dal into the stored procedure?

forgive me for the crappy question and all, i'm just limited by what little i know of this stuff. that's more or less why i figured i'd just send the value back to the code behind so i could run the second insertion. that was what i'd have been most familiar with, but i'm definitely willing to try your method. just stumped how i'd send all the values.

thank you for the reply
R. Giskard Reventlov 9-May-13 20:09pm    
Well, I guess you could send an array of parameters to the stored proc and parse them in there (there are loads of articles on Google about how to parse a parameter that is an array). Or you could create a bunch of insert statements and pass those as a string and then execute them in the stored proc (not the best idea). I'm sure others will have other ways of solving this but that should give you some food for thought.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900