datatable select rows - c#

I am using a datatable created by program. In this datatable i want to insert values in some specified columns.
Initially I am inserting primary key values leaving remaining columns null, when I am querying datatable with recently inserted value in Primary column to update same row, I am facing error Missing operand after ID operator
Can any one tell me the exact issue.
I am trying following code:
dt.Rows.Add(1);
int insertedValue = 1;
DataRow[] dr = dt.Select("ID = '" + insertedValue.toString() + "'");
And the table structure after entring primary value is as follows.
ID Volumn1 Volumn2 volumn3
--------------------------------------
1

You can do this more cleanly with LINQ and make this a strongly typed operation.
Something like:
dt.Rows.Add(1);
int insertedValue = 1;
var result =
dt.AsEnumerable().Where( dr => dr.Field<int>( "ID" ) == insertedValue );
Working example:
DataTable dt = new DataTable();
dt.Columns.Add( "ID", typeof( int ) );
dt.Rows.Add( 1 );
var result = dt.AsEnumerable().Where( dr => dr.Field<int>( "ID" ) == 1 );

You can simply format the selection string as shown below:
DataRow[] dr = dt.Select(string.Format("ID ='{0}' ", insertedValue));
Feel free to let me know if this works for you.. Thanks

You do not need ' ' in your filter.
I think this should work:
DataRow[] dr = dt.Select("ID = " + insertedValue.toString());

By the way, reference System.Data.DataSetExtensions

If you are looking for a specific row and your datatable has a primary key you could use the Find method and target the primary key which would return just the row you want rather than an array:
DataRow foundRow = dt.Rows.Find([INSERT SEARCH PARAMETER HERE]);
if(foundRow != null)
{
TO SET A STRING EQUAL TO A FOUND VALUE:
string str = foundRow["COLUMN NAME / INDEX];
OR IF YOU ARE INSERTING A VALUE YOU CAN USE IT LIKE THIS:
foundRow["COLUMN NAME / INDEX"] = NEW VALUE;
}

select column of row
dt.Rows[0].Field<string>("MyColumnName")

Related

Check if row exists in DataTable?

I have a datatable and a row. I want to import the row to the datatable only if it does not exist in the datatable.
How can i do that?
If you use a typed DataSet, I.e. declared in design time, the "linq Contains method" takes a typed DataRow. The default IEqualityComparer will compare all values in the DataRow. (Which is normally useless, since you should have a key defined).
DataSet1 ds = new DataSet1();
DataSet1.DataTable1Row row = ds.DataTable1.AddDataTable1Row(bla, bla);
bool exists = ds.DataTable1.Contains(row);
You can use LINQ to check if row is present in datatable. Follow this solution, and replace "id" with your row's primary key, by which you can uniquely identify a row in a table.
DataRow dr = null; // assign your DR here
DataTable dt = new DataTable(); // assign Datatable instance here.
var k = (from r in dt.Rows.OfType<DataRow>() where r["id"].ToString() == dr["id"].ToString() select r).FirstOrDefault();
if(k != null)
{ // Row is present }
if you want to check all the cells in a DataRow, you can try this function:
bool ContainDataRowInDataTable(DataTable T,DataRow R)
{
foreach (DataRow item in T.Rows)
{
if (Enumerable.SequenceEqual(item.ItemArray, R.ItemArray))
return true;
}
return false;
}
you can use Contains as below
if(DataTable.Columns.Contains("RowName"))
{
//Do some stuffs here
}
Tried all answers here but did not work, so I made something for myself which works in my case. The code is pretty simple, it checks if the row you want to add already exists in the datatable - if it does not exist in the datatable, add it.
// fill dt with information
DataTable dt = new DataTable();
// create a new row and fill it with information
DataRow dr = dt.NewRow();
// distinct
bool isDistinct = true;
for (int i=0; i < dt.Rows.Count; i++)
{
// check if both rows are equal
if (Enumerable.SequenceEqual(dt.Rows[i].ItemArray, dr.ItemArray))
{
// it already exists
isDistinct = false;
break;
}
}
if (isDistinct)
{
dt.Rows.Add(dr);
}
if ( Datatable1.Rows[NumOfRow].ToString().Deleted == "Deleted")
You should check row existence by comparing primary keys:
static bool RowExists(DataTable table, DataRow row)
{
var pk = table.PrimaryKey
.Select(column => row[column, DataRowVersion.Original])
.ToArray();
return table.Rows.Contains(pk);
}
Reason is, DataRow that you are trying to check against existing DataTable is, in real-life scenarios, different class instance compared to the DataRaw in the table, even when same DataRaw already exists in the DataTable. Usual .NET equality-comparison does not work properly in this scenarios. That includes DataTable.Contains(...) method.
To properly check for DataRaw existence in the table, primary key given DataRaw should be searched for in the table.
You can check using any with the key value
If (value.Tables(0).AsEnumerable().Any(Function(x) key = x.Field(Of Integer)("ProductId") ))

The name 'colType' does not exist in the current context

I am attempting to loop through a dataset's rows and columns in search for a match between the dataset's name column -- and the ColumnName from a DataReader object.
I have a new table called RECORDS which is empty at program startup. I also have a pre-populated table called ColumnPositions with a sub-set of column names found in the RECORDS table. This routine is intended to show a subset of all the available columns -- as a default display style.
My code works...except for the line of code that gets the dr["type"] value. I get the error:
The name 'colType' does not exist in the current context.
As you can clearly see, my string variables are declared outside the WHILE and FOREACH loops. The line statement colName = works just fine. But colType fails everytime. If I do a statement check in the Intermediate Window in VS2010 for ? dr["type"]" I get the result integer. But when I check ? colType, I get the above noted error message.
The intellisense for the DataRow object dr reveals an array of 6 items. Index 1 in the array maps to name. Index 2 maps to type. When I check the value of ? dr[2] in the Intermediate Window, the same result comes back integer. This is correct. But whenever this value is assigned to colType, VS2010 complains.
I'm no newbie to C# so I did a lot of testing and Googling before posting here. I'm hoping that this is a matter of me not seeing the forest through the trees.
Here's my code:
// get table information for RECORDS
SQLiteCommand tableInfo = new SQLiteCommand("PRAGMA table_info(Records)", m_cnCaseFile);
SQLiteDataAdapter adapter = new SQLiteDataAdapter(tableInfo);
DataSet ds = new DataSet();
adapter.Fill(ds);
DataTable dt = ds.Tables[0];
SQLiteCommand cmd = new SQLiteCommand("SELECT * FROM ColumnPositions WHERE ColumnStyle_ID = " + styleID + " ORDER BY ColumnPosition_ID ASC", m_cnCaseFile);
SQLiteDataReader colReader = cmd.ExecuteReader();
string colName = "";
string colType = "";
if (dt != null && colReader.HasRows)
{
while (colReader.Read())
{
foreach(DataRow dr in dt.Rows)
{
colType = Convert.ToString(dr["type"]);
colName = dr["name"].ToString();
if (colReader["ColumnName"].ToString() == colName)
{
DataGridViewColumn dgvCol = new DataGridViewColumn();
}
}
}
}
dt.Dispose();
colReader.Close();
Instead of using "dr["name"].ToString();", it is better to use "Convert.ToString(dr["name"]);"
Try using the array position instead of the column name:
colType = Convert.ToString(dr[1]);
and
colName = dr[0].ToString();
You probably don't need this, but here is the documentation for values returned by the SQLite PRAGMA table_info() command. LINK

UNIQUE constraint on DataTable

Can I have a clustered key on DataTable in C# ?
There's a requirement in my code to have constraints for possible combinations of 3 columns to be unique .....
What you need is really a unique constraint on your DataTable. Clustered keys are a SQL Server on-disk feature and not applicable to a DataTable.
Check out MSDN doc on DataTable constraints:
The UniqueConstraint object, which can be assigned either to a single column or to an array of columns in a DataTable, ensures that all data in the specified column or columns is unique per row. You can create a unique constraint for a column or array of columns by using the UniqueConstraint constructor.
So try something like this:
// this is your DataTable
DataTable custTable ;
// create a UniqueConstraint instance and set its columns that should make up
// that uniqueness constraint - in your case, that would be a set of *three*
// columns, obviously! Adapt to your needs!
UniqueConstraint custUnique =
new UniqueConstraint(new DataColumn[] { custTable.Columns["CustomerID"],
custTable.Columns["CompanyName"] });
// add unique constraint to the list of constraints for your DataTable
custTable.Constraints.Add(custUnique);
And that should do the trick for you!
To make your columns enforce a UNIQUE constraint you could use
DataTable dt = new DataTable();
dt.Columns.Add("UniqueColumn");
dt.Columns["UniqueColumn"].Unique = true;
Solution two
If you want some combination of the values in some columns to have unique value, you can try this.
DataTable dt = new DataTable();
dt.Columns.Add("UniqueColumn1");
dt.Columns.Add("UniqueColumn2");
dt.Columns.Add("UniqueColumn3");
dt.Columns.Add("NormalColumn");
string
value1 = string.Empty,
value2 = string.Empty,
value3 = string.Empty,
value4 = string.Empty;
//Logic to take values in string values variables goes here
DataRow[] founded = dt.Select("UniqueColumn1 = '"+ value1+
"' and UniqueColumn2 = '"+value2+
"' and UniqueColumn3 = '"+value3+"'");
if (founded.Length > 0)
// Message to say values already exist.
else
// Add a new row to your dt.
In this code you check the data present in DT to enforce uniqueness

Returning rows from a specific column name from a DataTable - C#

I've been trying to find an answer online but I'm working with a DataTable and I want to filter all rows based on the column name in the table but I can't seem to get the filterExpression to work. Even though the syntax is wrong, this is basically what I want it to do....
DataRow[] row = sqlDT.Select(ColumnName = "Foo", "ASC", DataViewRowState.CurrentRows);
Thanks so much.
There are a few ways to accomplish this. I would suggest using LINQ to filter the rows:
sqlDT = sqlDT.AsEnumerable().Where(r => r.Field<string>("ColumnName") == "Foo").CopyToDataTable();
You can also use the Select method or the DefaultView.RowFilter property:
//select method
sqlDT = sqlDT.Select("ColumnName = 'Foo'").CopyToDataTable();
//row filter property
sqlDT.DefaultView.RowFilter = "ColumnName = 'Foo'";
sqlDT = sqlDT.DefaultView.ToTable();
EDIT
If you just want to filter out unneeded columns, use the DefaultView.ToTable() method:
sqlDT = sqlDT.DefaultView.ToTable(false, "Column1", "Column2", "Column3");
You are doing it in the wrong order,simply do it like this:
Datatable.rows(0//its count).item("column name");
You could loop it then with foreach and make a list
you should try something like this
DataRow[] row = sqlDT.Select("ColumnName =' " + Foo + " ' " , "ASC", DataViewRowState.CurrentRows);
I hope this work for you.

Syntax error in aggregate argument: Expecting a single column argument with possible 'Child' qualifier

DataTable distinctTable = dTable.DefaultView.ToTable(true,"ITEM_NO","ITEM_STOCK");
DataTable dtSummerized = new DataTable("SummerizedResult");
dtSummerized.Columns.Add("ITEM_NO",typeof(string));
dtSummerized.Columns.Add("ITEM_STOCK",typeof(double));
int count=0;
foreach(DataRow dRow in distinctTable.Rows)
{
count++;
//string itemNo = Convert.ToString(dRow[0]);
double TotalItem = Convert.ToDouble(dRow[1]);
string TotalStock = dTable.Compute("sum(" + TotalItem + ")", "ITEM_NO=" + dRow["ITEM_NO"].ToString()).ToString();
dtSummerized.Rows.Add(count,dRow["ITEM_NO"],TotalStock);
}
Error Message: Syntax error in aggregate argument: Expecting a single column argument with possible 'Child' qualifier.
Do anyone can help me out?
Thanks.
You might try this:
dTable.Compute("sum([" + TotalItem + "])","");
I.e enclose your column name in square brackets [ ]
The idea is from this post.
The problem is exactly about your DataType of the column. If you have a row with dynamically added columns without DataType like that (it may be a result of a manual calculation or cross-tab query-like)
myTable.Columns.Add("AddedColumn");
You will probably face with the column conversion issue.
Instead, If you change your add method with pointing DataType like below
myTable.Columns.Add("AddedColumn", typeof(System.Int32));
It will work I think. It's what I experienced & fixed before...
You want to write:
dTable.Compute("sum(CONVERT(ITEM_STOCK, 'System.Double'))",
"ITEM_NO='" + dRow["ITEM_NO"].ToString() + "'")
instead of:
dTable.Compute("sum(" + TotalItem + ")", "ITEM_NO="...
because it will translate to dTable.Compute("sum(value_of_TotalItem), "ITEM_NO="..., value_of_TotalItem is a double and is not a column name.
See DataTable.Compute
UPDATE:
try this:
DataTable distinctTable = dTable.Clone();
dTable.Columns.Add("ITEM_STOCK_D", typeof(Decimal),
"CONVERT(ITEM_STOCK, 'System.Decimal')");
foreach (DataRow dRow in dTable.Rows)
{
String itemNo = dRow["ITEM_NO"].ToString();
if(distinctTable.Select(String.Format("ITEM_NO = '{0}'",itemNo)).Length == 0)
{
double totalStock = Convert.ToDouble(dTable.Compute("SUM(ITEM_STOCK_D)",
String.Format("ITEM_NO = '{0}'", itemNo)));
distinctTable.Rows.Add(itemNo, totalStock.ToString());
}
}
dTable.Columns.Remove("ITEM_STOCK_D");
In my case the issue was with my query itself.
My query returned difference of two columns. Like, query="Select A,B,A-B from Table" and I was performing sum on datatable using compute function as dt.Compute("Sum(A-B)","").
So, datatable was unable to compute the sum of A-B column. I gave the difference column alias as query="Select A,B,(A-B) as AB from Table"
and dt.Compute("Sum(AB)","").
Thus, resolved the error.

Categories

Resources