Solved

Remove row from data table (hash table) C#  vs2010

Posted on 2013-12-24
7
662 Views
Last Modified: 2014-02-23
Background:  I am working on an asp.net website (intranet) using c# that employs proprietary web methods to download students exam material from a hosted web site.  
I have no control over what gets downloaded from a hosted site.  I do have control over what is displayed.   I am displaying an exam details page that opens from a link on a course page.   The problem is the hosted site is not filtering out previous exam results (for same exam) which is bad for me.   I am comparing dates and attempting to delete the older rows.  My aim is to display only one exam.  
I need help with the syntax to delete a row from a dataset, or I can select a row to publish to the databound fields on the html page.  I feel the later might be harder than just deleting the row.

ds is the dataset downloaded from hosted website, and configured with xml stylesheet.
Table:  Lesson
Column which compare is made:  LessonStatusDate

Code:
 if (lesson.Rows.Count > 1)
            {
                //Keep the most recent row
                lessonCount = 0;
                foreach (DataRow lessonRow in ds.Tables["Lesson"].Rows)
                {
                    //C# complains if I don't assign a value.  The property "Now" is not relevant to anything, it's just a value.
                    DateTime dt1 = DateTime.Now;

                    DateTime dt = Convert.ToDateTime(ds.Tables["Lesson"].Rows[lessonCount]["LessonStatusDate"]);
                    if (lessonCount == 0)
                    {
                        dt1 = dt.Date;
                    }
                    if ((lessonCount > 1) && (dt.Date > dt1.Date))
                    {
                        //Delete dt1.Date
                       dt1=dt.Date
                    }
                    lessonCount++;
                }
            }


Thanks in advance for your help.   ...and yes, some of us used up all our vacation days and are working on Christmas Eve.  ;-(
0
Comment
Question by:Jon Davidson
[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
  • 6
7 Comments
 

Author Comment

by:Jon Davidson
ID: 39738227
I have made progress, but am now deleting the wrong date:
if (lesson.Rows.Count > 1)
            {anything, it's just a value.
                DateTime dt1 = DateTime.Now;
                //Keep the most recent row
                lessonCount = 0;
                //C# complains if I don't assign a value.  The property "Now" is not relevant to
 foreach (DataRow lessonRow in ds.Tables["Lesson"].Rows)
                {
                    DateTime dt = Convert.ToDateTime(ds.Tables["Lesson"].Rows[lessonCount]["LessonStatusDate"]);
                    if (lessonCount == 0)
                    {
                        dt1 = dt.Date;
                    }
                    if ((lessonCount >= 1) && (dt.Date > dt1.Date))
                    {
                        ds.Tables["Lesson"].Rows[lessonCount].Delete();
                        dt1 = dt.Date;
                    }

                    if (lessonCount == ds.Tables["Lesson"].Rows.Count)
                    {
                        break;
                    }
                    lessonCount++;
                }
          }
0
 

Author Comment

by:Jon Davidson
ID: 39738260
This is my third time through, and it works.   Please comment if there is a better way to do this:
if (lesson.Rows.Count > 1)
            {
                //Keep the most recent row
                lessonCount = 0;
                //C# complains if I don't assign a value.  The property "Now" is not relevant to anything, it's just a value.
                DateTime dt1 = DateTime.Now;
                foreach (DataRow lessonRow in ds.Tables["Lesson"].Rows)
                {
                    DateTime dt = Convert.ToDateTime(ds.Tables["Lesson"].Rows[lessonCount]["LessonStatusDate"]);
                    if (lessonCount == 0)
                    {
                        dt1 = dt.Date;
                    }
                    if ((lessonCount >= 1) && (dt1.Date > dt.Date))
                    {
                        ds.Tables["Lesson"].Rows[lessonCount].Delete();
                        dt1 = dt.Date;
                    }
                    else if((lessonCount >=1) && (dt1.Date < dt.Date))
                    {
                        ds.Tables["Lesson"].Rows[lessonCount-1].Delete();
                    }

                    if (lessonCount == ds.Tables["Lesson"].Rows.Count)
                    {
                        break;
                    }
                    lessonCount++;
                }
            }
0
 

Author Comment

by:Jon Davidson
ID: 39738298
Found on potential error.  Should I ever see three rows, I needed to set the dt1 to the max value.   My original request was for help with the syntax.  This code works, but I think it can be done much better.  Would appreciate any help I can get on this matter.

 if (lesson.Rows.Count > 1)
            {
                //Keep the most recent row, delete all others.
                lessonCount = 0;
                //C# complains if I don't assign a value.  The property "Now" is not relevant to anything, it's just a value.
                DateTime dt1 = DateTime.Now;
                foreach (DataRow lessonRow in ds.Tables["Lesson"].Rows)
                {
                    DateTime dt = Convert.ToDateTime(ds.Tables["Lesson"].Rows[lessonCount]["LessonStatusDate"]);
                    if (lessonCount == 0)
                    {
                        dt1 = dt.Date;
                    }
                    if ((lessonCount >= 1) && (dt1.Date > dt.Date))
                    {
                        ds.Tables["Lesson"].Rows[lessonCount].Delete();
                    }
                    else if((lessonCount >=1) && (dt1.Date < dt.Date))
                    {
                        ds.Tables["Lesson"].Rows[lessonCount-1].Delete();
                        dt1 = dt.Date;
                    }
                   
                    if (lessonCount == ds.Tables["Lesson"].Rows.Count)
                    {
                        break;
                    }
                    lessonCount++;
                }
            }
                       }
0
PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

 
LVL 10

Expert Comment

by:Mathiyazhagan
ID: 39738880
Hi Jon,
It can be easily done using Linq where we can apply group by and order by  as sql server.
please find following code for this :
DataTable lession = new DataTable("lession");
            lession.Columns.Add(new DataColumn("LessionId", typeof(string)));
            lession.Columns.Add(new DataColumn("LessonStatusDate", typeof(DateTime)));
            lession.Columns.Add(new DataColumn("LessonGrade", typeof(string)));
           
            //add records for lession 1
            lession.Rows.Add(new object[] { 1, "12/12/13","A" });
            lession.Rows.Add(new object[] { 1, "12/10/13","B" });
            lession.Rows.Add(new object[] { 1, "12/22/13","A" });
            //add records for lession 2
            lession.Rows.Add(new object[] { 2, "12/15/13","B" });
            lession.Rows.Add(new object[] { 2, "11/22/13","A" });
            //add records for lession 3
            lession.Rows.Add(new object[] { 3, "12/18/13","O" });
            lession.Rows.Add(new object[] { 2, "12/05/13" ,"A"});
           
            //add datatable to dataset
            ds.Tables.Add(lession);


            // group by lession id and order by LessonStatusDate descending and add first record of each group to result table
            DataTable dt = ds.Tables["lession"].AsEnumerable()
                .GroupBy(r => new { Col1 = r["LessionId"] }) 
                .Select(g => g.OrderByDescending(r => r["LessonStatusDate"]).First()).CopyToDataTable();  

Open in new window


now, in datatatble dt will be having filtered recordset for each lession.hope this helps. Merry Christmas !!!
0
 

Accepted Solution

by:
Jon Davidson earned 0 total points
ID: 39742024
I can see where this would be helpful.   Since all of the data is downloaded through XML style sheets, and a string reader, I would to make a few changes, but overall, this would give more lattitude in manipulated style, and content to the page.   Thanks!  Hope you had a Merry Christmas.
0
 

Author Comment

by:Jon Davidson
ID: 39868037
Project is closed and passed UAT.  Will go into production next week.
0
 

Author Closing Comment

by:Jon Davidson
ID: 39880360
The answer given by the previous person required more time than was alotted in project.
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …

622 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