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
  1. Home
  2. General Programming
  3. C#
  4. How to get a value to return in a textbox

How to get a value to return in a textbox

Scheduled Pinned Locked Moved C#
databasehelpquestionxmltutorial
9 Posts 5 Posters 1 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.
  • J Offline
    J Offline
    Justiin1265
    wrote on last edited by
    #1

    Hi, I am having issues for a project for work and was wondering if anybody can help me. I am trying to calculate The percentage for refurb for the day. The formula is Refurb_Rate = (totalRefurb / totalUnits * 100) The program is supposed to supposed count the refurb in the db and return a value in a textbox. I initialized my values to 0, but my problem is how do I get it to calculate each time a user scans in a unit to be refurbed. Anything I try to do gives me an error in my program. Right now it's returning a value of of 1 because I added RefurbRate++. Can anyone help me? Thanks. Justin Here is what I've done with my code: private int GetRefurbRate() { string sql = ""; int Refurb_Rate = 0; int totalRefurb = 0; int totalUnits = 0; string error_msg = ""; sql = "SELECT COUNT(*) " + "FROM " + schema + ".repair_part rp " + "WHERE rp.repair_ord = '" + txtRO.Text + "' "; while (true) { if (!myDb.RunSql(sql, true)) { error_msg = "DBError for getting Refurb Rate"; break; } if (myDb.dbRdr.HasRows) { if (myDb.dbRdr.Read()) { try { Refurb_Rate = (totalRefurb / totalUnits * 100); } catch (Exception e) { Console.WriteLine(e); } } Refurb_Rate++; } break; } myDb.dbRdr.Close(); if (error_msg != String.Empty) { MessageBox.Show(error_msg, "Get Refurb Rate", MessageBoxButtons.OK, MessageBoxIcon.Asterisk); } return Refurb_Rate;

    C L L 3 Replies Last reply
    0
    • J Justiin1265

      Hi, I am having issues for a project for work and was wondering if anybody can help me. I am trying to calculate The percentage for refurb for the day. The formula is Refurb_Rate = (totalRefurb / totalUnits * 100) The program is supposed to supposed count the refurb in the db and return a value in a textbox. I initialized my values to 0, but my problem is how do I get it to calculate each time a user scans in a unit to be refurbed. Anything I try to do gives me an error in my program. Right now it's returning a value of of 1 because I added RefurbRate++. Can anyone help me? Thanks. Justin Here is what I've done with my code: private int GetRefurbRate() { string sql = ""; int Refurb_Rate = 0; int totalRefurb = 0; int totalUnits = 0; string error_msg = ""; sql = "SELECT COUNT(*) " + "FROM " + schema + ".repair_part rp " + "WHERE rp.repair_ord = '" + txtRO.Text + "' "; while (true) { if (!myDb.RunSql(sql, true)) { error_msg = "DBError for getting Refurb Rate"; break; } if (myDb.dbRdr.HasRows) { if (myDb.dbRdr.Read()) { try { Refurb_Rate = (totalRefurb / totalUnits * 100); } catch (Exception e) { Console.WriteLine(e); } } Refurb_Rate++; } break; } myDb.dbRdr.Close(); if (error_msg != String.Empty) { MessageBox.Show(error_msg, "Get Refurb Rate", MessageBoxButtons.OK, MessageBoxIcon.Asterisk); } return Refurb_Rate;

      C Offline
      C Offline
      Colin Angus Mackay
      wrote on last edited by
      #2

      Okay - First thing is that your code is susceptible to a SQL Injection Attack. Here are a bunch of things I've blogged about SQL Injection Attacks[^] and I've also got an article about it here on Code Project here: SQL Injection Attacks and Some Tips on How to Prevent Them[^]

      Justiin1265 wrote:

      Anything I try to do gives me an error in my program.

      What is the error you are getting?

      User group: Scottish Developers Blog: The Blog of Colin Angus Mackay Quote: Man who stand on hill with mouth open wait long time for roast duck to drop in.

      J 1 Reply Last reply
      0
      • J Justiin1265

        Hi, I am having issues for a project for work and was wondering if anybody can help me. I am trying to calculate The percentage for refurb for the day. The formula is Refurb_Rate = (totalRefurb / totalUnits * 100) The program is supposed to supposed count the refurb in the db and return a value in a textbox. I initialized my values to 0, but my problem is how do I get it to calculate each time a user scans in a unit to be refurbed. Anything I try to do gives me an error in my program. Right now it's returning a value of of 1 because I added RefurbRate++. Can anyone help me? Thanks. Justin Here is what I've done with my code: private int GetRefurbRate() { string sql = ""; int Refurb_Rate = 0; int totalRefurb = 0; int totalUnits = 0; string error_msg = ""; sql = "SELECT COUNT(*) " + "FROM " + schema + ".repair_part rp " + "WHERE rp.repair_ord = '" + txtRO.Text + "' "; while (true) { if (!myDb.RunSql(sql, true)) { error_msg = "DBError for getting Refurb Rate"; break; } if (myDb.dbRdr.HasRows) { if (myDb.dbRdr.Read()) { try { Refurb_Rate = (totalRefurb / totalUnits * 100); } catch (Exception e) { Console.WriteLine(e); } } Refurb_Rate++; } break; } myDb.dbRdr.Close(); if (error_msg != String.Empty) { MessageBox.Show(error_msg, "Get Refurb Rate", MessageBoxButtons.OK, MessageBoxIcon.Asterisk); } return Refurb_Rate;

        L Offline
        L Offline
        Luc Pattyn
        wrote on last edited by
        #3

        please edit your message and use PRE tags for code snippets; it is too hard to read as is.

        Justiin1265 wrote:

        how do I get it to calculate each time a user scans in a unit

        what do you mean by scan? is there a bar code reader involved? if so what data is it providing? should that be added to the database too? the situation isn't clear to me at all. :)

        Luc Pattyn [Forum Guidelines] [My Articles] Nil Volentibus Arduum

        Please use <PRE> tags for code snippets, they preserve indentation, improve readability, and make me actually look at the code.

        J 1 Reply Last reply
        0
        • J Justiin1265

          Hi, I am having issues for a project for work and was wondering if anybody can help me. I am trying to calculate The percentage for refurb for the day. The formula is Refurb_Rate = (totalRefurb / totalUnits * 100) The program is supposed to supposed count the refurb in the db and return a value in a textbox. I initialized my values to 0, but my problem is how do I get it to calculate each time a user scans in a unit to be refurbed. Anything I try to do gives me an error in my program. Right now it's returning a value of of 1 because I added RefurbRate++. Can anyone help me? Thanks. Justin Here is what I've done with my code: private int GetRefurbRate() { string sql = ""; int Refurb_Rate = 0; int totalRefurb = 0; int totalUnits = 0; string error_msg = ""; sql = "SELECT COUNT(*) " + "FROM " + schema + ".repair_part rp " + "WHERE rp.repair_ord = '" + txtRO.Text + "' "; while (true) { if (!myDb.RunSql(sql, true)) { error_msg = "DBError for getting Refurb Rate"; break; } if (myDb.dbRdr.HasRows) { if (myDb.dbRdr.Read()) { try { Refurb_Rate = (totalRefurb / totalUnits * 100); } catch (Exception e) { Console.WriteLine(e); } } Refurb_Rate++; } break; } myDb.dbRdr.Close(); if (error_msg != String.Empty) { MessageBox.Show(error_msg, "Get Refurb Rate", MessageBoxButtons.OK, MessageBoxIcon.Asterisk); } return Refurb_Rate;

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

          Justin, there are multiple things that mnake no sense to me about this, for example you seem to be doing the same query again and again in a loop and also performing what should be a final calculation within that loop without setting the variables, which I would think (quite apart from being illogical) immediately throws a divide by zero error. I'm sure we could provide a solution quickly if we could understand what you are trying to do more clearly. Which, in pseudocode, might be something like... foreach repair_part record where repair_ord = value scanned in by user increment totalRefurb and totalUnits (based on some fields in repair_part record) endforeach Refurb_Rate = totalRefurb / totalUnits Is that on the right track?

          J 1 Reply Last reply
          0
          • C Colin Angus Mackay

            Okay - First thing is that your code is susceptible to a SQL Injection Attack. Here are a bunch of things I've blogged about SQL Injection Attacks[^] and I've also got an article about it here on Code Project here: SQL Injection Attacks and Some Tips on How to Prevent Them[^]

            Justiin1265 wrote:

            Anything I try to do gives me an error in my program.

            What is the error you are getting?

            User group: Scottish Developers Blog: The Blog of Colin Angus Mackay Quote: Man who stand on hill with mouth open wait long time for roast duck to drop in.

            J Offline
            J Offline
            Justiin1265
            wrote on last edited by
            #5

            Anytime I try to make the variables double,, its giving me an error of can't convert int to dbl which I know, but I did not put a stipulation of only int on it; so I do not know why it is telling me that. I'm assuming another programmer put that stipulation somewhere else in the program, but Idk where, and I also do not want to mess up any coding they did.

            1 Reply Last reply
            0
            • L Luc Pattyn

              please edit your message and use PRE tags for code snippets; it is too hard to read as is.

              Justiin1265 wrote:

              how do I get it to calculate each time a user scans in a unit

              what do you mean by scan? is there a bar code reader involved? if so what data is it providing? should that be added to the database too? the situation isn't clear to me at all. :)

              Luc Pattyn [Forum Guidelines] [My Articles] Nil Volentibus Arduum

              Please use <PRE> tags for code snippets, they preserve indentation, improve readability, and make me actually look at the code.

              J Offline
              J Offline
              Justiin1265
              wrote on last edited by
              #6

              Yes, a barcode reader is involved, but does not need to be in the db. It is just a tool for users to process units faster. A user would scan in a unit(cable box) and if a part is damaged then it would be refurbed(replaced) and if not a user would enter None(no parts replaced). My formula calculates the toal units refurbed / total units that had a value of none* 100 to get a percentage. My problem is idk how to make the formula constantly change values as the total refurb and total units changes constantly.

              L 1 Reply Last reply
              0
              • L Lost User

                Justin, there are multiple things that mnake no sense to me about this, for example you seem to be doing the same query again and again in a loop and also performing what should be a final calculation within that loop without setting the variables, which I would think (quite apart from being illogical) immediately throws a divide by zero error. I'm sure we could provide a solution quickly if we could understand what you are trying to do more clearly. Which, in pseudocode, might be something like... foreach repair_part record where repair_ord = value scanned in by user increment totalRefurb and totalUnits (based on some fields in repair_part record) endforeach Refurb_Rate = totalRefurb / totalUnits Is that on the right track?

                J Offline
                J Offline
                Justiin1265
                wrote on last edited by
                #7

                I'm confused about how to make total units and total refurb values change constantly. That seems to be on the right track I was thinking about putting the formula in a for loop to see if that would work.

                R 1 Reply Last reply
                0
                • J Justiin1265

                  Yes, a barcode reader is involved, but does not need to be in the db. It is just a tool for users to process units faster. A user would scan in a unit(cable box) and if a part is damaged then it would be refurbed(replaced) and if not a user would enter None(no parts replaced). My formula calculates the toal units refurbed / total units that had a value of none* 100 to get a percentage. My problem is idk how to make the formula constantly change values as the total refurb and total units changes constantly.

                  L Offline
                  L Offline
                  Luc Pattyn
                  wrote on last edited by
                  #8

                  OK. It is a bit clearer now. Still not sure what it is that gets scanned (a serial number? a repair cost label?) Here is a simple scheme that is bound to work: - have a class member "int countAccepted", initially zero. - have a class member "int countRejected", initially zero. - have an "Accept" button, when clicked it increments countAccepted and calls method Calc(). - have a "Reject" button, when clicked it increments countRejected and calls method Calc().

                  public void btnAccepted_Clicked(object sender, EventArgs e) {
                  countAccepted++;
                  Calc();
                  }

                  public void btnRejected_Clicked(object sender, EventArgs e) {
                  countRejected++;
                  Calc();
                  }

                  public void Calc() {
                  int totalCount=countAccepted+countRejected;
                  if (totalCount!=0) {
                  int rejectedRate=100*countRejected/totalCount;
                  textBoxAccepted.Text=countAccepted.ToString();
                  textBoxRejected.Text=countRejected.ToString();
                  textBoxRejectedRate.Text=rejectedRate.ToString()+" %";
                  }
                  }

                  So the trick here is I'm using two buttons. If there were a TextBox that accepts the barcode reader's input, then the TextBox.TextChanged event handler would have to update the counters appropriately, then call Calc().

                  public void BarcodeReaderTextBox_TextChanged(object sender, EventArgs e) {
                  // first decide which counter to increment, depending on ...
                  ...
                  // then recalculate
                  Calc();
                  }

                  HTH Suggestion: if you manage to clearly state your problem in plain English, it will also be easier for yourself to turn it into working code. :)

                  Luc Pattyn [Forum Guidelines] [My Articles] Nil Volentibus Arduum

                  Please use <PRE> tags for code snippets, they preserve indentation, improve readability, and make me actually look at the code.

                  1 Reply Last reply
                  0
                  • J Justiin1265

                    I'm confused about how to make total units and total refurb values change constantly. That seems to be on the right track I was thinking about putting the formula in a for loop to see if that would work.

                    R Offline
                    R Offline
                    Rotted Frog
                    wrote on last edited by
                    #9

                    More clarity about how the "change" happens in the figures would be helpful. For example: 1. Are you tring to update the figures whenever a new item is scanned?, If so you need to catch the scanning event (typically this would be an OnKeyDown or OnTextChanged event in a TextBox somewhere? 2. Are there lots of different places where the figures may change (e.g. the are lots of different people scanning in refurbs in different places) and you want to constantly check the database. If so a solution where you check the database at regular intervals might be better (e.g. use a Timer control and put the code in there). Without knowing exactly what you are trying to achieve and the thought process behind it, it's difficult to work out the best solution.

                    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