Monday, December 23, 2019

Create JSON file and auto download in c#


 public void ConvertJson()
    {
        string _JsonFileName = string.Empty;

        GSTInvoiceMaster _GST = new GSTInvoiceMaster();
        List<GSTInvoiceDetails> _GSTDetailsList = new List<GSTInvoiceDetails>();
        if (intBKNInvID > 0)
        {
            DataTable dtInvoiceHeader = new DataTable();
            dtInvoiceHeader = objInterfaceHelper.GetGSTInvoiceHeader(intBKNInvID, 'H');
            if (dtInvoiceHeader.Rows.Count > 0)
            {
                _GST.Supplier_Legal_Name = dtInvoiceHeader.Rows[0]["GSTCompanyName"].ToString().Trim();
                _GST.Supplier_Trading_name = "Supplier_Trading_name";
                _GST.Supplier_Address1 = dtInvoiceHeader.Rows[0]["CompanyAddress"].ToString().Trim();
                _GST.Supplier_Address2 = "";
                _GST.Supplier_City = "";
                _GST.Supplier_State = dtInvoiceHeader.Rows[0]["StateName"].ToString().Trim();
                _GST.Supplier_GSTIN = dtInvoiceHeader.Rows[0]["GSTINNo"].ToString().Trim();
                _GST.Supplier_Pincode = "";
                _GST.Supplier_Email = "";
                _GST.Supplier_Phone = "";

                //--   File Name
                _JsonFileName = dtInvoiceHeader.Rows[0]["SIMPraInvNumber"].ToString().Trim();               
     

//--    Add Item Details -------------
                if (dtInvoiceDetails.Rows.Count > 0)
                {
                    for (int i = 0; i < dtInvoiceDetails.Rows.Count; i++)
                    {
                        GSTInvoiceDetails _GSTDetails = new GSTInvoiceDetails();

                        _GSTDetails.SLNO = dtInvoiceDetails.Rows[0]["Number"].ToString().Trim();
                        _GSTDetails.ItemDescription = dtInvoiceDetails.Rows[0]["ProductDesc"].ToString().Trim();
                        _GSTDetails.ISService = "";
                        _GSTDetails.HSNcode = dtInvoiceDetails.Rows[0]["SIMPraHSCode"].ToString().Trim();
                        _GSTDetails.Batch = "";
                        _GSTDetails.Barcode = "";   

                        _GSTDetailsList.Add(_GSTDetails);
                    }
                }
            }

            _GST.GSTInvoiceDetails = _GSTDetailsList;
            string output = JsonConvert.SerializeObject(_GST);
            GSTInvoiceMaster deserializedProduct = JsonConvert.DeserializeObject<GSTInvoiceMaster>(output);

            Response.Clear();
            Response.AddHeader("content-disposition", "attachment; filename='"+ _JsonFileName + "'.JSON");
            Response.AddHeader("content-type", "text/plain");

            using (StreamWriter writer = new StreamWriter(Response.OutputStream))
            {
                writer.WriteLine(output);
            }
            Response.End();
        }
    }


public class GSTInvoiceDetails
    {
        public string SLNO;
        public string ItemDescription;
        public string ISService;
        public string HSNcode;
        public string Batch;
        public string Barcode;
    }

    public class GSTInvoiceMaster
    {
        public string Supplier_Legal_Name;
        public string Supplier_Trading_name;
        public string Supplier_Address1;
        public string Supplier_Address2;
        public string Supplier_City;
        public string Supplier_State;
        public string Supplier_GSTIN;
        public string Supplier_Pincode;
        public string Supplier_Email;
        public string Supplier_Phone;
     
        public List<GSTInvoiceDetails> GSTInvoiceDetails;
    }

Find Data in Procedure with Out Paramer in Procedure

 Step (A) ||
------------
string OldPR = objDAHeaderFactory.GetDATOPRNumber(Convert.ToInt32(DAID));


 Step (B) ||
------------  Class DAHeaderFactory

  public string GetDATOPRNumber(int DANo)
        {
            return this._dataObject.GetDATOPRNumber(DANo);
        }


Step (C) ||
-----------  class DAHeaderDAL

public string GetDATOPRNumber(int DANo)
        {
            string PRNo = "";
            SqlCommand sqlCommand = new SqlCommand();
            sqlCommand.CommandText = "dbo.[GETPRNumberFromDA]";
            sqlCommand.CommandType = CommandType.StoredProcedure;
            sqlCommand.Connection = this.MainConnection;
            try
            {
                sqlCommand.Parameters.Add(new SqlParameter("@DANo", SqlDbType.Int, 10, ParameterDirection.Input, false, (byte)0, (byte)0, "", DataRowVersion.Proposed, (object)DANo));
                sqlCommand.Parameters.Add(new SqlParameter("@PRNo", SqlDbType.VarChar, 20, ParameterDirection.Output, false, (byte)0, (byte)0, "", DataRowVersion.Proposed, (object)PRNo));
                this.MainConnection.Open();
                sqlCommand.ExecuteNonQuery();
                PRNo = Convert.ToString(sqlCommand.Parameters["@PRNo"].Value);
                return PRNo;
            }
            catch (Exception ex)
            {
                throw new Exception("GETPRNumberFromDA::GETPRFROMDA::Error occured.", ex);
            }
            finally
            {
                this.MainConnection.Close();
                sqlCommand.Dispose();
            }
        }


Step (D) ||
-----------  Create Procedure

Create PROCEDURE [dbo].[GETPRNumberFromDA]
@DANo int,@PRNo varchar(20) output
AS

select @PRNo=SIMPraPRNumber from PRHeader where PRID=( select PRID from DADetails where DAID=@DANo)
select @PRNo as PRNo


  ----  Check Procedure
declare @PRNo varchar(20)
exec GETPRNumberFromDA 485996,@PRNo

select len('CSDS1917053901')

Create Procedure with values save in parameter and again select

alter proc PRTODA(@SimpraPRNumber nvarchar(20),@SimpraDANumber int)
as
begin

declare @DRID int =0,@PRID int
declare @SOHID int =0,@CustomerID int =0
declare @Quantity int =0
declare @Rate decimal(15,2)=0.0
declare @CustomerIDOld int =0
declare @QuantityOld int =0
declare @RateOld decimal(15,2)=0.0
select @DRID=DRID,@PRID=PRID from prheader where simpraprnumber='CSDS1917492101'
select @SOHID=SOHID from drheader where drid=@DRID
select @CustomerID=CustomerID from soheader where sohid=@SOHID
select @Quantity=SOQuantity,@Rate=BasicRate from soheaderproductdetails where sohid=56115 
 --select @Rate as Rate,@Quantity as Quantity,@CustomerID as CustomerId
--Old
select @RateOld=BasicRate,@QuantityOld=Quantity,@SOHID=SOHID from dadetails where daid=485996 
 select @CustomerIDOld=CustomerID from soheader where sohid=@SOHID   
 --select @RateOld as RateOld,@QuantityOld as QuantityOld,@CustomerIDOld as CustomerIdOld
If(@Rate=@RateOld and @CustomerId=@CustomerIdOld and @Quantity=@QuantityOld)
begin
              insert into DADetailsLog select  *,GETDATE(),'@DeleteBy'  from dadetails where DAID=@SimpraDANumber
              update dadetails set PRID=@PRID where DAID=@SimpraDANumber
End
else
select 'invalid'

end

Create JSON file and auto download in c#

 public void ConvertJson()     {         string _JsonFileName = string.Empty;         GSTInvoiceMaster _GST = new GSTInvoiceMaster(); ...