Solved

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

Posted on 2013-12-24
7
638 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:JonDavid
  • 6
7 Comments
 

Author Comment

by:JonDavid
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:JonDavid
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:JonDavid
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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
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:
JonDavid 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:JonDavid
ID: 39868037
Project is closed and passed UAT.  Will go into production next week.
0
 

Author Closing Comment

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

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

746 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now