Sunday, 19 March 2017

Convert binary string to decimal in c#


To convert a binary number to decimal is to look at the bits of the binary number and raise 2 to the power of the index of the “on” bits and add those together. I define an “on” bit as a bit that is 1 as opposed to 0.


For example, the binary number 100 can be looked at as
22 + 0 + 0 = 4
public static void Main()
        {
            var strbinary = "100"; // binary all way read from right to left
            Console.WriteLine(BitStringToInt(strbinary));
            Console.ReadLine();
        }


In the code example below, I first reverse the array to allow the index of our loop (Power) match up with the index of the binary string (the power in which we want to raise 2 to).

private static int BitStringToInt(string bits)
        {
            var reversedbinary = bits.Reverse().ToArray();
           // So we have to get max power of bit , that why we have reversed string value
            var num = 0;
            for (var power = 0; power < reversedbinary.Count(); power++)
            {
                var currentBit = reversedbinary[power];
                if (currentBit == '1')
                {
                    var currentNum = (int)Math.Pow(2, power);
                    num += currentNum;
                }
            }
            return num;
        }

Ouput => 4


Friday, 17 March 2017

How to call Stored Procedure in Entity Framework 6 (Code-First)


    I have the following classes:
     public class LoanProgramInfoes
    {
      //  [Key]
       // [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
        public int LoanProgramInfoId { get; set; }
        public int LoanProgramID { get; set; }
        public string LoanProgramCode { get; set; }
        public string LoanProgramName { get; set; }
     
    }

My context class is as in the following. In this class, I overrode the OnModelCreating method to map the Identity column with the LoanProgramInfoes entity.

 public class DbContextDemo:DbContext
    {
        public DbContextDemo()
            : base("PortalContext")
        {
        }
      
        public virtual DbSet<LoanProgramInfoes> LoanProgramInfoDbSet { get; set; }

        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            Database.SetInitializer<DbContext>(null);

              modelBuilder.Entity<LoanProgramInfoes>().HasKey(x => x.LoanProgramInfoId);
            modelBuilder.Entity<LoanProgramInfoes>()
                .Property(x => x.LoanProgramInfoId)
                .HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);

        }

    }

 Now i am calling SP using entity frame work 

 using (var context = new DbContextDemo())

            {
 using (var dbContextTransaction = context.Database.BeginTransaction())
                {
                    try

                    {
                     var searchdata = context.Database.SqlQuery<LoanProgramInfoes>(
                    "proc_GetLoanProgramInfo @LoanProgramInfoId,@LoanProgramID",
                    new SqlParameter("@LoanProgramInfoId", loanProgramInfoId),
                    new SqlParameter("@LoanProgramID", loanProgramId))
    .Select(x => new LoanProgramInfoes()
                        {
                        LoanProgramID = x.LoanProgramID,
                        LoanProgramInfoId = x.LoanProgramInfoId,
                        LoanProgramCode = x.LoanProgramCode,
                        LoanProgramName = x.LoanProgramName
                       

                   }).ToList();
                  foreach (var itemvalue in searchdata)
                        {
                                Console.WriteLine("LoanProgramID  {0}  of LoanProgramCode {1}  and its name -                                 {2}",  itemvalue.LoanProgramID, itemvalue.LoanProgramCode,                                                               itemvalue.LoanProgramName);

                        }
               dbContextTransaction.Commit();
             }
              catch (Exception ex)
                    {
                        Console.WriteLine(ex);
                        dbContextTransaction.Rollback();
                        

                    }
       }
}

Like that you can call for update and delete .
In case of select query no need to use Transaction, you can remove it.

Check if string contains only numbers c#

            string myString = "354";
         
            var Output= !string.IsNullOrEmpty(myString) && myString.All(char.IsDigit);

             Result => True

        Note=> If you put instated of ALL with Any in above line ,its will return true if                                                 myString contain at least one number . 

         **************** OR Try below******************

                int number;
                bool isNumeric = int.TryParse(myString , out number);
if(isNumeric)
{
// Do your TASK with int n
}

             **************** OR Try With Regex******************
              
                var regex = new Regex(@"^[0-9]+$");
if(regex.IsMatch(myString ))
{
return true;
                       //  Do your TASK

}