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:
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();
scBO.sName = sNameText.Text;
scBO.orgID = 1;
try
{
intResult = scBLL.Insert(scBO);
if (intResult > 0)
{
lblMessage.Text = "New record inserted successfully.";
int sID = intResult;
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);
masciBLL.Insert(masciBO);
}
}
}
else
lblMessage.Text = "failed";
}
catch (Exception ee)
{
lblMessage.Text = ee.Message.ToString();
}
finally
{
scBO = null;
scBLL = null;
}
}
bal:
public int Insert(sBO scBO)
{
sDAL scDAL = new sDAL();
try
{
scDAL.Insert(scBO);
return scBO.sID;
}
catch
{
throw;
}
finally
{
scDAL = null;
}
}
bo:
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:
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:
ALTER PROCEDURE Insert
(
@sName varchar(50),
@orgID int
)
AS
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