Read from Dataset - c#

I need to read value of the dataset.
This dataset return many rows. and each rows have single value (Customer_Name)

If you have one table in dataset and that table has one column you could do it like this:
foreach (DataRow row in dataSet.Tables[0].Rows)
{
string result = row[0].ToString();
}
Or:
foreach (DataRow row in dataSet.Tables["tablename"].Rows)
{
string result = row["columnname"].ToString();
}

Related

Display all strings from array in datatable

I have a DataTable that I get a row value from and it is formatted as follows: String databaseVal = "00010-12-D2324, K2343-13-D234234".
I am trying to manipulate the data and output each line separated by a comma to a DataTable. This is the code I have so far:
foreach (DataRow dr in finalResults.Rows)
{
DataTable dt = new DataTable();
string pin = this.rowValue("DB_Values", dr);
string[] splitValue = pin.Split(',');
List<string> list = splitValue.ToList();
foreach (string item in list)
{
dt.Rows.Add(item.Split(new char[] { '-' }, 3));
}
foreach (DataRow row in dt.Rows)
{
equip1 = row[0].ToString();
equip2 = row[1].ToString();
equip3 = row[2].ToString();
}
}
So the desired result would be that the table would have 2 rows with the following:
Row 1: equip1=00010, equip2=12, equip3=D2324
Row 2: equip1=K2343, equip2=13, equip3=D234234
Right now I just get the first row and not the second. Any help would be appreciated.

Checking value in column foreach row

The DataTable (dt) stores the retrieved values of carID's and makes so it stores like 3 rows within the DataTable, I also have another DataTable called dt2 which also stores carID's and makes, I am trying to loop through each row in the dt to see if any carID stored in the dt exists in any of the rows in dt2, here is what I have so far:
DataTable dt = w.getUserCars(userID);
foreach (DataRow dr in dt.Rows)
{
string carID = dr["carID"].ToString();
}
How do I do this?
You should be able to achieve this using DataTable.Select() method. You are on the right track. You just need to add the method to find the Row(s) in dt2.
DataTable dt = w.getUserCars(userID);
DataRow[] foundRows;
foreach (DataRow dr in dt.Rows)
{
string carID = dr["carID"].ToString();
foundRows = dt2.Select("carID = " + carID);
// do stuff here with foundRows
foreach (DataRow r in foundRows)
{
r.Delete();
}
}

How to Append Textbox value to Datatable column value?

Here my webpage contains uploadbutton and textbox. When I upload one Excel File it contains Name and Contacts it stores in Datatable after I enter any integer value in Textbox the textbox value will be append to all fields of contacts column in Datatable.
Here I am write some code but it shows actual DATAtable Data.textbox value will not append to it..
public void countrycode(DataTable dt,string colname,string str)
{
foreach (DataColumn dc in dt.Columns)
{
for (int i = 0; i < dt.Columns.Count; i++)
{
str += i;
}
dt.AcceptChanges();
}
}
If you have one column Contacts and you want to replace the value in that field of each row by adding str to the end you have to loop the rows not the columns:
foreach(DataRow row in dt.Rows)
{
row.SetField("Contacts", row.Field<string>("Contacts") + str);
}
If the column name is dynamic and you actually don't want to append but to prepend it as suggested by your comments use this code:
foreach(DataRow row in dt.Rows)
{
row.SetField(columnName, str + row.Field<string>(columnName));
}
If you can't use DataRowExtensions.Field you can also use the indexer:
foreach(DataRow row in dt.Rows)
{
row[columnName] = row.IsNull(columnName) ? str : str + row[columnName].ToString();
}

Set value for cell item based on column header and row in DataTable

I have a DataTable binding to a WPF datagrid where I've created columns and created a set number of rows, with the first column populated with default values in each row. It looks like this table:
So I have the Size column and all its values and the headers for each column. Now I need to input the data I collected into my DataTable but not using the indexes for columns and rows, I need to find the cell item using the header titles for row and column. Here's what I have:
heatMapTbl.Rows['2000000']['BOA'] = statVal;
Any suggestions?
Hint:
DataTable table = new DataTable();
var myColumn = table.Columns.Cast<DataColumn>().SingleOrDefault(col => col.ColumnName == "myColumnName");
if (myColumn != null)
{
// just some roww
var tableRow = table.AsEnumerable().First();
var myData = tableRow.Field<string>(myColumn);
// or if above does not work
myData = tableRow.Field<string>(table.Columns.IndexOf(myColumn));
}
I looped through the rows like this:
foreach (DataRow row in LiqTbl.Rows)
{
if (row.ItemArray[0].Equals(timezone))
{
foreach (DataColumn column in LiqTbl.Columns)
{
if (column.ColumnName.Equals(lp))
{
//Write the value in the corresponding row
row[column] = liq.ToString("N", CultureInfo.InvariantCulture);
}
}
}
}

Looping through a DataTable

Well. I have a DataTable with multiple columns and multiple rows.
I want to loop through the DataTable dynamically basically the output should look as follows excluding the braces :
Name (DataColumn)
Tom (DataRow)
Peter (DataRow)
Surname (DataColumn)
Smith (DataRow)
Brown (DataRow)
foreach (DataColumn col in rightsTable.Columns)
{
foreach (DataRow row in rightsTable.Rows)
{
//output
}
}
I typed that out and noticed this would not work. Can someone please advice on a better way of doing this?
foreach (DataColumn col in rightsTable.Columns)
{
foreach (DataRow row in rightsTable.Rows)
{
Console.WriteLine(row[col.ColumnName].ToString());
}
}
foreach (DataRow row in dt.Rows)
{
foreach (DataColumn col in dt.Columns)
Console.WriteLine(row[col]);
}
Please try the following code below:
//Here I am using a reader object to fetch data from database, along with sqlcommand onject (cmd).
//Once the data is loaded to the Datatable object (datatable) you can loop through it using the datatable.rows.count prop.
using (reader = cmd.ExecuteReader())
{
// Load the Data table object
dataTable.Load(reader);
if (dataTable.Rows.Count > 0)
{
DataColumn col = dataTable.Columns["YourColumnName"];
foreach (DataRow row in dataTable.Rows)
{
strJsonData = row[col].ToString();
}
}
}
If you want to change the contents of each and every cell in a datatable then we need to Create another Datatable and bind it as follows using "Import Row". If we don't create another table it will throw an Exception saying "Collection was Modified".
Consider the following code.
//New Datatable created which will have updated cells
DataTable dtUpdated = new DataTable();
//This gives similar schema to the new datatable
dtUpdated = dtReports.Clone();
foreach (DataRow row in dtReports.Rows)
{
for (int i = 0; i < dtReports.Columns.Count; i++)
{
string oldVal = row[i].ToString();
string newVal = "{"+oldVal;
row[i] = newVal;
}
dtUpdated.ImportRow(row);
}
This will have all the cells preceding with Paranthesis({)

Categories

Resources