?
Solved

Updating a row in a DataSet

Posted on 2008-06-24
2
Medium Priority
?
665 Views
Last Modified: 2013-12-16
Hi
I have 2 DataSets. One, dsComplete, which contains all my data and another, dsSummery, which is empty. Now I want to add rows to dsSummery from dsComplete. In dsComplete "SubjectInComplete"-column has equal values for 3-4 rows, but the other fields in thies rows has other values. I want to make a summery of thies values in dsSummery where "SubjectInComplete" is unique.

Well below is my code so far. I don't get errors, but it's not working as it should - "SubjectInSummery" isn't unique. And also isn't there a better way than using try..catch - I don't fell like is that safe in this case.

Here is what dsComplete could look like...

SubjectInComplete ; Question1 ; Question2
"Math" ; 2 ; 5
"Math" ; 1 ; 3
"Math" ; 4 ; 1
"English" ; 2 ; 1
"English" ; 1 ; 3
"English" ; 2 ; 5
DataSet dsComplete; // Code filling dsComplete not shown here
DataSet dsSummery;
 
String RememberLastOne = "";
 
foreach (DataTable myTable in dsComplete.Tables)
{
	DataView dv = myTable.DefaultView;
	dv.Sort = "SubjectInComplete"; // Sort by this one so that the code can check i current and last row is identical
	
	foreach (DataRow row in myTable.Rows)
	{
		try // Try to edit row
		{
			
			DataRow[] editRow = dsSummery.Tables[0].Select("SubjectInSummery LIKE '%" + RememberLastOne + "%'");
			switch (row["Question1"].ToString())
			{
				case "1": // add value to a field in this row
					editRow[0]["Question1"] = Convert.ToInt32(editRow[0]["Question1"]) + 1;
					break;
				case "2": // add value to a field in this row
					editRow[0]["Question2"] = Convert.ToInt32(editRow[0]["Question2"]) + 1; 
					break;
			}
		}
		catch // If edit failed create new row
		{
			DataRow newRow = dsSummery.Tables[0].NewRow();
			newRow["ID"] = i + 1;
			newRow["SubjectInSummery"] = row["SubjectInComplete"];
			newRow["Question1"] = 0;
			newRow["Question2"] = 0;
			dsSummery.Tables[0].Rows.Add(newRow);
		}
 
		// Remember this row 
		RememberLastOne = row["SubjectInComplete"].ToString();
	}
}

Open in new window

0
Comment
Question by:Balltown
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
2 Comments
 

Accepted Solution

by:
Balltown earned 0 total points
ID: 21854389
Okay, think I've got it now. I made "SubjectInSummery" the primary key and searched for it.. Se code.
DataSet dsComplete; // Code filling dsComplete not shown here
DataSet dsSummery;
 
// Make SubjectInSummery the primary key
DataColumn[] PriKeyName = { dsSummery.Tables[0].Columns["SubjectInSummery"] };
dsSummery.Tables[0].PrimaryKey = PriKeyName;
 
int i = 0;
 
foreach (DataRow row in dsComplete.Tables[0].Rows)
{
	// Find primary key
	DataRow editRow = dsSummery.Tables[0].Rows.Find(row["FAG"].ToString());
 
	// if primary key is not null you can edit row
	if (editRow != null) {
                        
		switch (row["Question1"].ToString())
		{
			case "1": // add value to a field in this row
				editRow["Question1"] = Convert.ToInt32(editRow[0]["Question1"]) + 1;
				break;
			case "2": // add value to a field in this row
				editRow["Question2"] = Convert.ToInt32(editRow[0]["Question2"]) + 1; 
				break;
		}
	}
	else
	{
		DataRow newRow = dsSummery.Tables[0].NewRow();
		newRow["ID"] = i + 1;
		newRow["SubjectInSummery"] = row["SubjectInComplete"];
		newRow["Question1"] = 0;
		newRow["Question2"] = 0;
		dsSummery.Tables[0].Rows.Add(newRow);
	}
	i++;
}

Open in new window

0
 

Author Comment

by:Balltown
ID: 21854901
Woops... the line
DataRow editRow = dsSummery.Tables[0].Rows.Find(row["FAG"].ToString());

Should read
DataRow editRow = dsSummery.Tables[0].Rows.Find(row["SubjectInComplete"].ToString());

"fag" means "subject" in danish ;-)
0

Featured Post

Interactive Way of Training for the AWS CSA Exam

An interactive way of learning that will help you visualize core concepts so that you can be more effective when taking your AWS certification exam.  Built for students by a student to help them understand the concepts that they are being taught.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…
Suggested Courses

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question