Tuesday, October 22, 2013

Stored Procedures

ALTER PROCEDURE [dbo].[usp_SetItemCategory]
    @ncName            nvarchar(100),
    @ncDesctiption  nvarchar(256)

       
AS
BEGIN
--Check to make sure the Name  does not already exist
    DECLARE @existing AS int = 0
    SELECT @existing=count(Name)
    FROM dbo.tbl_itemCategory
    WHERE Name=@ncName

    IF @existing > 0
    BEGIN
        RAISERROR ('Name is  already exists', 1, 1)
        RETURN 2
    END

    --Attempt insert
    INSERT INTO dbo.tbl_itemCategory
                (Name,
                 Desctiption,
                 Status)
            VALUES       
            (@ncName,
            @ncDesctiption,
            1)

            --check to see if insert occured
           --and return status
           IF @@ROWCOUNT = 1
                RETURN 1
           ELSE
                RETURN 0
        
END
..............................................................
  public int SetItemCategory(string name, string description)
        {

            int return_value = 0;
            SqlConnection Conn = DBConn.GetDbConnection();
            try
            {
               

                SqlCommand cmd = new SqlCommand("usp_SetItemCategory", Conn);
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.AddWithValue("@ncName", name);
                cmd.Parameters.AddWithValue("@ncDesctiption", description);

                //get the return value from sp
                SqlParameter returnParam = new SqlParameter();
                returnParam.ParameterName = "returnValue";
                returnParam.Direction = ParameterDirection.ReturnValue;
                cmd.Parameters.Add(returnParam);

                cmd.ExecuteNonQuery();

                if (cmd.Parameters["returnValue"].Value.ToString() == "2")
                {
                    return_value = 2;
                }
                else if (cmd.Parameters["returnValue"].Value.ToString() == "1")
                {
                    return_value = 1;
                }


            }
            catch(Exception ex)
            {
                throw ex;

            }
            finally
            {
                Conn.Close();
            }

           

                return return_value;
           

        }
...............................................Update Stored Procedure
ALTER PROCEDURE dbo.usp_UpdateItemCategory
    @inCatID        int,
    @ncName            nchar(100),
    @ncDesctiption  nchar(256)
AS
BEGIN
--Check to make sure the Name  does not already exist
    DECLARE @existing AS int = 0
    SELECT @existing=count(Name)
    FROM dbo.tbl_itemCategory
    WHERE Name=@ncName AND CatID!=@inCatID

    IF @existing > 0
    BEGIN
        RAISERROR ('Name is  already exists', 1, 1)
        RETURN 2
    END
    --attempt to update
    UPDATE tbl_itemCategory
    SET              
    Name =@ncName,
    Desctiption =@ncDesctiption
    WHERE(CatID=@inCatID)
       --and return status
          /* IF @@ROWCOUNT = 1
                RETURN 1
           ELSE
                RETURN 0*/
END
 

0 comments:

Post a Comment

Popular Posts

Recent Posts

Text Widget

Powered by Blogger.

Find Us On Facebook

Flickr Images

Recent Posts

Video Of Day

Random Posts

Facebook

Random Posts

Advertising

Popular Posts