Thursday, November 2, 2017

How to save data temporarily before permanent saving in database?



Sometimes, we are asked to create a code in such a manner that we have to show the data to the user temporarily and after getting confirmation from user it get saved in the database table. 

To achieve this, we can use data table as a temporary storage to show data to the user. I am presenting logic here to save data temporarily. It is in C# language.

DataTable - DataTable is an in-memory representation of a single database table which has collection of rows and columns. To know more about data table, CLICK HERE.

The code is:
static void Main(string[] args)
        {
            string[] ar = { "John", "Shelly", "Kelly", "Steve" }; // Example Collection
            int i = 0; // we will use it for ID column value
            DataTable dt = new DataTable(); // New Instance of Data Table
            // Adding new columns
            dt.Columns.Add("Id");
            dt.Columns.Add("Name");
            dt.AcceptChanges(); // Save changes
            foreach (string it in ar) // Loop to save data
            {
                i++; // This variable is used to insert ID value with increment
                DataRow dtr = dt.NewRow();
                dtr["Id"] = i;
                dtr["Name"] = it;
                dt.Rows.Add(dtr); // Adding Data row to the data table
                dt.AcceptChanges(); // save changes
            }
            foreach (DataRow dr in dt.Rows) // Loop to display
            {
                Console.WriteLine("Id is {0} and {1}",dr["Id"],dr["Name"]);
            }


        }
Output is:




To delete any data row by indexing, we can use following code-

dt.Rows.RemoveAt(1); // here 1 is index number means second data
To delete data with condition –
for (int j = 0; j < dt.Rows.Count; j++)
           {
               DataRow dr = dt.Rows[j];
               if (Convert.ToInt32(dr["Id"]) == 3) // condition to check record having ID = 3
               {
                    dr.Delete(); // deleting record
               }
               dt.AcceptChanges();
           }
 

Happy Coding

My You Tube Channel – Click Here
Rudra Pratap Singh
Software Engineer

Friday, October 6, 2017

Write the code to find 1st , 2nd and 3rd highest element in an array



Programming Question -  Write the code to find 1st , 2nd and 3rd highest element in an array. You don’t have to use sorting function. Same values should be skipped, means if input is [3,3,2,5], output must be 5,3,2
Solution:
There are many logic to do it. I am trying following logic. I am using C# language syntax and keywords.
class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Enter number of elements of Array : "); // To take input from user
            int totNum = Convert.ToInt32(Console.ReadLine()); // Total number of elements in Array
            // int[] a = { 5, 5, 7, 1, 2,3 }; // use this code for fixed input
            int[] a = new int[totNum]; // creating array of input size
            Console.WriteLine("Input Array Elements : ");
            for (int j = 0; j < totNum; j++) // Loop to take input elements in the array
            {
                a[j] = Convert.ToInt32(Console.ReadLine());
            }
           
            int h1 = 0; // for first highest number
            int h2 = 0;// for second highest number
            int h3 = 0; // for third highest number
            for (int i = 0; i < a.Length; i++)
            {
                /*
                Inside this loop we are checking that if current value of array is greater than variable,
                we assign that value to the variable. In the same way, we do for all variables.
                */
                if (a[i]>=h1)
                {
                    if (a[i] == h1) // skip iteration if same value
                    {
                        continue;
                    }
                    h3 = h2;
                    h2 = h1;
                    h1 = a[i]; // Assigning First highest value. Always write this statement at last for no loss of assigned value
                }
                else if (a[i]>=h2)
                {
                    if (a[i] == h2)
                    {
                        continue;
                    }
                    h3 = h2;
                    h2 = a[i]; // Assigning Second highest value
                }
                else if (a[i]>=h3)
                {
                    if (a[i] == h3)
                    {
                        continue;
                    }
                    h3 = a[i]; // Assigning Third highest value
                }
            }
            Console.WriteLine("First highest:"+h1+", Second highest: "+h2+", Third highest: "+h3);
            Console.ReadLine();
        }
    }
Description – Please read comments for description.

I hope, it will help.

Happy Coding
Rudra Pratap Singh
Software Engineer

How to Get Organic Views on Your Channel/Content

 Hi Friends, This post will be very short and specific in the information.  If you are a content creator and want some help in getting organ...