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. Need Help

Need Help

Scheduled Pinned Locked Moved C#
help
6 Posts 6 Posters 0 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.
  • M Offline
    M Offline
    mjawadkhatri
    wrote on last edited by
    #1

    Hi. please tell me my mistake in blow code,code not return any value.. please help string query2 = "select sum(amount) Total from pur_inv_dtl where inv_id = '" + IdTxt.Text + "'"; SqlCommand cmd2 = new SqlCommand(query2, conn); try { conn.Open(); SqlDataReader sdr = cmd2.ExecuteReader(); while (sdr.Read()) { Gtotal.Text = sdr["Total"].ToString(); } } finally { conn.Close(); }

    O K OriginalGriffO L P 5 Replies Last reply
    0
    • M mjawadkhatri

      Hi. please tell me my mistake in blow code,code not return any value.. please help string query2 = "select sum(amount) Total from pur_inv_dtl where inv_id = '" + IdTxt.Text + "'"; SqlCommand cmd2 = new SqlCommand(query2, conn); try { conn.Open(); SqlDataReader sdr = cmd2.ExecuteReader(); while (sdr.Read()) { Gtotal.Text = sdr["Total"].ToString(); } } finally { conn.Close(); }

      O Offline
      O Offline
      OkkiePepernoot
      wrote on last edited by
      #2

      Not sure of all the types of the fields but try this string query2 = "select sum(amount) AS Total from pur_inv_dtl where inv_id = '" + IdTxt.Text + "'";

      1 Reply Last reply
      0
      • M mjawadkhatri

        Hi. please tell me my mistake in blow code,code not return any value.. please help string query2 = "select sum(amount) Total from pur_inv_dtl where inv_id = '" + IdTxt.Text + "'"; SqlCommand cmd2 = new SqlCommand(query2, conn); try { conn.Open(); SqlDataReader sdr = cmd2.ExecuteReader(); while (sdr.Read()) { Gtotal.Text = sdr["Total"].ToString(); } } finally { conn.Close(); }

        K Offline
        K Offline
        karmjit435
        wrote on last edited by
        #3

        While reading the value from textbox, Trim the blank spaces. That may cause some problem.

        1 Reply Last reply
        0
        • M mjawadkhatri

          Hi. please tell me my mistake in blow code,code not return any value.. please help string query2 = "select sum(amount) Total from pur_inv_dtl where inv_id = '" + IdTxt.Text + "'"; SqlCommand cmd2 = new SqlCommand(query2, conn); try { conn.Open(); SqlDataReader sdr = cmd2.ExecuteReader(); while (sdr.Read()) { Gtotal.Text = sdr["Total"].ToString(); } } finally { conn.Close(); }

          OriginalGriffO Offline
          OriginalGriffO Offline
          OriginalGriff
          wrote on last edited by
          #4

          To add to the previous - don't do it that way anyway. Use parametrised queries as it gives better security agains SQL Injection Attacks. (See SqlCommand.AddWithValue) Oh, and if you post a code fragment again, surround it with the "code block" widget to preserve teh formatting:

          string query2 = "select sum(amount) Total from pur_inv_dtl where inv_id = '" + IdTxt.Text + "'";

          SqlCommand cmd2 = new SqlCommand(query2, conn);
          try
          {
          conn.Open();
          SqlDataReader sdr = cmd2.ExecuteReader();
          while (sdr.Read())
          {
          Gtotal.Text = sdr["Total"].ToString();
          }
          }
          finally
          {
          conn.Close();
          }

          It makes things so much eaasier to read!

          Did you know: That by counting the rings on a tree trunk, you can tell how many other trees it has slept with.

          "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
          "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

          1 Reply Last reply
          0
          • M mjawadkhatri

            Hi. please tell me my mistake in blow code,code not return any value.. please help string query2 = "select sum(amount) Total from pur_inv_dtl where inv_id = '" + IdTxt.Text + "'"; SqlCommand cmd2 = new SqlCommand(query2, conn); try { conn.Open(); SqlDataReader sdr = cmd2.ExecuteReader(); while (sdr.Read()) { Gtotal.Text = sdr["Total"].ToString(); } } finally { conn.Close(); }

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

            Please learn how to post a question properly: with an informative subject line, with code snippets inside PRE tags, with variable declarations, etc. You have several mistakes here: - AS total - the WHERE clause does not take quotes when the field is a number And yes, using SqlParameter rather than command string concatenation is the preferred way to do things. Read up on "SQL injection attacks" :)

            Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles] Nil Volentibus Arduum

            Please use <PRE> tags for code snippets, they preserve indentation, and improve readability.

            1 Reply Last reply
            0
            • M mjawadkhatri

              Hi. please tell me my mistake in blow code,code not return any value.. please help string query2 = "select sum(amount) Total from pur_inv_dtl where inv_id = '" + IdTxt.Text + "'"; SqlCommand cmd2 = new SqlCommand(query2, conn); try { conn.Open(); SqlDataReader sdr = cmd2.ExecuteReader(); while (sdr.Read()) { Gtotal.Text = sdr["Total"].ToString(); } } finally { conn.Close(); }

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

              There are a few things that you need to look at here. 1. You are using a query (as others have pointed out) that is wide open to SQL Injection. 2. You don't dispose of your SqlCommand (try wrapping them it the using statement). 3. You are using the wrong method to read data (and I'm surprised nobody pointed this out to you). Basically you have opened up a DataReader to read a single value out of the database. This is overkill. Try replacing this with ExecuteScalar instead, which is designed to return single values. 4. In your finally block, you call conn.Close; what happens if you didn't manage to open the connection in the first place?

              "WPF has many lovers. It's a veritable porn star!" - Josh Smith

              As Braveheart once said, "You can take our freedom but you'll never take our Hobnobs!" - Martin Hughes.

              My blog | My articles | MoXAML PowerToys | Onyx

              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