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.
  • OriginalGriffO OriginalGriff

    Although you have two different variables declared as each of those name (one as a Label, one as a TextBox) I can't see anything immediately wrong with the code you show. So start by looking at which line is giving the error: double click the error message in VS and it'll take you right to it. Anything odd about that line? Or the lines immediately above? What concerns me slightly is that the code adding the parameters to your SqlCommand object in gvInternalAudit_RowUpdating is not indented correctly - which normally means that the curly brackets don't match somewhere or some similar error which means that the compiler doesn't understand the control flow properly. That can throw up a lot of "spurious" errors which disappear once the control flow is sorted. It's likely that your problem is something to do with that. Do a CTRL+K, CTRL+D to reformat the document and see if the indentation corrects itself. If it doesn't, then there is a serious problem elsewhere.

    Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...

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

    int lblQtr1 = int.Parse(Q1IntProc.Text) + int.Parse(Q1IntProj.Text) + int.Parse(Q1ExtBSI.Text) + int.Parse(Q1ExtCMMI.Text); This line is giving me the error:

    1 Reply Last reply
    0
    • N Norris Chappell

      Posted on 17 Jun 2015 Requirement Year Qtr.1 Q1IntProc Q1IntProj CMMICAMSG1 2015 7 4 1 Q1ExtBSI Q1ExtCMMI 0 2 I am trying to sum several fields into one. This code is giving me the I am getting the Object reference not set to an instance of an object error here: int lblQtr1 = int.Parse(Q1IntProc.Text) + int.Parse(Q1IntProj.Text) + int.Parse(Q1ExtBSI.Text) + int.Parse(Q1ExtCMMI.Text); protected void gvInternalAudit_RowDataBound(object sender, GridViewRowEventArgs e) { if (e.Row.RowType == DataControlRowType.DataRow) { Label Qtr1 = (Label)e.Row.FindControl("lblQtr1"); Label Q1IntProc = (Label)e.Row.FindControl("lblQ1IntProc"); Label Q1IntProj = (Label)e.Row.FindControl("lblQ1IntProj"); Label Q1ExtBSI = (Label)e.Row.FindControl("lblQ1ExtBSI"); Label Q1ExtCMMI = (Label)e.Row.FindControl("lblQ1ExtCMMI"); int lblQtr1 = int.Parse(Q1IntProc.Text) + int.Parse(Q1IntProj.Text) + int.Parse(Q1ExtBSI.Text) + int.Parse(Q1ExtCMMI.Text); Qtr1.Text = lblQtr1.ToString(); } } Here is all of my code:

      using System;
      using System.Data;
      using System.Collections.Generic;
      using System.Linq;
      using System.Web;
      using System.Drawing;
      using System.Web.UI;
      using System.Web.UI.WebControls;
      using System.Data.Sql;
      using System.Data.SqlClient;
      using System.Configuration;
      using System.Web.UI.WebControls.WebParts;

      namespace HIMSLA.InternalAudit
      {
      public partial class InternalAuditUserControl : UserControl
      {
      public SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["SQLMedTestConnHIM"].ConnectionString);
      SqlCommand cmd = new SqlCommand();
      protected void Page_Load(object sender, EventArgs e)
      {
      lblMsg.Text = "";
      if (!Page.IsPostBack)
      {
      BindSubjectData();
      }
      }

          //call to bind gridview
          protected void BindSubjectData()
          {
              using (SqlConnection sqlCon = new SqlConnection(ConfigurationManager.ConnectionStrings\["SQLMedTestConnHIM"\].ConnectionString))
              {
                  using (SqlCommand cmd = new SqlCommand())
                  {
                      cmd.CommandText = "SELECT \* FROM InternalAudit  order by Requirement";
                      c
      
      A Offline
      A Offline
      Agent__007
      wrote on last edited by
      #4

      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 1 Reply Last reply
      0
      • OriginalGriffO OriginalGriff

        Although you have two different variables declared as each of those name (one as a Label, one as a TextBox) I can't see anything immediately wrong with the code you show. So start by looking at which line is giving the error: double click the error message in VS and it'll take you right to it. Anything odd about that line? Or the lines immediately above? What concerns me slightly is that the code adding the parameters to your SqlCommand object in gvInternalAudit_RowUpdating is not indented correctly - which normally means that the curly brackets don't match somewhere or some similar error which means that the compiler doesn't understand the control flow properly. That can throw up a lot of "spurious" errors which disappear once the control flow is sorted. It's likely that your problem is something to do with that. Do a CTRL+K, CTRL+D to reformat the document and see if the indentation corrects itself. If it doesn't, then there is a serious problem elsewhere.

        Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...

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

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

        P 1 Reply Last reply
        0
        • 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