Monday, September 30, 2013

Little Programming: ASP.NET C# Popup MessageBox

Little Programming: ASP.NET C# Popup MessageBox:

 public static void ShowAlertMessage(string error)
        {
            Page page = HttpContext.Current.Handler as Page;
            if (page != null)
            {
                error = error.Replace("'", "\'");

                ScriptManager.RegisterStartupScript(page, page.GetType(), "err_msg", "alert('" + error + "');", true);
            }
        }

Cursor For Generate Bill Month

USE [ShopSMCity]
GO
/****** Object:  StoredProcedure [dbo].[SP_Advertising_GetBillMonth]    Script Date: 10/01/2013 10:09:59 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- EXEC SP_Advertising_GetBillMonth  @BillNo='201301260006'
-- =============================================
CREATE PROCEDURE [dbo].[SP_Advertising_GetBillMonth]
(
@BillNo nvarchar(30)
)

AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;
 

Declare
      @MonthBill nvarchar(15),
      @List nvarchar(300),
      @ID int
     
Create table  #temp(
ID int identity(1,1),
[Month YYYY]  nvarchar(20)
)    
insert into #temp([Month YYYY])    
SELECT (CASE
When [PaymentType]= 'Pre Paid' then DATENAME(MONTH, BillDate) + ' ' + DATENAME(YEAR, BillDate)
Else DATENAME(MONTH,dateadd(d,-1, BillDate)) + ' ' + DATENAME(YEAR, dateadd(d,-1, BillDate))

END)  AS [Month YYYY]
--DATENAME(MONTH, BillDate) + ' ' + DATENAME(YEAR, BillDate) AS [Month YYYY]

from Advertising_BillCreationDetails WHERE BillNo=@BillNo order by  BillDate

--select * from     #temp  


 
 DECLARE cursor_MonthList CURSOR FOR

 SELECT distinct  [Month YYYY],ID from #temp order by ID

 DECLARE @charInx int

 OPEN cursor_MonthList
 FETCH NEXT FROM cursor_MonthList INTO @MonthBill,@ID
 set @List = @MonthBill;
 WHILE @@FETCH_STATUS=0
 BEGIN
   

FETCH NEXT FROM cursor_MonthList INTO @MonthBill,@ID

SET @charInx =0

 select @charInx=  charindex(@MonthBill, @List)

 if(@charInx <=0)
 BEGIN
set @List += +', '+ @MonthBill;
END
 END
 CLOSE cursor_MonthList
 DEALLOCATE cursor_MonthList

 select coalesce(@List,'') as BillMonthList

 drop table #temp

END

Query From Different Sales Table

ALTER PROCEDURE [dbo].[SP_DateAndBarcodeWiseSalesDetailsSuppWise]

@supplier nvarchar(100),
@group nvarchar(100),
@product nvarchar(100),
@brand nvarchar(100),

@startDT nvarchar(20),
    @endDate nvarchar(20),
@sMonth int,
@sYear int,
@eMonth int,
@eYear int
AS
BEGIN

SET NOCOUNT ON;


declare @m int,
@y int,
@cYear int,
@month int,
@tblname nvarchar(100),
@tblSaleReturn nvarchar(100),
@stMonth int,
@SQL nvarchar(2000),
@retSQL nvarchar(2000)


create table #tmp
(
a int
)

CREATE TABLE #Sale(
[SupID] [nvarchar](4) NULL,
[Qty] [money] NULL,
[sBarCode] [nvarchar](15) NULL,
[BarCode] [nvarchar](18) NULL,
[SQty] [money] NULL,
[Invoice] [nvarchar](30) NULL

)

CREATE TABLE #SaleReturn(
[Invoice] [nvarchar](30) NULL,
[cInvoice] [nvarchar](30) NULL,
[SupID] [nvarchar](4) NULL,
[Qty] [money] NULL,
[sBarCode] [nvarchar](15) NULL,
[BarCode] [nvarchar](18) NULL,
[SQty] [money] NULL,
[rQty] [money] NULL,
[Returned] [nvarchar](1) NULL

)


SET @m = @sMonth
SET @y = @sYear

set @stMonth=@sMonth
while(@sYear<=@eYear)
begin
-- print convert(nvarchar(50),@sYear,101)

if(@stMonth=13)
begin
set @stMonth=1
end

while(@stMonth<=12)
begin
Set @m = 100+@stMonth
set @tblname='Sale'+convert(nvarchar(50),@sYear,101)+SUBSTRING(Cast(@m as nvarchar(4)),2,3)
set @tblSaleReturn = 'SaleReturn' + convert(nvarchar(50),@sYear,101)+SUBSTRING(Cast(@m as nvarchar(4)),2,3)
--print @tblname
--print @tblSaleReturn
declare
@sY nvarchar(50),
@sM nvarchar(50),
@sDT nvarchar(50),
@eDT nvarchar(50),
@foundTbl int,
@returnFound int

set @sY=cast(@sYear as nvarchar(50))
set @sM=cast(@stMonth as  nvarchar(50) )
set @sDT=convert(nvarchar(50),@startDT,101)
set @eDT=convert(nvarchar(50),@endDate,101)


set @SQL='insert into #tmp SELECT 1  FROM INFORMATION_SCHEMA.TABLES   WHERE TABLE_TYPE=''BASE TABLE''   AND TABLE_NAME= '''+@tblname+'''and TABLE_SCHEMA=''dbo'''
--print @SQL
exec(@SQL)
set @foundTbl = (select a from #tmp)
delete from #tmp

if(@foundTbl = 1)
begin
print ':)'
print @tblname
print @tblSaleReturn

    set @SQL='INSERT INTO #Sale
   (
[SupID],
[Qty],
[sBarCode],
[BarCode],
[SQty],
[Invoice]
)
SELECT
  s2.[SupID],
s2.[Qty],
s2.[sBarCode],
s2.[BarCode],
s2.[SQty],
s2.[Invoice]

FROM '+@tblname+' s2
inner join StyleSize ss
on ss.Barcode = s2.BarCode
WHERE (SaleDt>='''+@sDT+''' and SaleDt<='''+@eDT+''') and ((s2.SupID='''+@supplier+''' and '''+@supplier+'''<>''All'')or('''+@supplier+'''=''All''))'


--print @SQL

set @retSQL = 'INSERT INTO #SaleReturn
  (
[Invoice],
[cInvoice],
[SupID],
[Qty],
[sBarCode],
[BarCode],
[SQty],
[rQty],
[Returned]

)
SELECT
s2.[Invoice],
s2.[cInvoice],
s2.[SupID],
s2.[Qty],
s2.[sBarCode],
s2.[BarCode],
s2.[SQty],
s2.[rQty],
s2.[Returned]

FROM '+@tblSaleReturn+' s2
inner join StyleSize ss
on ss.Barcode = s2.BarCode
WHERE (ReturnedDt>='''+@sDT+''' and ReturnedDt<='''+@eDT+''') and ((s2.SupID='''+@supplier+''' and '''+@supplier+'''<>''All'')or('''+@supplier+'''=''All''))'

--print @retSQL
 
end
else
begin
set @SQL=''
set @retSQL=''
print ':('
end
print @SQL
print @retSQL
exec(@sql)
exec(@retSQL)


if(@stMonth=@eMonth and @sYear=@eYear)
begin
set @stMonth=13
end

else
begin
set @stMonth=@stMonth+1
end

end


set @sYear=@sYear+1
end
--select SUM(sQty) from #Sale
--select SUM(rQty) from #SaleReturn

SQL Trigger


ALTER TRIGGER [dbo].[TR_Buy_ChangeTransferStatus]
   ON [dbo].[Buy]
   AFTER UPDATE
AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;
Declare
@StockID decimal(18,0),
@Item_id nvarchar(10),
@Transfer nvarchar(1)

Select @StockID=StockID,@Item_id=Item_id,@Transfer=Coalesce(Transfer,'N') From deleted

IF(@Transfer='Y')
BEGIN
    UPDATE Buy SET Transfer='N' WHERE StockID=@StockID AND Item_id=@Item_id
END