add items to an array using C#

Posted by Klbaiju under ASP.NET on 9/13/2013 | Points: 10 | Views : 951 | Status : [Member] | Replies : 2
Hi,

I want to add field from database to an array

like this

string[] names = { "Sachin", "Dhoni", "Yuvraj" };


I tried following code but showing error dataset contains names such as sachin,dhoni,yuvraj

for (int j = 0; j < ds.Tables[0].Rows.Count - 1; j++)
{
string[] names += { ds.Tables[0].Rows[j]["firstname"].ToString() };
}

i need array name names contains 3 names as above.

how to solve this

Regards

Baiju




Responses

Posted by: Bandi on: 9/13/2013 [Member] [MVP] Platinum | Points: 25

Up
0
Down
            List<string> strDetailIDList = new List<string>();    


foreach(DataRow row in ds.Tables[0].Rows)
{
strDetailIDList.Add(row["firstname"].ToString());
}

string[] strDetailID = strDetailIDList.ToArray();


Mark This Response as Answer
--
Chandu
http://www.dotnetfunda.com/images/dnfmvp.gif

Klbaiju, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Bandi on: 9/13/2013 [Member] [MVP] Platinum | Points: 25

Up
0
Down
Another sample for adding dataset items to array
using System;

using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;

namespace RadioButtonListEx
{
public partial class AddArrayItems : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
AddItems();
}

protected void AddItems()
{
string sConnectionString;
sConnectionString = "Password=XXXXX@1234;User ID=testLogin; Initial Catalog=XXXX; Data Source=XXXXXX";
SqlConnection objConn = new SqlConnection(sConnectionString);
objConn.Open();

SqlDataAdapter daAuthors = new SqlDataAdapter("Select * From STATE", objConn);
DataSet dsItems = new DataSet("Medchive");
daAuthors.Fill(dsItems);

List<string> strDetailIDList = new List<string>();

foreach(DataRow row in dsItems.Tables[0].Rows)
{
strDetailIDList.Add(row["STATE_NAME"].ToString());
}

string[] strDetailID = strDetailIDList.ToArray();

}

}
}


Mark This Response as Answer
--
Chandu
http://www.dotnetfunda.com/images/dnfmvp.gif

Klbaiju, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response