For example, BLL Insert method, Do I have to put the message for the inserted record in the sproc or in the BLL or in the codehind of the asp pages? I currenttly have a sproc which returns a booleam for the wether the record has been inserted or not, but my bll methods is only retruning a true in every case, the user doen't see the error message from the codebehind which is set to fire when the returns a 0 and the insert operation has faled.
This is one of the BLL metheds..
[System.ComponentModel.DataObjectMethodAttribute(System.ComponentModel.DataObjectMethodType.Insert, false)]
public int InsertOwnership(int contactId, int staffDivId, int? themeId, int? statusId, bool inActive)
{
try
{
//Create a new ownershipRow instance
MarketingContacts.OwnershipDataTable ownerships = new MarketingContacts.OwnershipDataTable();
MarketingContacts.OwnershipRow ownership = ownerships.NewOwnershipRow();
ownership.ContactId = contactId;
ownership.StaffDivId = staffDivId;
if (themeId == null) ownership.SetThemeIdNull();
else ownership.ThemeId = themeId.Value;
if (statusId == null) ownership.SetStatusIdNull();
else ownership.StatusId = statusId.Value;
ownership.InActive = inActive;
//Add the new ownership
ownerships.AddOwnershipRow(ownership);
int OwnershipExits = Adapter.Update(ownerships);
return OwnershipExits;
}
catch (Exception ex)
//catch (SqlException ex)
{
//throw new Exception("not string", ex);
throw ex;
}
...............And this the sproc fro the insert commanda:.................
ALTER PROCEDURE [dbo].[spInsertOwnership]
-- Add the parameters for the stored procedure here
@ContactId int,
@StaffDivId int,
@ThemeId int,
@StatusId int,
@InActive bit = 0
AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;
DECLARE @Result int --varchar(100)
if exists (SELECT ContactId, StaffDivId, InActive
FROM Ownership
WHERE (ContactId = @ContactId) AND (StaffDivId = @StaffDivId) AND (InActive = 0))
Begin
Set @Result = 0 --'duplicate found'
End
Else
BEGIN
-- Insert statements for procedure here
INSERT INTO [dbo].[Ownership]
([ContactId],
[StaffDivId],
[ThemeId],
[StatusId],
[InActive])
VALUES
(@ContactId,
@StaffDivId,
@ThemeId,
@StatusId,
@InActive)
set @Result = 0 --'new onership created'
End
SELECT @Result as Result
END
........ this is my codebehind............
protected void AsignContactToOwner_Click(object sender, EventArgs e)
{
if (StaffPlusDivision.SelectedIndex == 0)
{
LblErrorMessage.Text = "You need to select a staff name from the list below";
LblErrorMessage.Visible = true;
}
else
foreach (GridViewRow row in this.GridViewAllContacts.Rows)
{
CheckBox checkBox = row.FindControl("TakeOwnership") as CheckBox;
if (checkBox.Checked)
{
Int32 temp = row.DataItemIndex;
Int32 pagenum = GridViewAllContacts.PageIndex;
Int32 pagesize = GridViewAllContacts.PageSize;
Int32 temporaldirective = temp - (pagenum * pagesize);
string contactId = GridViewAllContacts.DataKeys[temporaldirective].Value.ToString();
string cn = row.Cells[3].Text;
ObjectDataSource myOwnership = new ObjectDataSource();
OwnershipBLL ow = new OwnershipBLL();
int? themeId = null;
int? statusId = null;
try
{
int myResult = ow.InsertOwnership(int.Parse(contactId), StaffPlusDivision.SelectedIndex, themeId, statusId, false);
LblErrorMessage.Visible = false;
if (myResult == 0)
{
LblErrorMessage.Text = "You already have this contact on your list";
LblErrorMessage.Visible = true;
}
else
{
ow.InsertOwnership(int.Parse(contactId), StaffPlusDivision.SelectedIndex, themeId, statusId, false);
}
ClientScript.RegisterStartupScript(this.GetType(), "message", "alert('The contact(s) have been added to your List.');",true);
GridViewAllContacts.DataSourceID = "SqlDataSourceAllContact";
GridViewAllContacts.DataBind();
}
catch (Exception ex)
{
LblErrorMessage.Text = ex.Message;
LblErrorMessage.Visible = true;
//LblErrorMessage.Visible = true;
}
}
}
}
Now, I know it will easire for me to use sqlDatasource and have a direct connection to to database, but I really want to understand how this could be done. Any help at all will be greatly appreciated.
thank you
Akuyali