Solved

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

Posted on 2004-09-02
3
975 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

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Suggested Solutions

Article by: Najam
Having new technologies does not mean they will completely replace old components.  Recently I had to create WCF that will be called by VB6 component.  Here I will describe what steps one should follow while doing so, please feel free to post any qu…
Introduction Although it is an old technology, serial ports are still being used by many hardware manufacturers. If you develop applications in C#, Microsoft .NET framework has SerialPort class to communicate with the serial ports.  I needed to…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

744 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