Determine first table in a data set - c#

I am trying to determine if i have the first table out of a dataset, however the problem i having by using this
if(ds.Tables[0].TableName == "tablename")
is that the dataset is dynamically built depending on the results from the database, for example an AC unit will have Fan Speed table however a boiler unit will not that will have a Temp table.
Currently i have
DataSet ds = new DataSet("AllUsageEquipment");
foreach (var chanID in allChanIDs)
{
DataTable dt = api.GetTotalEquipmentForAreaLevel(startDate, endDate, period, utility, AreaLevel, AreaLevelID, unitType, chanID);
dt.TableName = dt.Rows[0]["Quantity"].ToString();
ds.Tables.Add(dt);
}
DataTable dtExport = new DataTable();
dtExport.Columns.Add("DateStamp", typeof(string));
foreach (DataTable dt in ds.Tables)
{
dtExport.Columns.Add(dt.Rows[0]["Quantity"].ToString(), typeof(int));
foreach (DataRow dr in dt.Rows)
{
if(//code to determine first datatable)
}
}
As you can see the data set is made up dynamically, using the Quantity returned from the database ('Fan Speed' example)
So i am struggling to get the first table in the dataset.

Related

C# DataRow from DataTable not reading first value

I'm new to StackOverflow and I have a question here.
When I'm reading my Rows from a DataTable, the first time the result is empty (the app crash) and after if I'm in debug mode, I can see that the first row is read but its too late.
Here is the code.
MySqlDataAdapter da = new MySqlDataAdapter(query, conn);
DataSet ds = new DataSet();
da.Fill(ds);
foreach (DataTable table in ds.Tables)
{
foreach (DataRow myrow in table.Rows)
{
Number.Text = Convert.ToString(myrow[0]);
Text.Text = Convert.ToString(myrow[1]);
I changed the query, shoing the same thing but in a fifferent way and now its working.

Trying to get second row from DataTable

I'm trying to retrieve the second row from a DataTable but this is not returning a value. What could be the problem as the DropDown populates just fine.
using (SqlDataAdapter sda = new SqlDataAdapter(cmd))
{
DataTable dt = new DataTable();
sda.Fill(dt);
DropDown.DataSource = dt;
DropDown.DataTextField = "FirstName";
DropDown.DataValueField = "FirstName";
DropDown.DataBind();
con.Close();
var secondRow = dt.Rows[2].ToString(); // This should return the second row in my datatable.
}
DataTable.Rows[int] will give you the DataRow object. If you do dt.Rows[2].ToString(), you are going to get the string representation of the object type.
The index starts at 0. Hence for 2nd row, you will query dt.Rows[1]. Further, You can extract the value of a column in the row and for that, you have to mention the column index or name like - dt.Rows[1][0] or dt.Rows[1]["col1"]
You can also loop through all the columns in the row like below:
foreach (DataColumn col in dt.Columns)
{
var columnValue = dt.Rows[1][col];
}

How to display specific row of data based on a column value

From this code I am trying to only display data rows that contain the fields "DBY" in the Station_From_Code column and "MAT" in the Station_To_Column. This will then be printed onto a HTML page. These fields are definitely in the datatable but when I run this cod the table is empty. I am not used to working with data tables in C# so apologies for the poor coding.
dynamic schedluedContent = JsonConvert.DeserializeObject(scheduledJson);
JArray items2 = new JArray();
foreach (JObject stops in schedluedContent.stops)
{
DataTable dt = new DataTable();
DataRow dr;
dr = dt.NewRow();
dr["From_Station_Code"] = stops["station_code"];
dr["To_Station_Code"] = stops["station_code"];
dt.Rows.Add(dr);
DataRow[] result = dt.Select("From_Station_Code = 'DBY' AND To_Station_Code = 'MAT'");
dt.Rows.Add(result);
GridViewTrainTimes.DataSource = dt;
GridViewTrainTimes.DataBind();
}
It's a long time I have not seen this style of code for working with database tables! EntityFramework comes to change and surely ease the way of working with database and prevent different risks of using SQL commands inside the code.
If you use EF, your code will become something like bellow:
var rows = myDBContext.MyTable.Where(x=>x.From_Station_Code == "DBY" && x.To_Station_Code == "MAT");
GridViewTrainTimes.DataSource = rows;
You can use find match rows and add in new datatable, that contains only your match rows data then you can bind this dt to gridview.
DataTable dt = new Datatable(); // Lets this datatable have data;
Datatable dt2 = new Datatable(); // Copy all matching rows
foreach (DataRow dr in dt.Rows)
{
if (dr["From_Station_Code"].ToString() == "DBY" && dr["To_Station_Code"].ToString() == "MAT")
{
dt2.Rows.Add(dr);
}
}
GridViewTrainTimes.DataSource = dt;
GridViewTrainTimes.DataBind();

how to retrieve data table in the form of table into the listbox

i am creating an application of extracting excel in to data table which will display in to list ox. but problem is all the data comes row wise data into list box.
DataTable dt = new DataTable();
DataSet ds = new DataSet();
DataTable sheet = dbConn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);
OleDbCommand selectCmd = new OleDbCommand(String.Format("SELECT * FROM [{0}]", sheet.Rows[0]["TABLE_NAME"]), dbConn);
OleDbDataAdapter dbAdapter = new OleDbDataAdapter(selectCmd);
dbAdapter.Fill(dt);
foreach (DataRow row in dt.Rows)
{
foreach (DataColumn col in dt.Columns)
{
ListBox1.Items.Add(row[col].ToString());
}
}
Why donĀ“t you just use the DataGridView class? Then just can set the DataTable as data source directly.
dataGridView1.DataSource = dt;
For a listbox, you'll need to create a string representing each column. Which will be hard to align. And if you would want to modify the cell values it would be quite cumbersome.
foreach (DataRow row in dt.Rows)
{
var sb = new StringBuilder();
foreach (DataColumn col in dt.Columns)
{
sb.Append(row[col] + "\t");
}
ListBox1.Items.Add(sb.ToString());
}

C# Adding a Datatable to a Datatable

I know that sounds really weird, but I am fairly new to C# and I am hoping you guys can help me out.
I am looping thru a DataTable, then excute a qry and get the the result into a DataTable.
Works OK for one record, but as soon as there are more records, only the last record is in the DT, which makes sense, I just do not know how to fix it. I need to have all the records in the DT.
Here is my code, any suggestions are welcome....
DataTable dt = ml.getRegistration();
DataTable dt2 = new DataTable();
foreach (DataRow row in dt.Rows)
{
dt2 = ml.getRegistrationExport(row["ID"]);
}
GridView1.DataSource = dt2;
GridView1.DataBind();
If ml.getRegistrationExport is returning a datatable, then it overwrites the data as you loop through the first datatable.. You need to use dt2.merge to add all the data to the datatable. HTH.
DataTable dt = ml.getRegistration();
DataTable dt2 = new DataTable();
foreach (DataRow row in dt.Rows)
{
dt2.merge(ml.getRegistrationExport(row["ID"]));
}
GridView1.DataSource = dt2;
GridView1.DataBind();
One of the problems I see is each row you are looping through you are replace dt2. So if you have 5 rows, you will replace dt2 5 times and which ever row happen to be last would rule dt2.
I would re think this.
Are what you are trying to do is add a row to dt2 for each row in the original dt?
DataTable dt = ml.getRegistration();
DataTable dt2 = new DataTable();
foreach (DataRow row in dt.Rows)
{
DataRow newRow = dt2.NewRow();
// What Does getRegistrationExport return?
// A row or series of rows or a new DT?
// Each would change solution
newRow["SomeColumn"] = ml.getRegistrationExport(row["ID"]);
dt2.Rows.Add(newRow);
}
GridView1.DataSource = dt2;
GridView1.DataBind();

Categories

Resources