Convert DataTable to Array

July 18, 2011

Recently, I had an requirement to save DataTable to multi-dimension string array.I googled but could not find everything in one place, so this post is the result of it.

Here is some data we will use for this example

ID Name
1 aaa
2 bbb
3 ccc
4 ddd
5 eee

Below is the function we will use to convert DataTable to string[,] array

 public string[,] ConvertDataTableToArray(DataTable dt)


            //Declare 2D-String array

            string[,] arrString =

                new string[dt.Rows.Count + 1, dt.Columns.Count];

            int index = 0;

            //Save ColumnName in 1st row of the array

            foreach (DataColumn dc in dt.Columns)


                arrString[0, index] =




            //Reset Index

            index = 0;

            //Now save DataTable values in array,

            //here we start from second row as ColumnNames are stored in first row

            for (int row = 1; row < dt.Rows.Count + 1; row++)


                for (int col = 0; col < dt.Columns.Count; col++)


                    arrString[row, col] =

                        Convert.ToString(dt.Rows[row – 1][col]);



            //Return 2D-String Array

            return arrString;


So now we have 2D-String Array converted from DataTable, but how to read values from this array, I leave it on readers to find out.


To find total row and total columns in Multidimension Array we have to use .GetLength() method


Comments good as well as bad are most welcome.


Setting Value in Textbox with TextMode=”Password”

February 22, 2009


I had a requirement, that when user details are edited the admin should be able to edit the password.

First I should to display clear password (ofcourse it is worng) as that page was visible to admin only.

Then I had to change that and had to display asterik (‘*’) instead of clear password.

So Obvisiouly I did something like

18 this.txtPassword.Text = (string)dr[0];

But this did not work for me

So I googled and found this article

Setting Value of Password Field

so I just changed the above code to

18 this.txtPassword.Attributes.Add(“value”,(string)dr[0]);

and it worked liked a charm.

Note: Please do read this article, if you want to preserve the value for password field even when postback occurs.

That’s all folks.
Hope you enjoyed this post.

Comments Good as well as Bad are most welcome.

NJoy !!! 🙂