DataRow[] itemsarray - c#

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.

Related

Trouble accessing data from DataTable

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;

Cannot fill temporary table from gridview

During a gridview => database update function, I use a column-by-column conversion to string in order to pass data back to my database as a whole command string. I'm aware that this sounds convoluted, so here is an example:
Classes in use:
InvoiceHandler.cs
Default.aspx.cs
Since this is a rather large snippet, here is a pastebin:
Default.aspx.cs: http://pastebin.com/Y3fJZ36Z
InvoiceHandler.cs: http://pastebin.com/ZsdAnDxr
At the first point of conversion (invoiceTableEdited.Columns["Column1"].ColumnName = "#K_INV";) I get a NullReferenceException error, assumedly because the method call in Default.aspx.cs
handler.invoiceTableEdited = ViewState["invoiceTable"] as DataTable;
handler.invoiceTableEdited.Rows[row.RowIndex]["K_INVOICE"] = sK_INVOICE;
appears to be having trouble.
What must I do to resolve this?
I'll be honest, it's not very clear how you're trying to create the column collection for the table 'InvoiceTableEdited'. What it looks like you're doing in OnUpdate is you are assigning a string variable to a row in the table with a given index and a column that doesn't currently exist. Your basically saying put this string into a cell with a row number of 'x' and a column name of "column1". At this point "column1" doesn't exist.
I'd create the columns you need first in InvoiceHandler.cs like this (assumes they're string):
invoiceTableEdited.Columns.Add("MyColumn1", typeof(string));
invoiceTableEdited.Columns.Add("MyColumn2", typeof(string));
Obviously if you have loads of columns and you don't care about their names then just create a loop and add them that way. This will give them the naming convention "ColumnN", where 'N' is the number. You can then assign a name to them by referencing there name ("Column1" for example) or using their index.

Control.Bindings.Add problem, exception telling me that the program cannot bind to a column that does not exist (but it does)

When I try to use the following line of code:
cboSite.DataBindings.Add("Text", _dtSite.Select("Site <> 'ALL'"), "Site")
I get the following exception:
EXCEPTION : Cannot bind to the property or column Site on the DataSource.{10}Parameter name: dataMember
details I am connecting to an Access database and using .net 3.5 and writing the code in VB. In this database I have a table named Sites with a column named Site and when I try to use the line of code above I get the exception noted. I was under the impression that I could explicitly name the column that I need to use in my control (combo box). What am I doing wrong?
Its because the result produced from the .Select() statement, produces an output of a collection of DataRows, when what you need is to bind to a DataTable source that has been filtered. What you can do is filter the rows 1st, place the filtered rows into a clone of the original DataTable and then bind to the filtered DataTable. This code sample is below:
'Begin by returning an array of DataRows that have been filtered
Dim filterdRows() As DataRow = _dtSite.Select("Site <> 'ALL'")
'Clone the original DataTable which will give us an empty one
'with an identical structure.
Dim _dtFiltered As DataTable = _dtSite.Clone
For Each drFiltered As DataRow In filterdRows
'Import the rwo into the filtered table
_dtFiltered.ImportRow(drFiltered)
Next
'Bind the ComboBox to the filtered DataTable
cboSite.DataBindings.Add("Text", _dtFiltered, "Site")
Hope this helps!

c# unable to edit datarow in dataset

I'm trying to edit the data in a dataset (change the value in a column on one row), which is not linked to a database. I've been googling for about an hour with no results and no good examples out there. Hopefully someone can help me.
My table (DataTable1) has these columns - ThreadID (string, PK), StatusText (string).
I can select a row in a DataGridView, and get the ThreadID value. No matter how I've tried to edit the row in the associated dataset, either nothing happens or I get an error. Here's what I have now:
string sThreadID = "";
sThreadID = gridThreads.Rows[gridThreads.CurrentRow.Index].Cells["ID"].Value.ToString(); // gives me a good id, which is in the dataset
DataRow drRow = dataThreads.Tables["DataTable1"].Rows.Find(sThreadID);
drRow["StatusText"] = "Test";
The error I get when getting the row (3rd line) is: "Object reference not set to an instance of an object.". I can't create a new DataRow object because there's no public constructor for it (according to my research).
I'm sure I'm missing something basic, but I'm not familiar with working with datasets. What am I doing wrong? Thanks for your help.
How does drRow know what "statusText" is? You may be only getting a shalllow copy, try cloning/copy so that the data types of the row get copied as well. Just an idea.
Does it happen that dataThreads or Tables["DataTable1"] is null ?
To create a new row, use the NewRow() method of the data table.

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