How To send Form Stored Procedure custome error message in case of some condition in an C#.net Windows forms application

Sushil Agarwal 406 Reputation points
2022-08-22T07:57:25.85+00:00

i am trying to call a stored procedure to fill a table in winform application in vs2022. filling table either by sql data adapter or table execute does the job.

there are situations that the procedure depending on parameters and data in database has now rows to return and it reurns a empty table that is also ok

i iwanted to capture two situation if the table being returned has no rows, i will run another query there it self and see if 2nd query select also returens zero then there is no order acceptance records, but if send query t returns rows means order acceptance is there but its not authorised.

in ssms i see the throw error as i want it to be, how can i get that throw msg in c#.net i.e. calling program where in real life the table will be filled or proper error communication can be done in application

here is the query i am using
/****** Object: StoredProcedure [dbo].[GetoaItemTable_b] Script Date: 03/04/2012 10:19:23 ******/
/*
declare @po_number varchar(50)='',
@mainslui uniqueidentifier,
@item_ui uniqueidentifier,
@deliveryui varchar(50)='',
@po_over int=0,
@Doc _dt Date='20220818',
@Oa_Authorisation bit =1,
@amendment_no varchar(50)=''
select @mainslui = a.ui
from mainsl a
where a.gl_code=2 and a.sl_code = 54
select @item_ui = a.ui
from item a
where a.ItemGroupID=92 and a.id= 377

exec [dbo].[GetoaItemTable_c] @mainslui,@deliveryui,@item_ui,@doc_dt,@po_over,@amendment_no,@Oa_Authorisation,@po_number  
*/  
SET ANSI_NULLS ON  
GO  
SET QUOTED_IDENTIFIER ON  
GO  
if  OBJECT_ID('[dbo].[GetoaItemTable_c]', 'P') is not null  
    drop procedure [dbo].[GetoaItemTable_c]  
go    
create procedure [dbo].[GetoaItemTable_c]  
@mainslui uniqueidentifier,  
@deliveryui varchar(50),  
@item_ui uniqueidentifier,  
@doc_dt Date,  
@po_over int,  
@amendment_no varchar(50)='',  
@Oa_Authorisation bit =0,  
@po_number varchar(50)=''  
--,@Message nvarchar(50) output  
with Execute as caller  
as  
begin  
set nocount on ;  
--if @deliveryui is null  
--  begin  
--      set @deliveryui = @mainslui   
--  end  
set @deliveryui = isnull(@deliveryui,'')   
if @Oa_Authorisation = 0  
    begin  
        SELECT a.ui,a.doc_gl,a.doc_no,  
            rtrim(rtrim(a.po_no)+ ' ' + case when b.amendment_no is null then '' else convert(varchar(10),b.amendment_no) end) po_no,  
            b.begindt po_dt,a.lstcd,a.mainsl_ui,  
          convert(varchar,a.doc_gl)+'-'+ convert(varchar,a.doc_no) +'-'+ convert(varchar,b.srl) oakey,  
              a.CrDays,b.ui as oadtlui,b.srl,b.item_ui, b.rate, isnull(b.td_rt,0) td_rt,isnull(b.rateper,1) rateper,  
              isnull(b.packing_rt,0) packing_rt,isnull(b.TransportRate,0) TransportRate, b.po_over,b.ss plant,b.po_srno,  
              b.dl,isnull(b.qty,0) qty,isnull(b.qty,0)-isnull(b.sold_qty,0) BalanceQty,   
              b.begindt, b.enddt,d.name,  
              cast( d.itemgroupid as varchar(5)) + '-' +  
              cast( d.id as varchar(8))as itemno,d.uom,   
              b.buyer_part_no,  
              isnull(b.assessableAmtpp,0) assessableAmtpp ,b.hsn,b.amendment_no,f.tcscriteria_id  
        FROM oa a   
        join oaitem b on a.ui=b.oa_ui   
        join item d on b.item_ui=d.ui   
        join mainsl f on f.ui=a.mainsl_ui  
        where (@po_over=-1 or po_over= @po_over)  
            and  (@po_over=-1 or @doc_dt>=isnull(b.begindt,'20000101')   
                    and (@po_over=-1 or @doc_dt<=isnull(b.enddt,'99981231')))  
          and b.item_ui=@item_ui  
          and a.mainsl_ui=@mainslui  
          and isnull(convert(varchar(50),a.delivery_ui),'') = @deliveryui  
        and @po_number = case when @po_number = '' then @po_number else isnull(a.po_no,'') end  
          and @amendment_no = case when @amendment_no='' then @amendment_no else isnull(b.amendment_no,'') end  
        order by A.PO_DT desc  
    end  
    else  
    begin  
        SELECT a.ui,a.doc_gl,a.doc_no,  
            rtrim(rtrim(a.po_no)+ ' ' + case when b.amendment_no is null then '' else convert(varchar(10),b.amendment_no) end) po_no,  
            b.begindt po_dt,a.lstcd,a.mainsl_ui,  
          convert(varchar,a.doc_gl)+'-'+ convert(varchar,a.doc_no) +'-'+ convert(varchar,b.srl) oakey,  
              a.CrDays,b.ui as oadtlui,b.srl,b.item_ui, b.rate, isnull(b.td_rt,0) td_rt,isnull(b.rateper,1) rateper,  
              isnull(b.packing_rt,0) packing_rt, isnull(b.TransportRate,0) TransportRate,b.po_over,b.ss plant,b.po_srno,  
              b.dl,isnull(b.qty,0) qty,isnull(b.qty,0)-isnull(b.sold_qty,0) BalanceQty,   
              b.begindt, b.enddt,d.name,  
              cast( d.itemgroupid as varchar(5)) + '-' +  
              cast( d.id as varchar(8))as itemno,d.uom,   
              b.buyer_part_no ,f.tcscriteria_id,  
              isnull(b.assessableAmtpp,0) assessableAmtpp ,b.hsn,b.amendment_no  
        FROM oa a   
        join oaitem b on a.ui=b.oa_ui   
        join item d on b.item_ui=d.ui   
        join OaAuthorise e on e.authorise=1 and e.oaitem_ui=b.ui  
        join mainsl f on f.ui=a.mainsl_ui  
        where (@po_over=-1 or po_over= @po_over)  
            and  (@po_over=-1 or @doc_dt>=isnull(b.begindt,'20000101')   
                    and (@po_over=-1 or @doc_dt<=isnull(b.enddt,'99981231')))  
          and b.item_ui=@item_ui  
          and a.mainsl_ui=@mainslui  
          and isnull(convert(varchar(50),a.delivery_ui),'') = @deliveryui  
        and @po_number = case when @po_number = '' then @po_number else isnull(a.po_no,'') end  
          and @amendment_no = case when @amendment_no='' then @amendment_no else isnull(b.amendment_no,'') end  
        order by A.PO_DT desc  
        if @@ROWCOUNT = 0  
        begin  
            SELECT a.ui,a.doc_gl,a.doc_no,  
                rtrim(rtrim(a.po_no)+ ' ' + case when b.amendment_no is null then '' else convert(varchar(10),b.amendment_no) end) po_no,  
                b.begindt po_dt,a.lstcd,a.mainsl_ui,  
              convert(varchar,a.doc_gl)+'-'+ convert(varchar,a.doc_no) +'-'+ convert(varchar,b.srl) oakey,  
                  a.CrDays,b.ui as oadtlui,b.srl,b.item_ui, b.rate, isnull(b.td_rt,0) td_rt,isnull(b.rateper,1) rateper,  
                  isnull(b.packing_rt,0) packing_rt,isnull(b.TransportRate,0) TransportRate, b.po_over,b.ss plant,b.po_srno,  
                  b.dl,isnull(b.qty,0) qty,isnull(b.qty,0)-isnull(b.sold_qty,0) BalanceQty,   
                  b.begindt, b.enddt,d.name,  
                  cast( d.itemgroupid as varchar(5)) + '-' +  
                  cast( d.id as varchar(8))as itemno,d.uom,   
                  b.buyer_part_no,  
                  isnull(b.assessableAmtpp,0) assessableAmtpp ,b.hsn,b.amendment_no,f.tcscriteria_id  
            FROM oa a   
            join oaitem b on a.ui=b.oa_ui   
            join item d on b.item_ui=d.ui   
            join mainsl f on f.ui=a.mainsl_ui  
            where (@po_over=-1 or po_over= @po_over)  
                and  (@po_over=-1 or @doc_dt>=isnull(b.begindt,'20000101')   
                        and (@po_over=-1 or @doc_dt<=isnull(b.enddt,'99981231')))  
              and b.item_ui=@item_ui  
              and a.mainsl_ui=@mainslui  
              and isnull(convert(varchar(50),a.delivery_ui),'') = @deliveryui  
            and @po_number = case when @po_number = '' then @po_number else isnull(a.po_no,'') end  
              and @amendment_no = case when @amendment_no='' then @amendment_no else isnull(b.amendment_no,'') end  
            order by A.PO_DT desc  
            if @@ROWCOUNT = 0  
            begin  
                ;THROW 51000, 'P.O.Not Found', 1;  
            end  
            else  
            begin  
                ;THROW 51000, 'O.A.Not Authorised', 1;  
            end  
        end  
    end  
end  
Windows Forms
Windows Forms
A set of .NET Framework managed libraries for developing graphical user interfaces.
1,891 questions
{count} votes

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.