Click here to Skip to main content
15,885,782 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
How to bind data table with string array in asp.net?

My problem is this...
I have a table which columns either have some value or null.
I want those columns which have null to be set to "not applicable" and then bind to datalist

For that firstly select the record from the table,then count the number of rows,then apply two nested loop one for row and one for column,after that i want to save table values in array then check the value of array is null or not,if array value is null then replace by "not applicable"

i m doing that

C#
ad = new SqlDataAdapter("select * from posted_data where 1=1" + query, c.con);
                 dt = new DataTable();
                 ad.Fill(dt);

                 if (dt.Rows.Count > 0)
                 {
                     for (int i = 0; i <= dt.Rows.Count; i++)
                       {
                                    for (int j = 0; j <= dt.Columns.Count; j++)
                                    {
                                        string[] arr=new string[];
                                            //if (dt.Rows[i][j].ToString() == "")
                                            // {
                                            //     str = "NA";
                                            // }
                                            //else
                                            //{
                                            //str=dt.Rows[i][j].ToString();
                                            //}

                                            //DataList1.DataSource = str;
                                            //DataList1.DataBind();
                                   }

                      }
                }

is it right???




how can i do..??
plz help me

thnx in adance
Posted
Updated 2-Nov-11 23:45pm
v2
Comments
Dalek Dave 3-Nov-11 5:46am    
Edited for Spelling, Grammar and Syntax.

C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Collections;
using System.Data;


public partial class Arraysindatalist : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        //1 dimensional Array::::
        string[] array = { "Anil","Kumar","Anilkumar","Eresh","Archana" };
        Grid1D.DataSource = array;
        Grid1D.DataBind();
        //2 Dimensional array::::
        string[,] arra1 ={

                    { "John", "21" },

                    { "Smith", "33" },

                    { "Ryder", "15" },

                    { "Jake", "18"},

                    { "Tom","34" }

                 };
        ArrayList arraylist = new ArrayList();
        for (int i = 0; i < 5; i++)
        {
            arraylist.Add(new ListItem(arra1 [i, 0], arra1 [i, 1]));

        }
        DataList1.DataSource = arraylist;
        DataList1.DataBind();

        //Multi Dimensional Array:::::
        string[,] Multiarray = {

                    { "John", "21", "Berlin", "Germany" },

                    { "Smith", "33" ,"London", "UK"},

                    { "Ryder", "15" ,"Sydney", "Australia"},

                    { "Jake", "18", "Tokyo", "Japan"},

                    { "Tom","34" , "Mumbai", "India"}

                 };

        DataTable dt = new DataTable();
        dt.Columns.Add("Name", Type.GetType("System.String"));
        dt.Columns.Add("Age", Type.GetType("System.String"));
        dt.Columns.Add("City", Type.GetType("System.String"));
        dt.Columns.Add("Country", Type.GetType("System.String"));
        for (int i = 0; i < 5; i++)
        {
            dt.Rows.Add();
            dt.Rows[dt.Rows.Count - 1]["Name"] = Multiarray[i, 0];
            dt.Rows[dt.Rows.Count - 1]["Age"] = Multiarray[i, 1];
            dt.Rows[dt.Rows.Count - 1]["City"] = Multiarray[i, 2];
            dt.Rows[dt.Rows.Count - 1]["Country"] = Multiarray[i, 3];   
        }
       DataList1.DataSource = dt;
       DataList1.DataBind();


       
        
    }
}



Try this may be u will get.


Regards,

Anilkumar.D
 
Share this answer
 
v2
C#
string[,] multiarray = new string[dt.Rows.Count, dt.Columns.Count];

        for (int i = 0; i < dt.Rows.Count; i++)
        {
            for (int j = 0; j < dt.Columns.Count; j++)
            {
                multiarray[i, j] = dt.Rows[i][j].ToString();


            }

        }
        DataList1.DataSource = dt;
        DataList1.DataBind();


Hope this helps.

Regards,

Anilkumar.D
 
Share this answer
 
v2
Comments
soniya sangal 3-Nov-11 3:39am    
after this
multiarray[i, j] = dt.Rows[i][j].ToString();

i want to chk the value ether is null or not
if value is null thn null is replaced by "not applicabel" and thn bound array with datalist

how can do it??
thnx
Anil Honey 206 3-Nov-11 4:11am    
So you want to show Null values in your Datalist That means NotApplicable.
soniya sangal 3-Nov-11 4:48am    
after this
multiarray[i, j] = dt.Rows[i][j].ToString();
i want to chk array value ether is null or not if value is null thn null is replaced by "not applicabel" and thn bound array with datalist how can do it?? thnx
C#
string[,] multiarray = new string[dt.Rows.Count, dt.Columns.Count];

        for (int i = 0; i < dt.Rows.Count; i++)
        {
            for (int j = 0; j < dt.Columns.Count; j++)
            {
                if (dt.Rows.Count <= 0)
                {
                    Label1.Text = "NotApplicable.";
                }
                else
                    multiarray[i, j] = dt.Rows[i][j].ToString();

              

            }

        }

        DataList1.DataSource = dt;
        DataList1.DataBind();


CheckThis,It Works.


Regards,

Anilkumar.D
 
Share this answer
 
v2
C#
string[,] multiarray = new string[dt.Rows.Count, dt.Columns.Count];

        for (int i = 0; i < dt.Rows.Count; i++)
        {
            for (int j = 0; j < dt.Columns.Count; j++)
            {
                if (dt.Rows[i][j].ToString()==string.Empty)
                {
                    multiarray[i, j] = "NotApplicable.";
                }
                else
                    multiarray[i, j] = dt.Rows[i][j].ToString();
               

            }

        }
            DataList1.DataSource = dt;
            DataList1.DataBind();



Its Works
 
Share this answer
 
v2
Comments
soniya sangal 3-Nov-11 5:30am    
thanx

i wnt that all values of dt store in array thn chk array value and thn bind array not dt with datalist
Anil Honey 206 3-Nov-11 5:51am    
See If dataset having any Null Values That Value Should Replace with NotApplicable and then Bind to Datalist.That only i have done.Still you have any Problems with that code Replace with

if (dt.Rows[i][j].ToString()==" ")
{
multiarray[i, j] = "NotApplicable.";
}

It will work
P.Salini 3-Nov-11 5:35am    
Dont post as solution if you have any changes use inprove solution option
Anil Honey 206 3-Nov-11 5:49am    
Ok Next Onwards i will not Do That.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900