Posts

Showing posts with the label C#

Simple way to generate C# class from database table

 Use the below sql script to generate c# model corresponding to the table.   Set @TableName to the name of your table.   declare @TableName sysname = 'TableName' declare @Result varchar (max) = 'public class ' + @TableName + '{' select @Result = @Result + ' public ' + ColumnType + NullableSign + ' ' + ColumnName + ' { get; set; }' from ( select replace(col.name, ' ' , '_' ) ColumnName, column_id ColumnId, case typ.name when 'bigint' then 'long' when 'binary' then 'byte[]' when 'bit' then 'bool' when 'char' then 'string' when 'date' then 'DateTime' when 'datetime' then 'DateTime' when 'datetime2' then 'DateTime' when 'datetim

Nuget - Encryption using AES algorithm.

 A simple nuget for encrypting and decrypting data using AES algorithm in .NET and C#     Step 1:  Get the NuGet package https://www.nuget.org/packages/Common.Security.AES Set the property with a string Security.CypherPassWord   Security.CypherPassWord = "CypherPassWord";    Step 2: Just call the methode for  Encrypt var encriptByteArray = Encryption.Security.Encrypt(planetext); var encriptHexTest = Encryption.Security.EncryptAsHex(planetext);  Step 3: Just call the methode for  Decrypt    var planetext = Encryption.Security.Decrypt(encriptByteArray);  var planetext = Encryption.Security.DecryptFromHex(encriptHexTest);

Azure Functions – Time Trigger (CRON)

Expression Description runs at 0 * * * * * every minute 09:00:00; 09:01:00; 09:02:00; … 10:00:00 0 */5 * * * * every 5 minutes 09:00:00; 09:05:00 0 0 * * * * every hour (hourly) 09:00:00; 10:00:00; 11:00:00 0 0 */6 * * * every 6 hours 06:00:00; 12:00:00; 18:00:00; 00:00:00 0 0 8-18 * * * every hour between 8-18 08:00:00; 09:00:00; … 18:00:00; 08:00:00 0 0 0 * * * every day (daily) Mar 1, 2017 00:00:00; Mar 2, 2017 00:00:00 0 0 10 * * * every day at 10:00:00 Mar 1, 2017 10:00:00; Mar 2, 2017 10:00:00 0 0 * * * 1-5 every hour on workdays Mar 3 (FRI), 2017 22:00:00; Mar 3 (FRI), 2017 23:00:00; Mar 6 (MON), 2017 00:00:00 0 0 0 * * 0 every sunday (weekly) Mar 5 (SUN), 2017 00:00:00; Mar 12 (SUN), 2017 00:00:00 0 0 9 * * MON every monday at 09:00:00 Mar 6 (MON), 2017 09:00:00; Mar 13 (MON), 2017 09:00:00 0 0 0 1 * * every 1st of month (monthly) Mar 1, 2017 00:00:00; Apr 1, 2017 00:00:00; May 1, 2017 00:00:00 0 0 0 1 1 * every 1st of january (yearly) Jan 1, 2017 00:00:00; Jan 1, 2018 00:00:00

Xamarin Forms toaster notification for android and iOS

A simple solution is by using the Dependency Service you can easily get the Toast notification in both Android and iOS.   Create an interface in your Xamarin forms package. public interface IMessage { void LongAlert ( string message ) ; void ShortAlert ( string message ) ; }   Android Project   [ assembly: Xamarin.Forms.Dependency(typeof(MessageAndroid)) ] namespace Your.Namespace { public class MessageAndroid : IMessage { public void LongAlert ( string message ) { Toast.MakeText(Application.Context, message, ToastLength.Long).Show(); } public void ShortAlert ( string message ) { Toast.MakeText(Application.Context, message, ToastLength.Short).Show(); } } }    iOS Project   [ assembly: Xamarin.Forms.Dependency(typeof(MessageIOS)) ] namespace Your.Namespace { public class MessageIOS : IMessage { const double LONG_DELAY = 3.5 ; const double SH

Email Providers

Step 1: Get the NuGet  package    Click here... Step 2 :             string userName = "userName ";             string password = "password ";             var data = SmtpConfig.ForSendGrid(userName, password);             SmtpEmailProvider obj = new SmtpEmailProvider(data);             var email = new Email()             {                 ReceiverName = "ReceiverName ",                 ToAddress = "ToAddress @domain.com",                 SenderName = "SenderName",                 FromAddress = "FromAddress @domain.com",                 Message = "Message ",                 Subject = "Subject ",                 IsHtmlEmail = false              };             var re = obj.Send(email); There are many Smtp setting available. ForGoDaddy ForGmail ForOutlook ForMailGun ForSendGrid Happy Coding... :)

NuGet - Common Security

Step 1: Get the NuGet package  https://www.nuget.org/packages/Common.Security/ Set the property with a string Security.CypherPassWord Security.CypherPassWord = "CypherPassWord"; Call the static method for  Security.Encrypt()  syntax :       Security.Encrypt(planeText,password) Call the static method for  Security.Decrypt()  syntax :       Security.Decrypt(planeText,password)

Method inside a Method in c# 7.0

A new feature was introduced in c# 7.0, in which we can call a local methods to be defines and call within a method. This feature was not in the previous versions. Which is like, defining the call back function just above the method in jquery. The blow is as sample code static void Main(string[] args)   {     void Display(string str)     {       Console.WriteLine(str);   }        Display("Hello world....")     Console.ReadKey();   } 

Difference between var and dynamic in C#

var Introduced in C# 3.0 At compile time, the compiler decide the type of variable which is declared.  Need to initialize at the time of deceleration. Visual Studio shows intellisense as the type of variable assigned is known to the compiler. We cannot assign two data type in one variable since the compiler has already assign the data type to the value.       that is : var obj1=1; obj1="hello";    will through an error. dynamic Introduced in C# 3.0 At runtime time, the compiler decide the type of variable which is declared.  No need to initialize at the time of deceleration. Visual Studio intellisense is not available since the type of variable assigned is unknown to the compiler, which will be known only at the run time. We can assign two data type in one variable since the compiler recreate the type when another data type is assigned to the variable.

Use of Is and As operators in C#

The   is operator checks if an object can be cast to a specific type. if (someObject is StringBuilder) The as operator attempts to cast an object to a specific type, and returns null if it fails. StringBuilder b = someObject as StringBuilder;

Type Casting or Type Conversion Methods

Converting one type of data to another type it is also known as Type Casting.     Type Casting are of two types:           1) Implicit conversion                 int num = 2147483647;                 long bigNum = num;         2) Explicit conversion        double x = 1234.7;                int a;                // Cast double to int.                a = (int)x;

NuGet - Check Combo Box

Step 1:  Get the NuGet package https://www.nuget.org/packages/Common.CheckComboBox/ Step 2: Add the CheckComboBox Control in the toolbar. Click here Step 3: Drag the "CheckedComboBox" control from the toolbar to the form to the position where it should be placed   Step 4: Add items to the CheckedComboBox control by        for (int i = 0; i < 10; i++)             {                 checkedComboBox1.Items.Add(i);             }

How to get the list of properties of a class?

public List<string> GetPropertiesNameFromClass(object pObject) { List<string> propertyList = new List<string>(); if (pObject != null) { foreach (var prop in pObject.GetType().GetProperties()) { propertyList.Add(prop.Name); } } return propertyList; } public static Dictionary<string, object> GetPropertDictionaryFromClass(object pObject) { if (pObject == null) return new Dictionary<string, object>(); Type t = pObject.GetType(); PropertyInfo[] props = t.GetProperties(); Dictionary<string, object> dict = new Dictionary<string, object>(); foreach (PropertyInfo prp in props) { object value = prp.GetValue(pObject, new object[] { }); dict.Add(prp.Name, value); } return dic

Convert byte array to Image and Image to byte array

Convert Image to byte[] array: public byte [] imageToByteArray(System.Drawing.Image imageIn) { MemoryStream ms = new MemoryStream(); imageIn.Save(ms,System.Drawing.Imaging.ImageFormat.Gif); return ms.ToArray(); } Convert byte[] array to Image : public Image byteArrayToImage( byte [] byteArrayIn) { MemoryStream ms = new MemoryStream(byteArrayIn); Image returnImage = Image.FromStream(ms); return returnImage; }

Gesture Recognizers with Xamarin.Forms

Why gestures ?   Some controls does not have support click event. In order to achieve add the below code this will enabling click event in the controls.. Gestures are mainly of 3 types Tap Gesture Recognizer Pinch Gesture Recognizer Pan Gesture Recognizer 1) Tap Gesture Recognizer var tapGestureRecognizer = new TapGestureRecognizer(); tapGestureRecognizer.Tapped += tapGestureRecognizer_Tapped; Controler_Name.GestureRecognizers.Add(tapGestureRecognizer); void tapGestureRecognizer_Tapped(object sender, EventArgs e) { ........//Code here } 2) Pinch Gesture Recognizer var pinchGestureRecognizer = new PinchGestureRecognizer(); pinchGestureRecognizer.PinchUpdated += pinchGestureRecognizer_PinchUpdated; Controler_Name.GestureRecognizers.Add(pinchGestureRecognizer); void pinchGestureRecognizer_PinchUpdated(object sender, PinchGestureUpdatedEventArgs e) { ........//Code here } 3) Pan Gesture Recognizer var panG

Lambda Query

What is a Lambda Expression?             A lambda expression is an anonymous function. It allows to write a method in the same place you are going to use it.  syntax : Parameters => Executed code. Basic Lambda Expression 1)Where List< int> mark = new List< int> {90, 71, 82, 93, 75, 82 }; var result= mark.Where(n => n > 80); Console.WriteLine( "Values greater than 80 are" );  foreach(val item in result) {  Console.WriteLine( "{0}" , item );  } // Outputs:  // Value greater than 80 are // 90 // 82 // 93 // 82 2)Count() int result= mark.Where(n => n > 80).Count() ; Console.WriteLine( "{0} markare greater than 80" , result);   // Outputs:  // 4 mark are greater than 80   3)Sum()   int result= mark.Where(n => n > 80).Count() ; Console.WriteLine( "{0} markare greater than 80" , result);     // Outputs:  // Sum of mark greater than 80 are // 347

NuGet - Common Database Settings

NuGet Common Database Settings Step 1:  get the nuget package https://www.nuget.org/packages/CommonDatabaseSettings Step 2: Make a object for DataBase class. (you can see many Method ) Call the method named CheckDatabaseConnection . Note : you can get the connection sting by calling DataBase.ConnectionString That's all, select which type of database to be connected. More over there are many methods in the BataBase class. Function Description Return type GetConnectionString(string) Set the database connection string void CheckDatabaseConnection(string) Check the connection string existing or not bool CheckDataExist(string) Check the data is present in the table bool DropdownFill(string) Fill the a Dropdown DataTable ExecuteDB(string) Can insert, update and delete data in the database void GetData(string) Get a strin