Trouble accessing data from DataTable - c#

I've got a DataTable the I'm trying to access the DataRow row by row like so:
dataTable.Select("someID=" + someID.ToString()).CopyToDataTable().Rows.Count;
This works fine for someID of 0-9, but when I get to 10 I get a System.InvalidOperationException. In Visual Studio DataTable Visualizer I can see someID as one of the columns with data of 0-24, so 10 should be there.
When I use the ImmediateWindow and look at dataTable.Select("someID=10") I get
{System.Data.DataRow[0]} and looking at dataTable.Select("someID=9") gives me
{System.Data.DataRow[1]}
What am I missing?

Well, why do you need the CopyToDataTable() method when all you need is the count of matches? You could simply use the Length or Count, isn't it?
x = dataTable.Select("someID=" + someID.ToString()).Length;

Related

Reading some rows in Excel

I have some problems when I try to read specific rows of an Excel document.
My implementation is here: Reading Excel in c# where some columns are empty.
How you can see, the entire Excel is taken into a DataTable.
Now I want get a specific range of n value:
I think is the DataTable the problem... I think that maybe I should to obtain another DataTable... maybe using another different query?
It seems that you are using a fixed template so you can solve using different queries on different ranges.
To select a particular range you can use this query:
string query = "SELECT * FROM [YourSheet$B58:D70]";
If you know where a range starts but not the number of rows you can use this syntax:
string query = "SELECT * FROM [YourSheet$B58:D]";
Using HDR=NO in your connection string and changing starting row you could use this query to simplify your next operations:
SELECT [F1] AS Compagnia,
[F2] AS Agenzia,
[F3] AS DataSinistro
FROM [YourSheet$B59:D]
Remember you can also use WHERE to filter your results or exclude empty rows; i.e.:
SELECT [F1] AS Compagnia,
[F2] AS Agenzia,
[F3] AS DataSinistro
FROM [YourSheet$B59:D]
WHERE [F3] IS NOT NULL

Convert Sorted DataView to Typed DataTable

i have a strong typed DataTable named Account wich i sorted on Account.FullName:
DataView dvAccount = new DataView(dtAccount)
dvAccount.Sort = "FullName desc";
The fullname is a generated field from my DataSet after my query, based on first name, middle, last etc. This means that sorting by SQL query is not an option unfortunately.
First i tried to get the table like this:
dtAccount = dvAccount.Table()
But this gave me the original Table the Dataview was based on. So after reading online i found out that i should have used the DataView.ToTable() function instead of the DataView.Table() function:
dtAccount = dvAccount.ToTable() as dsAccount.AccountDataTable; // returns null
dtAccount = ((dsAccount.AccountDataTable) dvAccount.ToTable()); // gives Convertion to Typed Datatable Error
Now i get the problem that my Account Table is Strong typed. so searching online tells me that i could go with the DataTable.Merge() Function or DataTable.ImportRow() for each row but these are told to be a very heavy procedures because every row gets checked on the Type. what's the best practice solution to this situation?
I just had the same issue. I used this kind of solution.
dtAccount = New dsAccount.AccountDataTable;
dtAccount.Merge(dvAccount.ToTable());
This works fine for me.
Tell me if you have a better one.

Trying to get the value of a specific column in every row of a dataset in visual c#

Sorry if the title is confusing. I was trying to keep it short.
I have a DataSet ds. In this dataSet are seven columns, One Machine Schedule Order, Two Machine Schedule Order, Series, Oven, Battery, and Oven Control Number.
I want to do a for loop from 1 to 183 (which is how many records there are, and they're the values of One Machine Schedule Order, which is a primary key), finding what the values of series, oven, and battery are in the matching row.
If I were to mix C# and SQL, what I would do is:
for (int i = 1; i<184; i++)
{
String result = SELECT series||battery||'/'||oven where \"One Machine Schedule Order\" = i
Console.WriteLine(result)
}
So if that doesn't make sense, I want to get one value back per row and I want it stored in a string. Then I want to output that string to the screen.
DataRow[] row = ds.Tables[0].Select("'One Machine Schedule Order' = " + "'"+i+"'");
Above is how I'm getting each row, and then just to see if the data was getting there, I tried
Console.WriteLine(row[0][0]); to print the value of the first column, which should be a three digit number. However, I'm getting an IndexOutOfRangeException on that statement. Any ideas what I'm doing wrong and how to fix it?
try
Console.WriteLine(row[0].ItemArray[0])
Try this.This should work if I understand your question right
foreach (DataRow dr in ds.Tables[0].AsEnumerable())
{
Console.WriteLine(dr[0].ToString());
}

DataRow[] itemsarray

I can't seem to be able to be access elements that my DataRow has pulled from my DataTable, I haven't had that much practice with c# either
This is my code:
DataRow[] results = dt.Select("[Acc No] = '"+ search +"'");
I have tried casting teh objects from datarow to a string but that was not working.
Search is just a string from a textbox.
When debugging i can see the items array with all the data in it so i know the select is working, can anyone help?
You need to provide more code that that... Such as how you're trying to access the contents of a DataRow. To get a value out of the row, I believe the syntax would be something like results[rowNumber][columnNumber/name]
I.e. results[0][0] to get the first column value out of the first row, or results[0]["Id"] to get the "Id" column from the first row.
Of course you should check results.Count() before attempting to access the DataRow array.

Is it possible to remove a duplicate value from a datatable in c#?

I have a datatable with a column MobileNo.... My datatable has 150 rows and each row has MobileNo... Now how to check every MobileNo is unique by parsing all the datarows of the datatable in c#?
EDIT:
"This datatable is being created by reading a CSV file"
Use Linq and Group By the MobileNo then you will need to traverse the collection and see which MobileNo's have multiple records and then do whatever you wish to remove what you deem is duplicated.
Edit: From Linq 101 Samples.
Try
DataTable.DefaultView.ToTable(bool distinct, string[] ColumnNames)
the third overload on that is what your looking for I believe, let me know how you get on.
Specify which columns are to be deduped in the string[]
And a true false for distinct records
So you can either just select a subset of data out or dedupe by setting distinct to true.
You will need to do some jiggery pokery to get your dataset how you want it, but I think thats what your after.

Categories