Rolando Puno

Rolando Puno

  • 1.5k
  • 218
  • 5.8k

How to apply this code to asynchronous way

Jul 15 2023 5:54 AM

1. validation

private void ValidateExistingHeaderCounter()
{
    try
    {
        if (this.CheckExistingHeaderCounter() == false && AddFlag.Text =="Add")
        {
           this.InsertTransferStockHeader();
            this.ValidateExistingRMDTLCounter();
        }
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.Message.ToString());
    }
}

2.  Data Access

public Boolean CheckRecordCouningHeader(string TransCode)
{
    string MemoCode;

    using (SqlConnection connection = DBCon.GetDbCon())
    {
        SqlCommand dCmd = new SqlCommand("CheckExistingISNORawMats", connection);
        dCmd.CommandType = CommandType.StoredProcedure;
        dCmd.Parameters.Add("@ISNO", SqlDbType.VarChar, 50).Value = TransCode;


        connection.Open();

        try
        {
            using (SqlDataReader reader = dCmd.ExecuteReader())
            {
                if (reader != null && reader.Read())
                {
                    if (reader.Read())
                    {
                        MemoCode = (String)System.Convert.ToString(reader["ISNo"]);

                        reader.Close();
                        reader.Dispose();
                    }
                    return true;
                }

                else
                {
                    if (reader != null)
                    {
                        reader.Close();
                        reader.Dispose();
                    }
                    return false;
                }
            }
        }

        catch (Exception ex)
        {
            throw ex;
        }
        finally
        {
            connection.Close();
            connection.Dispose();
            connection.Dispose();
        }
    }
}

3. Record Counting

private void rrRecordCountingRAWMATSIS()
{
    try
    {
        String Control = "RMIS-0000";
        int recCount = 0;

        RawMaterialsDA reccount = new RawMaterialsDA();
        recCount = reccount.AutoCountIssuanceSlip();

        recCount = recCount + 1;
        ISno.Text = Control + recCount;
    }

    catch (Exception ex)
    {

      MessageBox.Show(ex.Message.ToString());
    }
}

4. Inserting Reccord

private void InsertTransferStockHeader()
{
    try
    {
        int SupIdno;

        RawMaterialsDM supint = new RawMaterialsDM();
        RawMaterialsBR prd = new RawMaterialsBR();

        supint.ISno = Convert.ToString(ISno.Text);
        supint.ISDate = Convert.ToDateTime(ISDate.Text);
        supint.JobOrderNo = Convert.ToString(JobOrderNo.Text);

        supint.PRNo = Convert.ToString(PRNo.Text);
        supint.ChargeToID = Convert.ToString(ChargeToID.Text);
        supint.ChargeToName = Convert.ToString(ChargeToName.Text);

        supint.OriginID = Convert.ToString(OriginID.Text);
        supint.OringinName = Convert.ToString(OringinName.Text);
        supint.Particular = Convert.ToString(Particular.Text);

        supint.Checkedby = Convert.ToString(Checkedby.Text);
        supint.IssuedBy = Convert.ToString(Preparedby.Text);

        supint.TotalAmount = Convert.ToDecimal(TotalAmount.Text);
        supint.Status = Convert.ToString(Status.Text);
    
        SupIdno = prd.RamMatsIssuanceSlipBR(supint);

    }
    catch (Exception ex)
    {
        ex.Message.ToString();
    }
}

5. Business Roles

public int RamMatsIssuanceSlipBR(RawMaterialsDM supint)
{
    RawMaterialsDA mansr = new RawMaterialsDA();
    try
    {
        return mansr.InsertRawMaterialsDA(supint);
    }
    catch (Exception ex)
    {
        throw ex;
    }
    finally
    {
        supint = null;
    }
}

6. Adding Records

public int InsertRawMaterialsDA(RawMaterialsDM dtir)
{

    using (SqlConnection SqlCn = DBCon.GetDbCon())
    {
        SqlCommand dCmd = new SqlCommand("InsertRMIssuanceSlipHdr", SqlCn);
        dCmd.CommandType = CommandType.StoredProcedure;
        SqlCn.Open();

        try
        {

            dCmd.Parameters.AddWithValue("@ISno", Convert.ToString(dtir.ISno));
            dCmd.Parameters.AddWithValue("@ISDate", Convert.ToDateTime(dtir.ISDate));
            dCmd.Parameters.AddWithValue("@JobOrderNo", Convert.ToString(dtir.JobOrderNo));

            dCmd.Parameters.AddWithValue("@PRNo", Convert.ToString(dtir.PRNo));
            dCmd.Parameters.AddWithValue("@ChargeToID", Convert.ToString(dtir.ChargeToID));

            dCmd.Parameters.AddWithValue("@ChargeToName", Convert.ToString(dtir.ChargeToName));
            dCmd.Parameters.AddWithValue("@OriginID", Convert.ToString(dtir.OriginID));

            dCmd.Parameters.AddWithValue("@OringinName", Convert.ToString(dtir.OringinName));
            dCmd.Parameters.AddWithValue("@Particular", Convert.ToString(dtir.Particular));

            dCmd.Parameters.AddWithValue("@Checkedby", Convert.ToString(dtir.Checkedby));

            dCmd.Parameters.AddWithValue("@IssuedBy", Convert.ToString(dtir.IssuedBy));
            dCmd.Parameters.AddWithValue("@TotalAmount", Convert.ToDecimal(dtir.TotalAmount));
            dCmd.Parameters.AddWithValue("@Status", Convert.ToString(dtir.Status));
            return dCmd.ExecuteNonQuery();
        }
        catch (Exception ex)
        {
            throw ex;
        }
        finally
        {
            dCmd.Dispose();
            SqlCn.Close();
            SqlCn.Dispose();
        }
    }
}

My Sample SP

USE [HwlScm]
GO
/****** Object:  StoredProcedure [dbo].[CheckExistingISNORawMats]    Script Date: 7/15/2023 2:41:30 PM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER PROCEDURE  [dbo].[CheckExistingISNORawMats]
@ISNO    varchar(50)

AS
BEGIN
    SET NOCOUNT ON;

    DECLARE    @trancount               int
    SET @trancount = @@TRANCOUNT;
    BEGIN TRY
        IF @trancount =0
            BEGIN TRANSACTION

            SELECT ISNo from [dbo].[RMIssuanceSlipHdr]
            WHERE ISNo =@ISNO

            lbexit:
            IF @trancount = 0
            COMMIT
    END TRY

    BEGIN CATCH
    DECLARE @error int,
                    @message  varchar(4000),
                    @xstate int;

    SELECT
              @error = ERROR_NUMBER(),
              @message = ERROR_MESSAGE(),
              @xstate = XACT_STATE();

              IF @xstate = -1
                   ROLLBACK;
              IF @xstate = 1 AND @trancount = 0
                  ROLLBACK
              IF @xstate = 1 AND @trancount > 0
                  ROLLBACK TRANSACTION CheckExistingISNORawMats;

                  RAISERROR('CheckExistingISNORawMats : %d: %s', 32, 1, @error, @message);
  END CATCH
END

Database Con

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Configuration;

using System.Data;
using System.Data.SqlClient;

namespace Supply.Data.Access
{
   public  class DBCon
    {
        public static SqlConnection GetDbCon()
        {
            try
            {
                SqlConnection con = new SqlConnection();
                con.ConnectionString = System.Configuration.ConfigurationManager.ConnectionStrings["CiiDiErp"].ConnectionString;
                return con;
            }
            catch (Exception)
            {
                throw;
            }
        }
    }
}
<connectionStrings>
    <add name="CiiDiErp" connectionString="Data Source=;Initial Catalog=HwlScm;TrustServerCertificate=True;User ID=sa; Password=;Integrated Security=false"/>    
</connectionStrings>

Login

public static List<UserModel> DisplayCurrentUsers(string username, string password)
{
    
    using (SqlConnection SqlCn = dbCon.GetDbCon())
    {
        SqlCommand cmd = new SqlCommand("GetAvailableUser", SqlCn);
        cmd.CommandType = CommandType.StoredProcedure;

        cmd.Parameters.Add("@UserName", SqlDbType.VarChar, 50).Value = username;
        cmd.Parameters.Add("@Password", SqlDbType.VarChar, 50).Value = password;

        SqlCn.Open();

        List<UserModel> csd = new List<UserModel>();
        try
        {
            using (SqlDataReader reader = cmd.ExecuteReader())
            {
                while (reader.Read())
                {
                    UserModel grdcsd = new UserModel();

                    grdcsd.FullName = (String)System.Convert.ToString(reader["FullName"]);
                    grdcsd.DeptCode = (String)System.Convert.ToString(reader["DeptCode"]);
                    grdcsd.DepartmentNamr = (String)System.Convert.ToString(reader["Department"]);


                    csd.Add(grdcsd);
                }

                reader.Close();
                return csd;
            }
        }
        catch(Exception)
        {
            throw;
        }

        finally
        {
            SqlCn.Close();
        }
    }
}

Checking

private void PerformGetUserInfomation()
{
    string Usrname = Convert.ToString(txtUserName.Text);
    string PassWord = Convert.ToString(txtPassWord.Text);

    string eStr = SharedFunctionLib.Encrypt(PassWord);


    List<UserModel> mysr = new List<UserModel>();
    try
    {
        mysr = UserDA.DisplayCurrentUsers(Usrname, eStr);
        mysr.ToList();


        if (mysr.Count == 0)
        {

            result = MessageBox.Show("Username and password doesn't match in the database..." +
                                     "\n" +
                                     "\n" +
                                     "\nDo you want to continue this transaction?", "Error Message", MessageBoxButtons.YesNo, MessageBoxIcon.Information, MessageBoxDefaultButton.Button1);

            if (result == DialogResult.Yes)
            {
                txtUserName.Focus();
            }

            if (result == DialogResult.No)
            {
                this.Dispose();
            }

        }

        else
        {
            foreach (UserModel se in mysr)
            {
                currentUser = se.FullName.ToString();
                deptCode = se.DeptCode.ToString();
                departname = se.DepartmentNamr.ToString();

            }

            List<UserValidationModel> myvalid = new List<UserValidationModel>();

            myvalid = UserDA.DisplayUserValid(Usrname);
            myvalid.ToList();

            foreach (UserValidationModel se in myvalid)
            {
                if(se.DeptCode.Equals(deptCodes.Text))
                {
                    validaccess = true;
                }
                
            }
        }

        if (validaccess.Equals(true))
        {
            ErpMainMenuFrm mmf = new ErpMainMenuFrm(currentUser, deptCodes.Text, DeptNames.Text);
            mmf.ShowDialog();
            validaccess = false;

        }
        else
        {
            MessageBox.Show("Sorry! You don't have rights to access this Department..." +
                                     "\n" +
                                     "\n" +
                                     "\nPlease contact your system ERP...", "Error Message", MessageBoxButtons.OK, MessageBoxIcon.Information, MessageBoxDefaultButton.Button1);
        }
    }

    catch (Exception ex)
    {
     MessageBox.Show(ex.Message.ToString());
    }
}

 


Answers (3)