This website completely moved to new platform. For latest content, visit www.programmingposts.com

Search this Site

12 May 2017

Get Comma (delimiter) Separated String from DataTable Column Values in C#.Net

In this Programming Post, we will see an example program to get string of comma separated values from DataTable Column in C#.Net.

Generally while working with DataTable, we may need to generate a comma (or any delimiter) separated string and even we may need to generate comma separated values with single quotes to use the string in any sort of Data Filter or to pass it for sql query IN or NOT IN clause etc,. We will achieve this in the following program.

Program :
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
using System;
using System.Linq;
using System.Data;

namespace DataTableColumnCommaSeperated
{
    class Program
    {
        static void Main(string[] args)
        {
            //Printing Dictionary
            Console.WriteLine("\n*** www.programmingposts.com ***\n");
            Console.WriteLine("\n>>> C# Program to Get Comma Separated String from DataTable Column Values <<<\n");

            DataTable dt = getDepartments();

            //Generating string array from DataTable Column DeptId
            string[] strArr = dt.AsEnumerable().Select(r => Convert.ToString(r["DeptId"])).ToArray();

            //Generating CommaSeperated string from String Array
            string strCommaSeperated = string.Join(",", strArr);

            //Generating CommaSeperated string with single quoted values from String Array
            string strCommaSeperatedWithSingleQuotes = string.Join(",", strArr.Select(r => "'" + r + "'"));

            Console.WriteLine("Printing Comma Seperated String ");
            Console.WriteLine(strCommaSeperated);

            Console.WriteLine("Printing Comma Seperated String with single quoted values");
            Console.WriteLine(strCommaSeperatedWithSingleQuotes);

            Console.ReadLine();
        }

        /// <summary>
        /// returns sample DataTable
        /// </summary>
        /// <returns></returns>
        private static DataTable getDepartments()
        {
            DataTable dtDept = new DataTable();
            dtDept.Columns.Add("DeptId", typeof(Int32));
            dtDept.Columns.Add("DeptName", typeof(String));
            dtDept.Columns.Add("DeptEmpCount", typeof(Int32));
            DataRow dr;

            for (int i = 1; i <= 5; i++)
            {
                dr = dtDept.NewRow();
                dr["DeptId"] = i;
                dr["DeptName"] = "Dept" + i.ToString();
                dr["DeptEmpCount"] = (i * 10);
                dtDept.Rows.Add(dr);
            }
            return dtDept;
        }
    }
}

Output:


output text :
 *** www.programmingposts.com ***

 >>> C# Program to Get Comma Separated String from DataTable Column Values <<<

 Printing Comma Seperated String
 1,2,3,4,5
 Printing Comma Seperated String with single quoted values
 '1','2','3','4','5'

19 Nov 2016

C Program to count duplicate / repeated elements in an array

C PROGRAM TO FIND COUNT OF DUPLICATE ELEMENTS IN AN ARRAY


In this post we will see a simple C program to find the count of duplicate elements in an Array.
In below program we will get the count of elements that are duplicated i.e. elements that are occurred more than once in an array.

Example :

Suppose the elements of array are : 10,20,30,10,10

Here the element 10 is repeated 3 times in array, it means it is duplicated 2 more times and remaining elements occurred only once. Hence its output will be 2.

Program Algorithm :
Step 1: Read size of array and store it in n
Step 2: If n (i.e size of array) is greater than MAX, print message and exit program
Step 3: Read elements of array and store in arr[MAX] , here MAX is a global variable whose value
            is 25 in the program below
Step 4: Initialize count = 0, to avoid garbage values
Step 5: Set i = 0
Step 6: Set j = i+1
Step 7: Check for duplicate, if arr[i] = arr[j] then increment count by 1 i.e. count = count + 1
Step 8: Increment j by 1 i.e. j = j + 1 and repeat Step 7 till j<n
Step 9: Increment i by 1 i.e. i = i + 1 and repeat Step 6-7 till i<n
Step 10: Print the count and exit program

Program :

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#include<stdio.h>
#define MAX 25 //maximum size of array is 25

main()
{
  int i,j,n,count,arr[MAX];

  printf(" *** www.ProgrammingPosts.com ***");
  printf("\n >>> C program to count duplicate elements in an array <<<");
  
  //taking size of array as input from user
  printf("\n\n Enter the size of array: ");
  scanf("%d",&n); 
  
  //checking if n value greater than MAX size of array
  if(n > MAX) 
  {
     printf(" Max Array size allowed is %d",MAX);
     getch();
     return; //terminates program execution   
  }
  
  printf("\n Enter the %d elements of array: \n",n);
  
  //Reading the elements of array using for loop
  for(i=0;i<n;i++)
  {
    printf("\n arr[%d]:",i);
    scanf("%d",&arr[i]);    
  }
   
   //initializing the count to 0, to avoid garbage value
   count =0; 

   //Counting the duplicate elements in Array
  for(i=0; i<n; i++)
  {
     for(j=i+1; j<n; j++)
     {
       /* If duplicate found then increment count by 1 */
       if(arr[i]==arr[j])
       {
         count++;
         break;
       }
     }
   }
    
   printf("\n Count of duplicate elements in an array : %d\n",count);
   getch();
   return 0;
}

Sample output :


Sample output text :
 *** www.ProgrammingPosts.com ***
 >>> C program to count duplicate elements in an array <<<
 Enter the size of array: 5
 Enter the 5 elements of array:
 arr[0]:10
 arr[1]:20
 arr[2]:30
 arr[3]:10
 arr[4]:20
 Count of duplicate elements in an array : 2

7 Nov 2016

Get Financial Year of a given Date in C#.Net / VB.Net

In this post we will see, generating a Financial Year string based on the given date.
I have given code in C#.Net and VB.Net.

C#.Net Method to Get Financial Year of a given Date :


    /// <summary>
    /// Method to Get Financial Year of a given Date
    /// </summary>
    /// <param name="curDate"></param>
    /// <returns></returns>
    public string GetFinancialYear(DateTime curDate)
    {
        int CurrentYear = curDate.Year;
        int PreviousYear = (curDate.Year - 1);
        int NextYear = (curDate.Year + 1);

        string PreYear = PreviousYear.ToString();
        string NexYear = NextYear.ToString();
        string CurYear = CurrentYear.ToString();

        string FinYear = null;

        if (curDate.Month > 3)
        {
            FinYear = CurYear + "-" + NexYear;
        }
        else
        {
            FinYear = PreYear + "-" + CurYear;
        }
        return FinYear;
    }


VB.Net Method to Get Financial Year of a given Date :

    ''' <summary>
    ''' Method to Get Financial Year of a given Date
    ''' </summary>
    ''' <param name="curDate"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Function GetFinancialYear(ByVal curDate As DateTime) As String

        Dim CurrentYear As Integer = curDate.Year
        Dim PreviousYear As Integer = curDate.Year - 1
        Dim NextYear As Integer = curDate.Year + 1

        Dim PreYear As String = PreviousYear.ToString()
        Dim NexYear As String = NextYear.ToString()
        Dim CurYear As String = CurrentYear.ToString()

        Dim FinYear As String = Nothing

        If (curDate.Month > 3) Then
            FinYear = CurYear + "-" + NexYear
        Else
            FinYear = PreYear + "-" + CurYear

        End If
        Return FinYear

    End Function

23 Sept 2016

C# Program to count particular digit in a given number


> C# program to find the occurrence of a digit in a number
> C# program to find frequency of digit in a given number

In the previous posts, i have given C#.Net code to count the occurrences of a character in a string

This program is to count the occurrences of a digit in a given number.

I have written 3 different methods for the same thing.

8 May 2016

Check Uncheck All Checkboxes in Html Table Using JQuery

# Select deselect all checkboxes gridview using jquery
# Select deselect all checkboxes jquery
# Multiple checkbox Select/Deselect using jquery
# Check Uncheck all checkboxes in Gridview using jquery


In this blog post, we will see a small example to check or uncheck all checkboxes in a html table using jquery. You can implement the same code in Asp.net gridview.

To make this work, first you need to use jquery in your web page.

There are many articles on web providing code samples to check or uncheck all checkboxes.
But here we will also see how a header checkbox acts indeterminate when few of the checkboxes are checked/unchecked.