I am having trouble updating the database from a dataset. The data is inserted into the datatable in the dataset correctly and I can see it in a datagridview, but when I look at the database the columns have not been added
This is my code below:
TimeSpan timeDif = dateToSet.Subtract(dateInTable);
int timeDifference = (int)timeDif.Days;
for (int i = 0; i < timeDifference; i++)
{
DataColumn columnToAdd = new DataColumn(dateInTable.AddDays(i + 1).ToShortDateString());
Program.pDiResourcesDataSet.WorkingProjects.Columns.Add(columnToAdd);
Program.WorkingProjectsA.Update(Program.pDiResourcesDataSet.WorkingProjects);
}
Program.pDiResourcesDataSet.AcceptChanges();
Program.WorkingProjectsA.Update(Program.pDiResourcesDataSet);
As rene said, you cannot add columns with dataadapter, rather use separate DDL statement to carry out the DDL operations, and use dataadapter to manipulate the records
Related
I have a dataset that looks like the image. I'm trying to filter by table and get all the columns next to it and compare them against other datasets
This dataset has tables named table 1 and table 2 and when they're selected they look like the picture below. It shows the columns and I need to compare those columns against the rows from the matching table in the first dataset
I've looked at dataview but that would be a lot of work and I'm very inexperienced. I'm trying to find a way to implement a foreach loop that'll get the name of the table in the first dataset and then compare the rows in it against the columns inside the datatable in the second dataset that matched the table name from the first dataset.
Without knowing more about these DataSets (like do they have primary keys, the data types of the columns, the number of rows in each table, etc), I can only provide limited help. The following example tries to be as general as possible and avoid some basic problems:
DataSet ds1 = <<fetch dataset1>>;
DataSet ds2 = <<fetch dataset2>>;
foreach (DataTable tbl1 in ds1.Tables)
{
if (ds2.Tables.Contains(tbl1.TableName))
{
DataTable tbl2 = ds2.Tables[tbl1.TableName];
List<string> commonColumnNames = new List<string>(tbl1.Columns.Cast<DataColumn>().Select(c => c.ColumnName).Intersect(tbl2.Columns.Cast<DataColumn>().Select(c => c.ColumnName)));
int maxRows = Math.Min(tbl1.Rows.Count, tbl2.Rows.Count);
for (int r = 0; r <= maxRows; r++)
{
foreach (string colName in commonColumnNames)
{
if (tbl1.Rows[r][colName] != tbl2.Rows[r][colName])
{
// Different value
}
}
}
}
}
Update 1: I've added comments to the following example to explain step-by-step what this code is doing. As I try to say before, since I didn't know much about your data, I had to put in extra code. This extra code is for things like: 'Does the table ABC exist in both DataSets?', 'Do the two tables have the same columns in them?', 'Do the tables have the same number of rows in them?'. Your original question did not have this information, so I made this code a little more robust to handle those unknowns.
DataSet ds1 = <<fetch dataset1>>;
DataSet ds2 = <<fetch dataset2>>;
// Loop through all of the tables in the 1st DataSet
foreach (DataTable tbl1 in ds1.Tables)
{
// If the 2nd DataSet has a table with same name as the one from the 1st DataSet
if (ds2.Tables.Contains(tbl1.TableName))
{
DataTable tbl2 = ds2.Tables[tbl1.TableName];
// Create a list of column names that the two tables have in common.
// We will only compare the values in these two tables, in this set of matching column names.
List<string> commonColumnNames = new List<string>(tbl1.Columns.Cast<DataColumn>().Select(c => c.ColumnName).Intersect(tbl2.Columns.Cast<DataColumn>().Select(c => c.ColumnName)));
// Before we start comparing the rows in the two tables, find out which one has the fewer number of rows in it.
int maxRows = Math.Min(tbl1.Rows.Count, tbl2.Rows.Count);
// If the tables have a different number of rows, then we will only compare the set of rows numbered 0-to-MinRowCount
for (int r = 0; r <= maxRows; r++)
{
// For each row, compare the values of common columns
foreach (string colName in commonColumnNames)
{
if (tbl1.Rows[r][colName] != tbl2.Rows[r][colName])
{
// Different value
}
}
}
}
}
I wish to create an array of DataTables with the same columns. Is there a way to set the schema of all the DataTables in one shot instead of running through each DataTable and add the columns?
No, you can't do this without a loop. Even if there was a method like DataSet.CloneManyTables it would use a loop for you. A LINQ solution would also use a loop. So use following:
You can use DataTable.Clone, for example with 100 clone tables:
for (int i = 1; i <= 100; i++)
{
DataTable tClone = tSource.Clone(); // tSource is your source-table
tClone.TableName = $"{tClone.TableName}_{i + 1}";
ds.Tables.Add(tClone); // ds is your DataSet
}
The columns can have the same names but the table-name must be unique.
I have created a datatable which has some records, now what I want is to copy the record of first datatable to another datatable.
I tried like below:
Session["AmountData"] = AmountDatatable; // 1st datatable which has data
DataTable CompanyWiseRecord = new DataTable();
for (int i = 0; i < AmountDatatable.Rows.Count; i++)
{
CompanyWiseRecord.ImportRow(AmountDatatable.Rows[i]); // 2nd datatable which does not have data
}
Kindly let me know where I am going wrong.
You directly use session Like:
DataTable CompanyWiseRecord = (DataTable)Session["AmountData"]; //Retrieving DataTable from Session.
if you are not fetching data from table one based on some condition or assign particular rows or columns then You can do it simply by using
DataTable CompanyWiseRecord=AmountDatatable;
Is it possible to add a row to the middle of an existing dataset with c#? I've done a lot of searching and haven't been able to find anything on how to do this. What have I tried? I've tried searching a lot and haven't found anything like an 'insertAt' method for datasets.
Thanks
Mike
The DataSet consists of a collection of DataTable objects so I assume that you are talking about a Datatable, right? If so, it has an InsertAt method:
DataTable dt = dataset.Tables[0]; //Get first datatable from dataset
DataRow row = dt.NewRow();
//fill row
dt.Rows.InsertAt(row,3); //Insert at index 3
DataSet does not have a rows collection, so you can't add a row to it at all.
You can insert a row by index into a DataTable object using DataTable.Rows.InsertAt(row, i). If the table is in a DataSet, your syntax would be DataSet.Tables[i].Rows.InsertAt(row, 0)
In my opinion (though this could take a lot of time), you can create an array or a list array then transfer all the data there from your dataset through for loop or any loop...then put an if statement inside to check where you want to put your extra data like this:
List<string> arrayList = dataset;// i know this is not possible just showing you that you have to put all your data from dataset to array:)
List <string> newList = new List<string>();//its up to you if you want to put another temporary array or you could simply output your data from the loop.
//THE LOOP
for(int i = 0; i<=arrayList.Count(); i++){
if(i == x)//x is the index or you may change this statement its up to you
{
//do the print or pass the data to newList
newList.add(arraList[i]);//not sure about this. its been a while since the last time i use this array list..
}
}
another way is customize your query(if your pulling out some data from database)
happy coding:)
Here's a short sample of doing it:
class Program
{
static void Main(string[] args)
{
DataSet ds = new DataSet();
DataTable dt = ds.Tables.Add("Table");
dt.Columns.Add("Id");
for (int i = 0; i < 10; i++)
{
dt.Rows.Add(new object[]{i});
}
var newRow=dt.NewRow();
newRow.ItemArray=new string[]{(dt.Rows.Count/2).ToString()+".middle"};
dt.Rows.InsertAt(newRow, dt.Rows.Count / 2);
}
}
I am trying to add data to a newly created column...
DataSet ds = new DataSet;
ds.Tables[0].Columns.Add("Count");
Data.DataSource = ds;
Data.DataBind();
Where do I add, a hardcoded count? (I want count column to have '4')
You add data to rows - so:
DataSet ds = new DataSet();
DataTable table = ds.Tables.Add();
table.Columns.Add("Count", typeof(int));
table.Rows.Add(4);
However, there are probably easier options; a DataSet seems massively overkill to bind a single value.
It technically is a repeater function but if I get the hardcoded function... I can make the code to make it flexible. I got it to work. I was adding a column to an existing table. This is what I did...
for int i=0; i<ds.tables[0].Rows.Count; i++)
{
ds.tables[0].Rows[i]["Count"] = "4";
}