how to get data from a dynamic dataColumn in datatable mvc - asp.net-mvc-4

I have a dataTable in which some columns are added dynamically. Now i want to get a dynamic dataColumn data from the dataTable.
List<bool> EngColumnData = new List<bool>(ResultsGrid.Rows.Count);
foreach(DataRow row in ResultsGrid.Rows)
{
EngColumnData.Add((bool)row["EnglishAchieved"]);
}
The above code works if the column is actually present in the dataTable. I am not sure about the dynamic Column.
Please help me with this.Thanks in advance.

First you can get list of columns names have in dynamic data DataTabel , like below ,
foreach(DataColumn col in table.Columns) {
List.Add(col.ColumnName);
}
this will give you list of columns
Then after get data coresponding to that data coloumns like below ,
foreach(DataRow row in table.Rows) {
Console.WriteLine(row[List.First()]);
}
You can use this way to solve your problem.

Related

SSIS update a column with a result of an sql select

I was wondering if there's a way of updating a column inside a dataflow task by running a select on every row?
Here's the situation :
Let's take this as our start position. I collect info from 2 files, then I merge them, and I add a column with the derived column tool. Is there a way of populating this column by performing a select on every row using the values of the the row?
Ex :
SELECT Count(*) AS cnt
FROM TABLE T
WHERE T.COLUMN1 = ROW.COLUMN3
AND T.COLUMN2 = ROW.COLUMN5
I don't know if I'm just not phrasing my need properly but I couldn't get any results
Thank you
You should be able to do this with a Lookup Transformation.
EDIT based on comment:
If you don't want to use a lookup due to the size of the table, you can do exactly what you want with a Script Component. You can create and execute your SQL Command for each row of the dataflow just like you would in any .net application.
I was able to do it with a Script Component
1- I've removed the Derived Column
2- I've created a string variable where I stored the query whith a wildcard string to replace every value that I need to get from the row.
3- I've passed this variable allong with one containing the connecection string info to the Script Component
4- I've added a new column to the Output Columns of the Script Component
5- Added using System.Data.OleDb;
6- Created 2 variables :
string jourFerieQuery;
string dbcsoledbschema;
7- Updated the PostExecute() to put the values of my SSIS variables into the script variables :
public override void PostExecute()
{
base.PostExecute();
jourFerieQuery = Variables.jourFerieQuery;
dbcsoledbschema = Variables.dbcsoledbschema;
}
8- Added a method :
int GetData(string cs, string query)
{
OleDbConnection conn = new OleDbConnection(cs);
conn.Open();
OleDbCommand cmd = new OleDbCommand(query, conn);
DataTable dt = new DataTable();
dt.Load(cmd.ExecuteReader());
conn.Close();
return (int)dt.Rows[0][0];
}
9- Updated the Input0_ProcessInputRow(Input0Buffer Row) :
public override void Input0_ProcessInputRow(Input0Buffer Row)
{
string query = jourFerieQuery.Replace("[1]", Row.CODDEVI).Replace("[2]", Row.DATCRBEZEROCONGE.ToString());
Row.Keep = GetData(dbcsoledbschema, query);
}
My query returns a count that's why the method I've added returns an int
You can do that through a script component (transformation).
Add an output of Ct.
I use System.Data.OleDB as it matches the SSIS package connection string.

VB.Net DataTable.Select - Sort expression syntax

I need to select rows from a DataTable and sort them by "Field1 / Field2"
I tried this code:
Using DT_Tmp As DataTable = DT.Select("", "FirstNum/SecondNum desc").CopyToDataTable
but I get error saying that column "FirstNum/SecondNum" doesn't exist.
EDIT
So far I'm using (as a workaround) a temp table which I'm adding a Field where I store the ratio and I'm using that Field to sort the table.
I don't think the select method is smart enough to do calculations on the field. The other workaround is to add a computed column to your datatable and use that as the sort column
Try putting each column inside brackets
Using DT_Tmp As DataTable = DT.Select("", "[FirstNum] / [SecondNum] desc").CopyToDataTable
Based on the links you looked at, you need to add the caluculated column first.
csortnum= New DataColumn
With csortnum
.DataType = System.Type.GetType("System.Decimal")
.ColumnName = "sortcolumn"
.Expression = "FirstNum/SecondNum"
End With
DT.add(csortnum)
Using DT_Tmp As DataTable = DT.Select("", csortnum desc").CopyToDataTable

Retrieve the latest or the last value of a column in a dataset

The below code gives the first row.. but i need to get the latest or the last row updated. Please help
Dim dt As DateTime = ds.Tables(0).Rows(0)("Columnname")
You can use the Rows.Count property as shown in other answer or just let do that to Linq
Dim row = ds.Tables(0).AsEnumerable().Last()
Dim dt As DateTime = row.Field(Of DateTime)("ColumnName")
Of course this works for the last row of the table. This doesn't mean something like the last (more recent) value for the "ColumnName". If this is your intention then you need to "Sort" the datatable or better ask the source (a database ? ) of the rows to sort it.
If you are not able to change the data loading query to have it sorted directly from the database engine then you could reach (in code) the latest row ordered by "ColumnName" using something like this
' Create a dataview from the datatable, with no filter and ordered by ColumnName
Dim dv As DataView = New DataView(ds.Tables(0), "", "ColumnName ASC", DataViewRowState.CurrentRows)
dt = dv.Cast(Of DataRowView).Last().Row.Field(Of DateTime)("Column")
You have to use ds.Tables(x).Rows.Count-1
Dim dt As DateTime = ds.Tables(0).Rows(ds.Tables(0).Rows.Count - 1)("Columnname")

Datatable Sorting From A Custom Number Sequence

My datatable has a field that has values like so..
0000006685-001
0000006685-002
0000006713-001
0000006714-002
0000006713-002
0000006697-002
I want the datatable to be re-ordered by this field so the numbers on the left of the hyphen go in order, followed by the corresponding sequence in order. So the above example data would be ordered like..
0000006685-001
0000006685-002
0000006697-002
0000006713-001
0000006713-002
0000006714-002
Dim dv As New DataView(parsedDataset.Tables("Detail"))
dv.Sort = "the field"
Dim dt As DataTable = dv.ToTable
The easiest way is, most probably, through the Select method then the CopyToTable method
dt = dt.Select("", "ColumnName ASC").CopyToDataTable
Replace ColumnName with the name of the column for that field.

How to make this table query more efficient

I've added a dataset , and a table adapter to my C# project.(The dataset and tableadapter was added via DataSources box)
In this query , I get table data , then filter , then I will iterate and fill a listbox.
listBox1.Items.Clear();
ETPDataset.t_USR_UsersDataTable tbl = (new ETPDataset.t_USR_UsersDataTable());
ETPDatasetTableAdapters.t_USR_UsersTableAdapter tblAdap = new TestIntellisenseSql.ETPDatasetTableAdapters.t_USR_UsersTableAdapter();
tblAdap.Connection.ConnectionString = scon.ConnectionString;
tblAdap.Fill(tbl);
ETPDataset.t_USR_UsersRow[] rows2;
rows2 = (ETPDataset.t_USR_UsersRow[])tbl.Select("USR_RECORDID > 60");
foreach (ETPDataset.t_USR_UsersRow drow in rows2)
{
listBox1.Items.Add(drow.USR_UserID);
}
This is my problem
tblAdap.Fill(tbl);
because this line of code will fetch ALL data(problem for big tables) , how can i fix it ?
-Thanks
Create a new method on your adapter to use a where clause to filter the data at the database.