Click here to Skip to main content
15,896,334 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
How to pass dataset value into array . my code attached here in this code foreach (DataRow dr in table.rows) this line showing error"System.Data.DataSet' does not contain a definition for 'Rows' and no extension method 'Rows' accepting a first argument of type 'System.Data.DataSet' could be found (are you missing a using directive or an assembly reference?) "
C#
SqlCommand cmd = new SqlCommand("select price from  Dataupload",sc );
SqlDataAdapter da = new SqlDataAdapter();
da.SelectCommand = cmd;
DataSet ds = new DataSet();
da.Fill(ds);
string[] Gh = new string[ds.Tables[0].Rows.Count];

foreach (DataSet table in ds.Tables)
{
   int i = 0;

   foreach (DataRow dr in table.rows)
   {
       Gh[i] = dr["price"].ToString();
       i++;
   }
Posted
v4

C#
//try here eg:

//Empty array
string[] arrvalues = new string[ds.Tables[0].Rows.Count];

//loopcounter
for (int loopcounter = 0; loopcounter< ds.Tables[0].Rows.Count; loopcounter++)
{
    //assign dataset values to array
    arrvalues[loopcounter] = ds.Tables[0].Rows[loopcounter]["urcolumnName"].ToString();
}
 
Share this answer
 
v2
Comments
Unni R 27-Jun-15 0:03am    
SqlCommand cmd = new SqlCommand("select time,price from Dataupload",sc ); how to apply two dimensional array .
James Dean 8-Jun-20 12:16pm    
I have a question, i have this result from this code if(ds.Tables[0].Rows[g].ItemArray[0].ToString() == ds2.Tables[0].Rows[j].ItemArray[6].To String()). I want to convert this result ds2.Tables[0].Rows[j].ItemArray[6].To String()) into an array. Because I want them to put in Select SQL query using wildcard. I tried adding "%" it just gives me one row instead of three when I fired to ListView.
try this.........


/ Declaring Array
string[,] ArrayValues=new string[ DataTable.Rows.Count,datatable1.Columns.Count];


for(int row = 0; row < DataTable.Rows.Count; ++row)
{
       for(int col = 0; col < DataTable.Columns.Count; col++)
        {
                 // inserting Array values from DataTable
                ArrayValues[row, col] = DataTable.Rows[row][col].ToString();
        }
}
 
Share this answer
 
Um... look at the error message:
System.Data.DataSet' does not contain a definition for 'Rows'
And then look at your code:
C#
foreach (DataSet table in ds.Tables)
   ...
   foreach (DataRow dr in table.rows)
      ...
Even ignoring that C# is case sensitive, so "rows" is not the same as "Rows" why do you expect a collection of DataTables in a DataSet to return a DataSet rather than a DataTable?
Try:
C#
foreach (DataTable table in ds.Tables)
And correct the case.

And BTW: this is dangerous.
C#
string[] Gh = new string[ds.Tables[0].Rows.Count];
Since there is nothing which guarantees that each table in the DataSet has the same number of rows.
I'd recommend that instead of filling a DataSet you fill a single DataTable and only use one loop. It's a lot safer, and more obvious exactly what you are trying to do.
 
Share this answer
 
try ArrayList...
C#
ArrayList arrlst = new ArrayList();
   foreach (DataRow row in ds.Tables[0].Rows)
   {
       arrlst.Add(row);
   }
 
Share this answer
 

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