Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C#
  4. Trying to sum several fields into a total field

Trying to sum several fields into a total field

Scheduled Pinned Locked Moved C#
csharpdatabaselinqgraphicsdesign
15 Posts 5 Posters 2 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Agent__007

    A quick check would be - just put a debug point on the line throwing the error, and hover (move your mouse pointer over) Q1IntProc, Q1IntProj, Q1ExtBSI and Q1ExtCMMI one by one (or with a quick watch (right click -> Quick Watch)) and check which one's null. There must be (atleast) one of them which is null. If so, look at corresponding "FindControl" statement and check if the string param you are specifying for the "FindControl" call ("txtQ1IntProc", for instance) does actually represent a valid TextBox in your (GridView's) row. Hope that helps.

    You have just been Sharapova'd.

    N Offline
    N Offline
    Norris Chappell
    wrote on last edited by
    #6

    I did that and all of them are null. aspx: I changed to my code to TextBox but still getting the null errors: Qtr1 value is 0 the other 4 are null. :(

    L A 2 Replies Last reply
    0
    • N Norris Chappell

      I did that and all of them are null. aspx: I changed to my code to TextBox but still getting the null errors: Qtr1 value is 0 the other 4 are null. :(

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #7

      The ids of the textboxes in your ASP file do not (as far as I can see) match the names you are using in your code. Also coding a line such as

      int lblQtr1 = int.Parse(Q1IntProc.Text) + int.Parse(Q1IntProj.Text) + int.Parse(Q1ExtBSI.Text) + int.Parse(Q1ExtCMMI.Text);

      is asking for trouble. You have no idea what may be in any of the text fields, so if the user enters garbage it will just fail. You shoud use int.TryParse to capture the numeric values, so you can alert the user if any is invalid. You should also assign each value to an individual variable before summing them. That way, you can debug it much more easily.

      1 Reply Last reply
      0
      • N Norris Chappell

        The qtr1 field is 0. However the other 4 fields are showing in debug a Null value.

        P Offline
        P Offline
        Pete OHanlon
        wrote on last edited by
        #8

        Null is not an integer value. This is why it's failing - as a plain int is not a nullable type, you cannot just parse it, you should actually use TryParse instead. You need to break your logic up into something that looks a little bit like this:

        //int lblQtr1 = int.Parse(Q1IntProc.Text) + int.Parse(Q1IntProj.Text) + int.Parse(Q1ExtBSI.Text) + int.Parse(Q1ExtCMMI.Text);
        private int GetValue(TextBox textBox)
        {
        int output;
        if (int.TryParse(textBox.Text, out output))
        {
        return output;
        }
        return 0;
        }
        int lblQtr1 = GetValue(Q1IntProc) + GetValue(Q1IntProj) + GetValue(Q1ExtBSI) + GetValue(Q1ExtCMMI);

        N 3 Replies Last reply
        0
        • N Norris Chappell

          I did that and all of them are null. aspx: I changed to my code to TextBox but still getting the null errors: Qtr1 value is 0 the other 4 are null. :(

          A Offline
          A Offline
          Agent__007
          wrote on last edited by
          #9

          Norris Chappell wrote:

          I changed to my code to TextBox but still getting the null errors:

          Yes, that should be Label only and not TextBox, my mistake. Not sure what's wrong here. But in your ASP markup, are you missing an <ItemTemplate> wrapped around the controls? You can also try finding the control in a "Cell" (like row.Cells[N].FindControl(), where N-> index of the container cell). Not sure it's related, but you can also check the actual IDs of the controls after they are rendered and there's a property "ClientIDMode"(?) of GridView (not sure but I remember to have faced something similar a few years ago when I worked on ASP.NET WebForms) which you can use if there's a mismatch. Good luck!

          You have just been Sharapova'd.

          N 1 Reply Last reply
          0
          • A Agent__007

            Norris Chappell wrote:

            I changed to my code to TextBox but still getting the null errors:

            Yes, that should be Label only and not TextBox, my mistake. Not sure what's wrong here. But in your ASP markup, are you missing an <ItemTemplate> wrapped around the controls? You can also try finding the control in a "Cell" (like row.Cells[N].FindControl(), where N-> index of the container cell). Not sure it's related, but you can also check the actual IDs of the controls after they are rendered and there's a property "ClientIDMode"(?) of GridView (not sure but I remember to have faced something similar a few years ago when I worked on ASP.NET WebForms) which you can use if there's a mismatch. Good luck!

            You have just been Sharapova'd.

            N Offline
            N Offline
            Norris Chappell
            wrote on last edited by
            #10

            Hi Everyone I finally figured it out. I didn't need to do it in C# after all. I modified my SQL to sum the fields I needed. I want to thank everyone that contributed.

            1 Reply Last reply
            0
            • P Pete OHanlon

              Null is not an integer value. This is why it's failing - as a plain int is not a nullable type, you cannot just parse it, you should actually use TryParse instead. You need to break your logic up into something that looks a little bit like this:

              //int lblQtr1 = int.Parse(Q1IntProc.Text) + int.Parse(Q1IntProj.Text) + int.Parse(Q1ExtBSI.Text) + int.Parse(Q1ExtCMMI.Text);
              private int GetValue(TextBox textBox)
              {
              int output;
              if (int.TryParse(textBox.Text, out output))
              {
              return output;
              }
              return 0;
              }
              int lblQtr1 = GetValue(Q1IntProc) + GetValue(Q1IntProj) + GetValue(Q1ExtBSI) + GetValue(Q1ExtCMMI);

              N Offline
              N Offline
              Norris Chappell
              wrote on last edited by
              #11

              I getting that error that The name 'Q1IntProc' does not exist in the current context. So I should have an if statement for each field that I am summing? So what goes here textBox.Text?

              L 1 Reply Last reply
              0
              • P Pete OHanlon

                Null is not an integer value. This is why it's failing - as a plain int is not a nullable type, you cannot just parse it, you should actually use TryParse instead. You need to break your logic up into something that looks a little bit like this:

                //int lblQtr1 = int.Parse(Q1IntProc.Text) + int.Parse(Q1IntProj.Text) + int.Parse(Q1ExtBSI.Text) + int.Parse(Q1ExtCMMI.Text);
                private int GetValue(TextBox textBox)
                {
                int output;
                if (int.TryParse(textBox.Text, out output))
                {
                return output;
                }
                return 0;
                }
                int lblQtr1 = GetValue(Q1IntProc) + GetValue(Q1IntProj) + GetValue(Q1ExtBSI) + GetValue(Q1ExtCMMI);

                N Offline
                N Offline
                Norris Chappell
                wrote on last edited by
                #12

                Hi, I not sure how to use this code? Please let me know if I need to make changes to it?

                1 Reply Last reply
                0
                • N Norris Chappell

                  I getting that error that The name 'Q1IntProc' does not exist in the current context. So I should have an if statement for each field that I am summing? So what goes here textBox.Text?

                  L Offline
                  L Offline
                  Lost User
                  wrote on last edited by
                  #13

                  I already suggested that you check the variable names that you are using. They appear not to match the names you have set in your ASP page.

                  N 1 Reply Last reply
                  0
                  • L Lost User

                    I already suggested that you check the variable names that you are using. They appear not to match the names you have set in your ASP page.

                    N Offline
                    N Offline
                    Norris Chappell
                    wrote on last edited by
                    #14

                    It is what I have in my asp. Did I not set it properly?

                    1 Reply Last reply
                    0
                    • P Pete OHanlon

                      Null is not an integer value. This is why it's failing - as a plain int is not a nullable type, you cannot just parse it, you should actually use TryParse instead. You need to break your logic up into something that looks a little bit like this:

                      //int lblQtr1 = int.Parse(Q1IntProc.Text) + int.Parse(Q1IntProj.Text) + int.Parse(Q1ExtBSI.Text) + int.Parse(Q1ExtCMMI.Text);
                      private int GetValue(TextBox textBox)
                      {
                      int output;
                      if (int.TryParse(textBox.Text, out output))
                      {
                      return output;
                      }
                      return 0;
                      }
                      int lblQtr1 = GetValue(Q1IntProc) + GetValue(Q1IntProj) + GetValue(Q1ExtBSI) + GetValue(Q1ExtCMMI);

                      N Offline
                      N Offline
                      Norris Chappell
                      wrote on last edited by
                      #15

                      Pete, I not for sure how to use this code. Can you let me know what I need to do? I still getting the error for the 4 fields. The fields do not exist in the current context.

                      1 Reply Last reply
                      0
                      Reply
                      • Reply as topic
                      Log in to reply
                      • Oldest to Newest
                      • Newest to Oldest
                      • Most Votes


                      • Login

                      • Don't have an account? Register

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • World
                      • Users
                      • Groups