public static string GetDigitalSignature(int signatureId)
        {
            string base64String = string.Empty;
            try
            {
                DigitalSignatureBE signature = null;

                using (SqlConnection conn = new SqlConnection(_connectionstring))
                {
                    conn.Open();

                    SqlCommand cmd = new SqlCommand("usp_DigitalSignature_Fetch", conn);
                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.Parameters.AddWithValue("SignatureID", signatureId);

                    using (SqlDataReader reader = cmd.ExecuteReader())
                    {
                        if (reader.Read())
                            signature = ToDigitalSignatureBE(reader);
                    }
                }

                if (signature != null)
                {
                    base64String = Convert.ToBase64String(signature.Blob, 0, signature.Blob.Length);
                    base64String = "data:image/png;base64," + base64String;
                }
            }
            catch (Exception e)
            {

            }
            return base64String;
        }
        private static DigitalSignatureBE ToDigitalSignatureBE(SqlDataReader rdr)
        {
            DigitalSignatureBE o = new DigitalSignatureBE();
            o.SignatureID = Convert.ToInt32(rdr["SignatureID"]);
            o.UserID = Convert.ToString(rdr["UserID"]);
            o.Blob = rdr["Blob"] != DBNull.Value ? (byte[])(rdr["Blob"]) : null;
            o.DigitalSignatoryTypeSurrogate = Convert.ToInt32(rdr["DigitalSignatoryTypeSurrogate"]);
            o.CreationDateTime = Convert.ToDateTime(rdr["CreationDateTime"]);
            o.LastUpdatedDate = Convert.ToDateTime(rdr["LastUpdatedDate"]);

            return o;
        }
        public static void UpdateDigitalSignature(DigitalSignatureBE o)
        {
            using (SqlConnection conn = new SqlConnection(_connectionstring))
            {
                conn.Open();

                SqlCommand cmd = new SqlCommand("usp_DigitalSignature_Update", conn);
                cmd.CommandType = CommandType.StoredProcedure;
                BusinessEntityHelper.ReplaceNullProperties<DigitalSignatureBE>(o);
                FromDigitalSignatureBE(ref cmd, o);
                cmd.Parameters.AddWithValue("LastUpdatedDate", DateTime.Now);
                cmd.ExecuteNonQuery();
            }
        }
 private static void FromDigitalSignatureBE(ref SqlCommand cmd, DigitalSignatureBE o)
 {
     cmd.Parameters.AddWithValue("SignatureID", o.SignatureID);
     cmd.Parameters.AddWithValue("UserID", o.UserID);
     cmd.Parameters.AddWithValue("CreationDateTime", o.CreationDateTime);
     cmd.Parameters.AddWithValue("DigitalSignatoryTypeSurrogate", o.DigitalSignatoryTypeSurrogate);
     if (o.Blob != null)
     {
         cmd.Parameters.AddWithValue("Blob", o.Blob);
     }
     else
     {
         cmd.Parameters.AddWithValue("Blob", DBNull.Value);
         cmd.Parameters["Blob"].SqlDbType = SqlDbType.Image;
     }
 }
        public static void AddDigitalSignature(DigitalSignatureBE o, out int createdSurrogate)
        {
            createdSurrogate = 0;
            using (SqlConnection conn = new SqlConnection(_connectionstring))
            {
                conn.Open();

                SqlCommand cmd = new SqlCommand("usp_DigitalSignature_Add", conn);
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.Add("Surrogate", SqlDbType.Int, 8).Direction = ParameterDirection.Output;
                BusinessEntityHelper.ReplaceNullProperties<DigitalSignatureBE>(o);
                FromDigitalSignatureBE(ref cmd, o);
                cmd.ExecuteNonQuery();
                if (cmd.Parameters["Surrogate"].Value != DBNull.Value)
                {
                    createdSurrogate = Convert.ToInt32(cmd.Parameters["Surrogate"].Value);
                }
            }
        }
        public static DigitalSignatureBE FetchDigitalSignature(int surrogate)
        {
            DigitalSignatureBE o = null;

            using (SqlConnection conn = new SqlConnection(_connectionstring))
            {
                conn.Open();

                SqlCommand cmd = new SqlCommand("usp_DigitalSignature_Fetch", conn);
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.AddWithValue("SignatureID", surrogate);

                using (SqlDataReader reader = cmd.ExecuteReader())
                {
                    if (reader.Read())
                        o = ToDigitalSignatureBE(reader);
                }
            }
            return o;
        }