Solved

Can't get DefaultView.Find to work like I want it...

Posted on 2004-09-02
3
977 Views
Last Modified: 2008-01-09
Hello experts,

I am working on a basic time card system that is linked to a trouble ticket submission system.  This morning i discovered (much to my chagrin) that there is some bug going on in the time card calculation method.  I am trying to use the DefaultView.Find method to check if an existing account code is already in a datatable.  If the account code is in the table, I merely want to add the accompanying value (number of hours) to the account in the Datatable instead of adding a whole new row.  To try and explain this better I want to see:

Acct1      2.5 hours
Acct2      3 Hours

Instead of

Acct1  2 hours
Acct2  3 hours
Acct1 .5 hours

I have some code which catches when there is a duplicate in the DB - but when I go to add it in the hours value is often put in the wrong area (generally column 0 row 0).  I would think that my code is totally wrong except sometimes - it works :/  Here is a snippet.

                //Call a stored procedure to add this Trouble Ticket to the database
      SqlCommand cmd = new SqlCommand("TURK.dbo.sp_GetHours", conn.Connection);
      cmd.CommandType = CommandType.StoredProcedure;
      SqlDataReader reader = null;
                        
      //Set up the DataTable with the display names for the Columns
      DataTable myTable = new DataTable("troubleTickets");
      myTable.Columns.Add("Grant Number");
      myTable.Columns.Add("Hours");
                        
      DataColumn[] colPk = new DataColumn[1];
      colPk[0] = myTable.Columns["Grant Number"];
      myTable.PrimaryKey = colPk;
      myTable.DefaultView.Sort = "Grant Number";
      
                ...            

while (reader.Read())
     {
                string[] myStringArray = new string[2];
      decimal totalHours = reader.GetDecimal(1);
      //Makes sure something is in the grants charged area
      if(reader.GetString(2).Length > 0)
     {
      //gets information for the specific trouble ticket bill time, total hours,
      //and percent charged that corresponds with the correct grant
      decimal percentCharged = (reader.GetDecimal(6));
      decimal billTime = CalculateHours(percentCharged,totalHours);
                                    
      //Set up the array to hold values to input into the dataTable
      //Pulls values from reader
      myStringArray[0] = reader.GetString(2);
      string showMe = reader.GetString(2);
      myStringArray[1] = System.Convert.ToString(billTime);
                                    
      //Check if row exists, if no row exists add a new row to the existing
      //DataTable, otherwise, add the extra time charged to the existing DataRow
      //"TimeCharged" column
      DataRow drFound;
                                          
      // Find the Row specified in txtFindArg
      drFound = myTable.Rows.Find(reader.GetString(2));
      if (drFound == null)
      {
            //No row with the same PK exists, add a new row to DT
            myTable.Rows.Add(myStringArray);
      }
      
else
      {
      //Otherwise, get the TimeCharged in the DataTable, and add it to
      //the TimeCharged in the reader
      int duplicateRow;
      decimal oldValue;
      decimal newValue;
                                                
      duplicateRow= myTable.DefaultView.Find(reader.GetString(2));
      //Grab the next three values for error checking - see what they are.
                string showMeHere = reader.GetString(2);
      string showMore = System.Convert.ToString(myTable.Rows[whoopie][0]);
      string evenMore = System.Convert.ToString(myTable.Rows[whoopie][1]);
      //end data checking
                oldValue = System.Convert.ToDecimal(myTable.Rows[duplicateRow][1]);
      newValue = System.Convert.ToDecimal(myStringArray[1]);
      newValue += oldValue;
      myTable.Rows[duplicateRow].BeginEdit();
      myTable.Rows[duplicateRow][1] = (System.Convert.ToString(newValue));
      myTable.Rows[duplicateRow].EndEdit();
      }                                    
      }

Sometimes the duplicateRow is the correct number, and adds the corresponding values accordingly.  Other times it is wrong and adds it to other columns (one of which is account code "none" - thank you users :P )

I will be glad to provide any more information you see fit.  Sorry for the long post wanted to be as specific as possible.
TIA
0
Comment
Question by:RuralInstitute
  • 2
3 Comments
 
LVL 9

Expert Comment

by:Rodney Helsens
ID: 11967605
why are you using the reader.GetString(2) and reader.GetDecimal(6) etc..? This sure makes it hard are hell to tell what we are looking at..

Have you seen this syntax reader["FieldName"]?

Sorry althought, I can't really help point out where this issue lies in your code, but it would seem that it should be possible to extract the data the way you want using GROUP BY and SUM in SQL server and save yourself alot of this jumping through hoops.


0
 

Author Comment

by:RuralInstitute
ID: 11967888
So summing is a good idea - don't know why I didn't do that a few months ago in the first place...I like to make things difficult i guess.

As for the GetString(2) etc, those are the corresponding columns in the current selection of the datareader.  GetString(2) would be the AccountCode to check against/add to the datatable.

Going to SQL Land right now.
0
 
LVL 9

Accepted Solution

by:
Rodney Helsens earned 500 total points
ID: 11968016
Well just a suggestion.. if you have trouble, you can get expert help over in the Sql Server area to get this data returned to you exactly as you want it...  

I guess that reader.getString() is just a preference, I guess i was just saying it's makes the code more readable when doing reader["AccountCode"] vs. reader.GetString(2)

good luck
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying 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

Extention Methods in C# 3.0 by Ivo Stoykov C# 3.0 offers extension methods. They allow extending existing classes without changing the class's source code or relying on inheritance. These are static methods invoked as instance method. This…
This article describes a simple method to resize a control at runtime.  It includes ready-to-use source code and a complete sample demonstration application.  We'll also talk about C# Extension Methods. Introduction In one of my applications…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

821 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