c# unable to edit datarow in dataset - c#

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.

Related

Can't call DataTable.Load() when IDataReader source has a VARCHAR(256) Identity column

I'm running into some trouble trying to load data into a DataTable using an IDataReader. To keep it really simple, I just call ExecuteReader() on the command, create a DataTable, call Load() on it and feed it the object implementing IDataReader:
...
if(dataReader.HasRows)
{
DataTable tempDT = new DataTable();
tempDT.Load(dataReader);
....
}
...
This works in the vast majority of cases. However, in (rare) circumstances, I get the following exception (column name is obviously variable - in this case, it's ID):
Error - MaxLength applies to string data type only. You cannot set Column `ID` property MaxLength to be a non-negative number
I investigated the source table I was trying to load, and I suspect that the problem stems from it having a VARCHAR(256) ID column, that is a Required, Unique, Key (the issue doesn't seem to occur when the PK is a regular old int). This type of situation is really uncommon in the source data, and while it definitely isn't ideal, I can't modify the schema of the source data.
I took a look at the SchemaTable in more detail, and I am at a loss:
ColumName - ID
ColumnSize - 256
ProviderType - NVarChar
DataType - {Name = "String" FullName = "System.String"}
IsIdentity - True
IsKey - True
IsAutoIncrement - True
IsUnique - True
It just doesn't make sense to me. The source table uses unique codes as the ID, and while it isn't the way I would've designed it, it's.. fine. But I don't understand how a String/Varchar can ever be an identity, auto-increment, etc.
Unfortunately, I'm at the mercy of this source data and can't mess with it, so I'm hoping someone here might have more insight into what exactly is going on. Can anyone conceive of a way for me to Load() my DataTable without applying all the constraints from the IDataReader source data? Is there an entirely alternative approach that would avoid this problem?
Thanks for reading, thanks in advance for any assistance. It's my first question so be gentle. If there's any more information that would help, please let me know!
EDIT: Some people asked for the full code for loading the DataTable. Appended here. Should add that the CacheCommand/etc. comes in from this 'InterSystems.Data.CacheClient' assm. Kinda hoping the problem can be approached more generically. In this case, the Query string is just a 'SELECT TOP 10 *' test.
using (CacheConnection cacheConnection = new CacheConnection())
{
cacheConnection.ConnectionString = connectionString;
cacheConnection.Open();
using (CacheCommand cacheCommand = new CacheCommand(Query, cacheConnection))
{
using (CacheDataReader cacheDataReader = cacheCommand.ExecuteReader())
{
if (cacheDataReader.HasRows)
{
DataTable tempDT = new DataTable();
tempDT.Load(cacheDataReader); // Exception thrown here.
cacheConnection.Close();
return tempDT;
}
else
{
cacheConnection.Close();
return null;
}
}
}
}
EDIT 2: In case it's not clear, I'm trying to extract the entirety of a (small) table from the Cache DB into a DataTable. I normally do this by calling dataTable.Load(cacheDataReader), which works fine 99% of the time, but breaks when the source table in the Cache DB has an identity column of type VARCHAR.
Calling Load() on my DataTable object (which is empty) causes it to infer the schema based on the result set from the imported IDataReader (in this case, CacheDataReader). The problem is that the schema in the CacheDataReader specifies the data in the list above^, and DataTable doesn't seem to allow the MaxLength property, even though the type is VARCHAR/String.
SELECT TOP 10 * FROM table
WHERE IsNumeric(ColumName) = 0
This will return only data where the Primary Key is of type Int

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.

Accesing table in VS 2013 C#

I want to do a login form in C# and a problem occurred. I want to search all rows of table but I get an error. I have done it two minutes earlier and it worked. Tried to re-do it and it failed. I have no idea how to set up the reference to database object (I didn't do it the first time and somehow it worked).
Line of code with error :
foreach (DataRow row in LoginsDataSet.Loginy)
error text:
An object reference is required for the non-static field, method, or...
As you mentioned in comment your DataSet instance variable is dane, then you must use something like this:
foreach(DataRow row in dane.Loginy.Rows)
{
....
}
in case if you have strongly typed dataset you may have something like this:
foreach(LoginsDataSet.LoginyRow row in dane.Loginy)
{
....
}

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.

A DataTable named 'Table' already belongs to this DataSet

when i am adding second table(dtResult) to data set that time it's giving error
A DataTable named 'Table' already belongs to this DataSet.
DataTable dtSession = new DataTable();
DataTable dtResult= new DataTable();
dtResult.TableName = "A";
dtSession.TableName = "B";
dtSession = objOpt.GetSearchDetails().Copy();
ds.Tables.Add(dtSession);
dtResult = objOpt.Search_Synchronous().Copy();
ds.Tables.Add(dtResult);
Thanks in advance
You need to name the tables after getting the copy from your method and before adding it to the DataSet.
DataTable dtResult= new DataTable();
dtSession = objOpt.GetSearchDetails().Copy();
dtSession.TableName = "B";
ds.Tables.Add(dtSession);
dtResult = objOpt.Search_Synchronous().Copy();
dtResult.TableName = "A";
ds.Tables.Add(dtResult);
Since you are getting the copy from your methods objOpt.GetSearchDetails().Copy() and objOpt.Search_Synchronous().Copy(), they are overwriting the names assigned to the table previously, and both of these are returning the table with name Table, that is why you are getting this error
I was getting this exception today, but it had nothing to do with adding a DataTable.
I have an ASP.Net Core WebApi, and in the Post and Put endpoints, I was attempting to save/update the new record and also search in the database (in the same table) for an existing record with the same details, and if such a record exists, to update it.
I was getting that exception when it tried to save these changes.
The solution was to split it up, save the new/updated record first...
// Avoid an "A DataTable named 'Users' already belongs to this DataSet." exception
await _context.SaveChangesAsync();
... and then check for an existing record, update it if necessary, and then doing a separate save...
await _context.SaveChangesAsync();
Yup, it's weird, but saving twice fixed this issue for me.
(I appreciate that this doesn't answer this exact StackOverflow question, but for anyone stumbling onto this page when they hit this strangely-worded exception, this'll be a lifesaver !)
I received this error when running command.ExecuteDataSet on a stored procedure where I declared a table variable then queried that table multiple times to return several datatables in the dataset. Because each datatable was queried from the same table, they all had the same name '#t'. I was able to resolve it by creating multiple table variables with different names in the stored procedure.

Categories