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] =

                    Convert.ToString(dc.ColumnName);

                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.

Hint:

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

NJoy!!!

Comments good as well as bad are most welcome.

Advertisements

Setting Value in Textbox with TextMode=”Password”

February 22, 2009

Recently,

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 !!! 🙂


Finding Elements Top and Left Using JavaScript

December 21, 2007

Recently,

I was trying to create orkut like interface, where user can click on image and upload files using IFrame, so it gave ajax like user experience.

For this I used javascript to find top, left, height and width of image so I could display iframe from center of image.

My javascript function was something like this

function ShowUploadFrame(myImage)

{

var frm = document.getElementById(‘ImageFrame’);
frm.src = “FileUpload.aspx”;

frm.style.display= “block”;

frm.style.top = parseInt(myImage.offsetTop) +
(parseInt(myImage.style.height)/2) + ‘px’;

frm.style.left = parseInt(myImage.offsetLeft) +
(parseInt(myImage.style.width)/2) + ‘px’;

}

I always use Mozilla Firefox(FF), so when I tested my code it worked perfectly.But, when I tested this code in Internet Explorer 6(IE6) the offsetTop did not work correctly.

I used Google my best friend to find the answer, and here is what I found

“When your element is nested inside other elements or when it has elements above it with position relative or absolute, element.offsetTop does not work properly in IE6.”

So, I found this code from different blogs on net

Now, my javascript function is as below

function ShowUploadFrame(myImage)

{

var frm = document.getElementById(‘ImageFrame’);
frm.src = “FileUpload.aspx”;
frm.style.display= “block”;

var dim = GetTopLeft(myImage);

frm.style.top = (parseInt(dim.Top) +
(parseInt(
myImage.style.height)/2)) + ‘px’;

frm.style.left = parseInt(dim.Left) +
(parseInt(myImage.style.width)/2) + ‘px’;

}

But the important function GetTopLeft(elm) is what you should have a look at, if you want to find Top and Left of element using Javascript.

function GetTopLeft(elm)

{

var x, y = 0;

//set x to elm’s offsetLeft
x = elm.offsetLeft;

//set y to elm’s offsetTop
y = elm.offsetTop;

//set elm to its offsetParent
elm = elm.offsetParent;

//use while loop to check if elm is null
// if not then add current elm’s offsetLeft to x
//offsetTop to y and set elm to its offsetParent

while(elm != null)
{

x = parseInt(x) + parseInt(elm.offsetLeft);
y = parseInt(y) + parseInt(elm.offsetTop);
elm = elm.offsetParent;
}

//here is interesting thing
//it return Object with two properties
//Top and Left

return {Top:y, Left: x};

}

Here are two important points I would like to share are

  1. I used while loop to find if elm has parent, then add the elements offsetLeft to x and offsetTop to y variable and set elm’s offsetParent to elm.
  2. I used return {Top:y, Left: x}; which returns an Object with two properties Top and Left whose values are set to y and x respectively.

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

Comments Good as well as Bad are most welcome.


Visual Studio 2008, .NET Framework 3.5 and Visual Web Developer 2008

November 20, 2007

Microsoft released Visual Studio 2008 officially (i.e. RTM) and Visual Web Developer 2008

For a starter, following is a list of new features included in Visual Studio 2008 as well as Visual Web Developer 2008

  1. Built in AJAX Functionality: Now AJAX is not an external framework but an internal part of ASP.NET 3.5.
  2. JavaScript Support: These new IDE(s) fully supports JavaScript Debugging and Javascript Intellisense.
  3. LINQ (Language Integrated Query): With LINQ Support data access becomes easier.
  4. Debugging .NET Framework: As .NET Framework Source Code is freely available (courtesy Microsoft), VS2008 allows you to debug .NET Framework Source Code.
  5. Split View: Now with new split view feature, you can work with “Source View” as well as “Design View” at the same time.
  6. Multi-Targeting: With these new IDE(s), you can develop web application for .NET Framework 2.0(Visual Studio 2005) as well as .NET Framework 3.5.

These above feature are only for starter, there are many more new features included in Visual Studio 2008 as well as .NET Framework 3.5.

Download

As Visual Studio 2008 has reached RTM, it will not be available for free download.

But you can download Visual Web Developer 2008 (of course it is free…) from
here http://www.microsoft.com/express/vwd/

For getting started with ASP.NET 3.5, here is a useful link
Learn ASP.NET 3.5

Hope this helps !!!!

If you have any suggestion, question or problem feel free to comment below.


Tutorial regarding asp.net IDE

August 2, 2007

These are tutorials related to ASP.NET IDE


Second Post

August 2, 2007

This is my second Post


Introduction

May 13, 2007

This blog is created by me for people like me who have just started learning ASP.NET 2.0

The Coding Practice and Conventions are my own, though I have tried to follow the Coding Practice and Conventions followed by Professional Programmers, from their books and blogs.

If you find this blog useful or if you have any comment please write it below

ASP.NET 2.0 Tutorial and Code Snippets coming soon

View My Second Post Second Post