SQLiteDataAdapter does not fill the specified DataTable - c#

I'm attempting to pull some data from a SQLite database so that I can populate a GridView in my GUI: here is the code that returns the DataTable:
DataTable table = new DataTable();
SQLiteDataAdapter adapter = new SQLiteDataAdapter(this.command.CommandText, this.connection);
adapter.Fill(table);
return table;
For some reason after calling adapter.Fill, the DataTable is still not populated with anything. So far I've verified that the command text is correct and that the connection contains the correct connection string. Both are used successfully in other parts of the application. No exceptions seem to be thrown... Is there any place else I should be looking for trouble? Am I using the API incorrectly?
Thanks!

This looks like correct usage.
One thing to check -- after the fill you say the datatable is not populated. Were you just checking Rows.Count? What about columns? If the Fill creates columns to match your SELECT statement, but there aren't any rows, then you know the code is working but there's a problem with either your query, or you're not hitting the same database you think you are.

Is there a constructor that lets you just pass in the command directly?

Related

DataTable TableName = DataView.ToTable("TableName") will not work

I am trying to make a copy of a DataTable with a different TableName. I have used this in the past without issue and the code from the past works just fine today. When i try using the exact same code the table will add to the DataSet but there is no data or column names.
DataView dv = dataSet.Tables["Traveler"].DefaultView; /// the dataview is exactly like the datatable
DataTable Parts_Kit = dv.ToTable("Parts_Kit"); /// the new datatable has no data or columns
dataSet.Tables.Add("Parts_Kit"); /// Parts_Kit is added to dataset but completly blank
I have dorked around with this for 4 hours now with no results. What could i possibly be missing?
as per the definition of Add(string DataTable name), you are creating a new / blank table in dataSet that has nothing to do with the DataTable Parts_Kit.
As per MS documentation as well, you want to use,
dataSet.Tables.Add(Parts_Kit); // without quotes around Parts_Kit.
This will add the table you created from view (Parts_Kit) to the DataTableCollection, dataSet. To Test this as well, you can print Parts_Kit.TableName to see if it is the correct "Parts_Kit" name (from ToTable("Parts_Kit") command).

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.

NullReferenceException after accessing datatable added to dataset

So basically, I have a dataset which contains several datatables, from which columns are removed in my program.
At some point I want to reset my datatable so that all changes to the columns are gone.
Because I can't just create a new instance of that datatable in my dataset, I do this:
this._dsTest = new DsTest();
//Removing/Adding the first datatable
this._dsTest.Tables.Remove(this._dsTest.TestTable);
this._dsTest.Tables.Add(new DsTest.TestTableDataTable());
//Removing/Adding the second datatable
this._dsTest.Tables.Remove(this._dsTest.ReloadTable);
this._dsTest.Tables.Add(new DsTest.ReloadTableDataTable());
However, if I try to access the second datatable, I receive a NullReferenceException, telling me that the second dataTable is null.
What I don't understand is:
If I would add another table to my dataset, that third table would throw the NullReferenceException once I try to access it, and the second one would work without any problems.
Is there anything I am missing that would explain this behaviour?
Update:
This is the code I am using to access the datatables:
this._dsTest.TestTable.AddTestTableRow("Test", 1);
this._dsTest.ReloadTable.AddReloadTableRow(1, "Reload", 1); //This throws the exception
When you made any changes to your dataset / datatable you should use AcceptChanges method.
For example :
this._dsTest = new DsTest();
//Removing/Adding the first datatable
this._dsTest.Tables.Remove(this._dsTest.TestTable);
this._dsTest.AcceptChanges();
this._dsTest.Tables.Add(new DsTest.TestTableDataTable());
this._dsTest.AcceptChanges();
So, I finally found the answer to my problem.
The reason that I received the NullReferenceException when I tried to do
this._dsTest.ReloadTable.AddReloadTableRow(1, "Reload", 1)
was, that the datatable that I added to the dataset was working perfectly fine, but the public property of that datatable (this._dsTest.ReloadTable)
was not set to the datatable I had juts added.
So the solution for this is to simply do this:
this._dsTest.InitVars();
After that, accessing the DataTable works without a problem.
Thanks to anyone who tried to help me with this.

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.

C# SQL Data Adapter Fill on existing typed Dataset

I have an option to choose between local based data storing (xml file) or SQL Server based.
I already created a long time ago a typed dataset for my application to save data local in the xml file.
Now, I have a bool that changes between Server based version and local version. If true my application get the data from the SQL Server.
I'm not sure but It seems that Sql Adapter's Fill Method can't fill the Data in my existing schema
SqlCommand cmd = new SqlCommand("Select * FROM dbo.Categories WHERE CatUserId = 1", _connection);
cmd.CommandType = CommandType.Text;
_sqlAdapter = new SqlDataAdapter(cmd);
_sqlAdapter.TableMappings.Add("Categories", "dbo.Categories");
_sqlAdapter.Fill(Program.Dataset);
This should fill my data from dbo.Categories to Categories (in my local, typed dataset).
but it doesn't. It creates a new table with the name "Table". It looks like it can't handle the existing schema.
I can't figure it out. Where is the problem?
btw. of course the database request I do isn't very useful that way. It's just a simplified version for testing...
The Fill overload you are using, passing in a DataSet will always create a NEW DataTable in the supplied DataSet with name "Table" (MSDN).
So, I think you either need to change your TableMapping to:
_sqlAdapter.TableMappings.Add("Table", "Categories");
(MSDN) assuming your DataTable name is "Categories".
Or, don't use TableMappings and just supply the second argument - the name of the DataTable in that DataSet you want to populate (MSDN). The approach I usually use is actually to pass the DataTable itself that you want to populate, instead of the DataSet (MSDN).
Try _sqlAdapter.TableMappings.Add("Table", "Categories");, but as i remember you will have to add column mapping also. btw, you can try to create typed dataadapter, it is useless thing, but you can take mapping from there.

Categories