combined primary key search in DataTable - c#

I am new to C#.net.I want a simple solution for my problem.
I have a datatable with three fields used as a PRIMARY KEY**(ITEM_CODE,WAREHOUSE_CODE,LOTNO)**.
I want to get the row index of the row that I 'm trying to match with this PRIMARY KEY from my datatable.
How can I do this?
Thanks a lot for your help and interest...

I solved my problem with this code,
int rowIndex = dt.Rows.IndexOf(dt.Select("ITEM_CODE = 'i001' AND WAREHOUSE_CODE='001' AND LOTNO='111'")[0]);
Thank you for all of your interest and answers.

Call the Find() method to get the reference of DataRow object based upon the given values and if it is (DataRow object) not null then compare its reference to each row of Rows collection of Datatable and obtain the row index.

Related

identify which row caused exception in c#

I have a datatable and we fetch values from one database, put them in a datatable and insert them into another database. I am using the execute query method of sql and stored procedures to insert data. If one row has a string or binary data truncated error can we identify this using c# and printing that row on console??
Basically, everything is fine in dt but when I insert it I will get exception. Can I get the detail row which is causing exception?
Can anyone guide me on how to proceed with this? I need to know the exact row which is causing the issue.
If you don't know which specific row is causing the error, you'll probably have to foreach loop through it.
foreach(DataRow row in yourDataTable.Rows) {
//Check for the issue.
}
This will loop through each DataRow in your table. You'll have to check each individual cell, but you can't foreach loop through it, you'll have to do it manually based on the row. For example, if you know each Column name, you can do:
if(row["whatever"]...) // you want to check for the issue here.
You can also do:
int len = yourDataTable.Columns.Count;
foreach(DataRow row in yourDataTable.Rows) {
for(int i = 0; i < len; i++) {
//Check based in row[i] for your problem.
}
}
That will loop through each row, then each cell in each row based on index. You'll have to do your comparison based on the type received, though, which you'll have to determine based on the contents of your DataTable.

Why does CopyToDataTable unsort the DataRow Array

I've got a datatable that I need to sort and place back into another datatable. On the face of this its easy as below:
DataTable sortme = getdata();
sortme.Select("col1 = 'something'", "sortbyme ASC").CopytoDataTable();
However, I've found as soon as I pass the DataRow array created by select() to CopytoDataTable(), the new datatable is no longer sorted by sortbyme.
How do I fix this without creating a loop to push each DataRow into a datatable? And what is causing the sorting to be lost?
There is already a question asked and solved about how to sort DataTables by row, please look at it here, it might help you:
Sorting rows in a data table

Compare Two Datatable that has same schema and N no of columns

I have two DataTables both has same no of columns and column names.Am in need of comparing both for the different rows.Which means even if one cell doesnt match the row should be plotted.I tried with
table1.Merge(table2);
DataTable modified = table2.GetChanges();
But this is returning null.
Where as
IEnumerable<DataRow> added = table1.AsEnumerable().Except(table2.AsEnumerable());
This is returning the table1 values alone even there are different values for a some cells in table1 compared to table2.
Can anyone help me for this comparison.Various sites i referred,the instruction said was to compare each column in a row but since i have N no of columns i cant go with that.I need a smarter way of comparison which would be efficient.
Thanks in advance
IEnumerable<DataRow> added = table1.AsEnumerable().Except(table2.AsEnumerable());
should be changed to
IEnumerable<DataRow> added = table1.AsEnumerable().Except(table2.AsEnumerable(),DataRowComparer.Default);
Because DataRows don't know how to compare themselves to eachother on their own. You can also provide your own equality delegate instead of DataRowComparer.Default if required.
Have you tried using merge? http://msdn.microsoft.com/en-us/library/fk68ew7b.aspx
Datatable1.Merge(datatable2);
DataTable DataTable3 = Datatable2.GetChanges();

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.

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.

Categories