Retrieving data from a dataset - c#

I am sorry if this question has been asked before, i have tried using google but all the answers couldn't help me.
I am a total begginer in using datasets and connecting to database using c#. I have a dataset usersDataset gotten from an access 2007 Database that has two table Administrators and Users. Both tables have two columns UserName and Password. The Administrators Table has a row of data.
Now the problem is that i want to retrive the row in the dataset. I have tried many things but they all throw an Exception.
I have tried
DataRow rows = usersDataset1.Administrators.Rows[0];
MessageBox.Show(rows.ToString());
Also tried
usersDataset data = new usersDataset();
MessageBox.Show(data.Administrators.Rows[0].ToString());
Also tried
MessageBox.Show(usersDataset1.Tables[0].Rows[0].ToString());
All of the following code snippets throw an IndexOutofRangeException with the message There is no Row at Index 0.
Then i tried
MessageBox.Show(usersDataset1.Administrators.Rows.Count.ToString());
It shows '0'.
Pls what did i do wrong and how can i correct it?
EDIT : When i drag the Administrator from the DataSources Windom to the form and i run the Application. The row is displayed.

Your current problem (as evidenced by your last example) is that you have no data in the Administrators table. Perhaps you aren't properly loading your data into your data structure?
On a side note, it's generally better to iterate through the rows in a table unless you will always know exactly how many rows it will contain. For example:
foreach (DataRow row in usersDataset1.Administrators.Rows)
{
//Do stuff here...
}

Related

In VSTO how can I delete rows from a Table in Excel

I have a table in an Excel worksheet where I need to programatically remove entire rows using VSTO. After a lot of searching here and everywhere else, I was unable to find the answer. Due to some unrelated code, I also cannot delete the first row of the table, but need to remove all other rows.
Here are the specific requirements:
One of the functions of this addin is to populate the table. This is done through a loop starting with the "root" named range in the left column of the first row of the table.
Whenever populating the table, I first need to delete all data from the table and then add the new data. I need to use the "root" to add the data, so I can't have it deleted.
I am using the Table for the automated formatting instead of formatting the table manually after adding each cell.
I never know how many rows will be added, but it will always be at least one.
After banging my head on this for a few hours, I slept on it and came at it refreshed this morning. After much trial and error, here is the code I came up with.
var deplTable = ThisSheet.Evaluate("DeploymentTable");
if (deplTable.ListObject.ListRows.Count > 1)
{
do deplTable.ListObject.ListRows[2].Delete();
while (deplTable.ListObject.ListRows.Count > 1);
}
NOTE: ThisSheet is set to the correct sheet earlier. The application works on multiple sheets, so it needs to be flexible.
I tried this a few ways before finally getting it to work. Looping through the rows gave unexpected results; possibly due to timing issues between Excel and VSTO.
Hope this helps other people!

Resizing column size of a table in DB and effecting with the dataTableAdapter on a Dataset.xsd

I'm asking about a problem with C# and Visual Studio 2012; I'm trying to resize a column of a table in my database, effect with the dataTableAdapter on a Dataset.xsd
I'm using DataTableAdapter from a stored procedure with a SELECT statement to populate a DataGridView, reports and many more.
I created the table long time ago, but now there is an a problem with it.
I had to increase the length of a column and I changed the appropriate column length of the DataTable also. But it didn't give me the solution. still whenever I Fill or Get data through that DataTableAdapter it response with the previous (original) size of the column.
But when I create a new DataTable and redirect my code to the new DataTableAdapter, it works.
Why is this happening ?
Because redirecting code to the new DataTableAdapter is little bit difficult because I don't know all the places it use in the entire solution.
And also if can please tell me how to add new column to the table and deal with the DataTableAdapter with it also.
Thanks and waiting for your reply.
after doing small research with my friends , i got an proper way to fix this error.
not even re-sizing, but also any other change with the database Table or storedprocedure you have to reconfigure the dataTableAdaptor, unless it just work as , when it was created.
it will continue with major errors or sometimes, it will function incorrectly, even you cant figure out there is an error.
so whenever you do any change with the database Table or storedprocedure go to the dataSet.xsd , where the dataTable locate and right on the dataTable , then configure it again.
this saved me and worked.

All rows are not getting imported to datatable using oledbconnection

I am stucked up at this point. I searched alot on gooogle but didnot find anything.
My problem is:
I have an Excel file which i want to export to datatable and from datatable i want to save it to oracle DB.
Excel file contains multiple columns and each column consists of large data(approx 20000characters/numbers).
using oledbconnection,excel columns with such large data are not copied to datatble.(Small data columns gets copied).
Can anyone suggest workaround to my problem???
Thanks in advance.
Check the dataypes and their length i.e nvarchar(3000)
If that doesnt work, test with a small set of data maybe 5 rows, you should be able to see a trend there.
Also check the data type of your application, sometimes for large number you may use long, or if they are large strings maybe use a stringbuilder to pass the data instead of just a string....

Saving a deleted row in a datatable to be deleted later in a database

Sorry if the title is a bit vague, but I'm trying to think of a good way to do this right now. We are using .NET 3.5, by the way.
Example code:
if (ExistsInDB(dt.Rows.Find(rowID)))
dt.Rows.Find(rowID).Delete();
else
dt.Rows.Remove(dt.Rows.Find(rowID));
If the row exists in the database, mark it for deletion. Otherwise, remove it from the datatable.
Now, if I go ahead and change the DataRow's RowState property to Deleted via the DataRow.Delete() method, that works fine except I can't access information I would need to delete the row from the database when the user hits "Save Changes".
I don't want to store the rows in another datatable because I already have a lot of other data tables and I don't want to add more complexity to the object. I would put another column on the datatable with the name "ToBeDeleted" or something, but the tables get displayed in the program via a grid.
Essentially, I want to be able to flag a DataRow in a DataTable for deletion in the database later without resorting to throwing the DataRow into a List or adding another column to it. Is this possible?
You can get the deleted row's data by writing
row["ColumnName", DataRowVersion.Original]
Why not use DataAdapter for this? You can learn how to do this at http://msdn.microsoft.com/en-us/library/xzb1zw3x%28v=VS.90%29.aspx
Thanks,
Vamyip

VC# 2008: Get Table names from DataTable into ComboList / How to merge tables from 2 MDBs?

My program that I am writing's purpose arose with this issue:
There are two users, each user saves to a .MDB file. One user has half the updated / correct information (the other half is outdated) and the other user has half the information (the other half is outdated).
User1: 25% + 25% = 50% current information needed the other 50% is outdated
User1 works on 2 out of the 4 items.
User2: 25% + 25% = 50% current information needed the other 50% is outdated
User2 works on 2 out of the 4 items.
I need to take that 50% (2 out of the 4 items) from lets say...User1 and add it to User2 making it 100% current (4 out of 4 items).
Their SQL style table structure is (should be anyways) identical (but if possible I would like to provide an event where for some a new table was added I would know)
If I could find out how to get all the table names from the DataTable, I could systematically array through the DataTable and replace the tables with the tables from the other .MDB file that I know need to be updated. I know DataSet has "DataSet.Tables" ...but that doesn't help me very much.
If I can do that, I can also add the tables to a combo box and create functionality to where whatever the combo box says, thats the table I will list on my Datagrid.
If any of you have any ideas on how to go about doing this (or if you even understand what i'm saying) please let me know. I'm 70% done with ths project, and these seem to be my last logic road blocks. I think I explained this right.
How do I list just the Table names
in a DataTable object.
What are your ideas on taking specific Tables out of a .MDB file and adding them to another .MDB file?
How would I go about inputting a ComboList drop-down box that included all the tables names...when I changed the table name it would list those contents on a Datagrid.
Is there a way to list tables on a Datagrid, and when you click on a Table it lists the contents of that table (kind of like a Tree structure).
EDIT:
I think he is right! I think DataTables are just one table whereas DataSets are sets of Tables. With that in mind, how do I list all the tables in a .MDB file into a DataSet? That would fix my problem perfectly.
I thought that a DataTable object was only a single table and a DataSet was what contained one to many DataTables.
If you are looking for the actual name of your DataTable, that would be accessed through the DataTable.TableName property.
Edit: If you are wanting to add DataTables into a DataSet object, just create a new DataSet and then use the .Add() method.
Dim DS as new DataSet
Dim DT as new DataTable("TableName")
DS.Add(DT)
You should then be able to loop through your DataSet and retrieve table names by accessing each DataTable's TableName property:
For each table as DataTable in DS.Tables
Console.Writeline(table.TableName)
Next

Categories

Resources