Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

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

Posted on 2013-12-24
7
Medium Priority
?
694 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
  • 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
Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

 
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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

High user turnover can cause old/redundant user data to consume valuable space. UserResourceCleanup was developed to address this by automatically deleting user folders when the user account is deleted.
Hello there! As a developer I have modified and refactored the unit tests which was written by fellow developers in the past. On the course, I have gone through various misconceptions and technical challenges when it comes to implementation. I would…
In response to a need for security and privacy, and to continue fostering an environment members can turn to for support, solutions, and education, Experts Exchange has created anonymous question capabilities. This new feature is available to our Pr…
Look below the covers at a subform control , and the form that is inside it. Explore properties and see how easy it is to aggregate, get statistics, and synchronize results for your data. A Microsoft Access subform is used to show relevant calcul…
Suggested Courses

879 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