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. a couple of C# questions

a couple of C# questions

Scheduled Pinned Locked Moved C#
questioncsharpcssdatabasesql-server
9 Posts 4 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.
  • B Offline
    B Offline
    Brendan Vogt
    wrote on last edited by
    #1

    Hi, I just have a couple of questions: If I have the following, what is the value of decEuro if the textbox txtEuro is empty? decimal decEuro = Convert.ToDecimal(txtEuro.Text.Trim()); Is decimal the best data type used for a currency value? /********************************************************/ Check the following function: public static Object CheckDecimalForDB(decimal decCheckDecimal) { if(decCheckDecimal != 0) return decCheckDecimal; else return DBNull.Value; } The datatype/type returned when decCheckDecimal contains a value, is decimal, or Object? /********************************************************/ I need to format data in a data grid, firstly time based on Culture info (South Africa), and secondly, decimal values to contain only 2 decimal places after the comma. This is how I populate my datagrid with the 2 values: "DateRequested" has a datetime datatype in SQL Server, and "Euro" is of type money. /********************************************************/ I need to post data from one form to another form. I used Server.Transfer but then the new page doesn't display in the url "bar". Basically I just want the data to go from page A to page B. Usually in classic ASP I used Request.Form("txtFName") to get the value of the textbox in page A. I'm not sure if the Response.Redirect method does this, don't think so. This is all for now, I hope to here from the experts soon. Regards. ma se :cool:

    R G B 3 Replies Last reply
    0
    • B Brendan Vogt

      Hi, I just have a couple of questions: If I have the following, what is the value of decEuro if the textbox txtEuro is empty? decimal decEuro = Convert.ToDecimal(txtEuro.Text.Trim()); Is decimal the best data type used for a currency value? /********************************************************/ Check the following function: public static Object CheckDecimalForDB(decimal decCheckDecimal) { if(decCheckDecimal != 0) return decCheckDecimal; else return DBNull.Value; } The datatype/type returned when decCheckDecimal contains a value, is decimal, or Object? /********************************************************/ I need to format data in a data grid, firstly time based on Culture info (South Africa), and secondly, decimal values to contain only 2 decimal places after the comma. This is how I populate my datagrid with the 2 values: "DateRequested" has a datetime datatype in SQL Server, and "Euro" is of type money. /********************************************************/ I need to post data from one form to another form. I used Server.Transfer but then the new page doesn't display in the url "bar". Basically I just want the data to go from page A to page B. Usually in classic ASP I used Request.Form("txtFName") to get the value of the textbox in page A. I'm not sure if the Response.Redirect method does this, don't think so. This is all for now, I hope to here from the experts soon. Regards. ma se :cool:

      R Offline
      R Offline
      Rob Philpott
      wrote on last edited by
      #2

      Surely if you are unsure of stuff like this, the best thing to do is create a simple console application and try it out? decimal decEuro = Convert.ToDecimal(txtEuro.Text.Trim()); If the textbox is empty, you'll be trying to convert an empty string to a decimal, and an exception will be thrown so it'll never get as far as assigning decEuro. public static Object CheckDecimalForDB(decimal decCheckDecimal) { if(decCheckDecimal != 0) return decCheckDecimal; else return DBNull.Value; } The return type is Object which is the base type for everything. This object will either be a boxed value type of System.Decimal or reference type System.DBNull. You can use the .GetType() method to determine which. Rob Philpott.

      B 1 Reply Last reply
      0
      • B Brendan Vogt

        Hi, I just have a couple of questions: If I have the following, what is the value of decEuro if the textbox txtEuro is empty? decimal decEuro = Convert.ToDecimal(txtEuro.Text.Trim()); Is decimal the best data type used for a currency value? /********************************************************/ Check the following function: public static Object CheckDecimalForDB(decimal decCheckDecimal) { if(decCheckDecimal != 0) return decCheckDecimal; else return DBNull.Value; } The datatype/type returned when decCheckDecimal contains a value, is decimal, or Object? /********************************************************/ I need to format data in a data grid, firstly time based on Culture info (South Africa), and secondly, decimal values to contain only 2 decimal places after the comma. This is how I populate my datagrid with the 2 values: "DateRequested" has a datetime datatype in SQL Server, and "Euro" is of type money. /********************************************************/ I need to post data from one form to another form. I used Server.Transfer but then the new page doesn't display in the url "bar". Basically I just want the data to go from page A to page B. Usually in classic ASP I used Request.Form("txtFName") to get the value of the textbox in page A. I'm not sure if the Response.Redirect method does this, don't think so. This is all for now, I hope to here from the experts soon. Regards. ma se :cool:

        G Offline
        G Offline
        Gavin Jeffrey
        wrote on last edited by
        #3

        - decimal decEuro = Convert.ToDecimal(txtEuro.Text.Trim()); - would throw an FormatException - I would say that double would be better for currency - returned is always object i.e. it is an Object of decimal - You could look at something like: System.Globalization.CultureInfo c = new System.Globalization.CultureInfo("en-ZA");//I think? i don't really know much about this so I can't really help. Or you could format your data in your query before it gets returned to the front end e.g. select convert(varchar(10),DateCaptured,101)... would return me my date already in the format i want - you could use response.redirect("pageb.aspx?txtFName=" + txtFName.Text + "); and then at page b you could use Request["txtFName"].ToString();

        B 1 Reply Last reply
        0
        • R Rob Philpott

          Surely if you are unsure of stuff like this, the best thing to do is create a simple console application and try it out? decimal decEuro = Convert.ToDecimal(txtEuro.Text.Trim()); If the textbox is empty, you'll be trying to convert an empty string to a decimal, and an exception will be thrown so it'll never get as far as assigning decEuro. public static Object CheckDecimalForDB(decimal decCheckDecimal) { if(decCheckDecimal != 0) return decCheckDecimal; else return DBNull.Value; } The return type is Object which is the base type for everything. This object will either be a boxed value type of System.Decimal or reference type System.DBNull. You can use the .GetType() method to determine which. Rob Philpott.

          B Offline
          B Offline
          Brendan Vogt
          wrote on last edited by
          #4

          Thanks. If the return type is Object, and I do return the decimal value, as for example 10.5, would I still need to convert the value to a decimal where it is being called, or will it assume that it is of type decimal? Lets say the called is used like this: paramPound.Value = Globals.CheckDecimalForDB(objProperty.Pound); Does .value contain a decimal, or must I still use Convert.ToDecimal();?? Do you have any comments on formatting dates and decimal numbers in the datagrid.

          R 1 Reply Last reply
          0
          • G Gavin Jeffrey

            - decimal decEuro = Convert.ToDecimal(txtEuro.Text.Trim()); - would throw an FormatException - I would say that double would be better for currency - returned is always object i.e. it is an Object of decimal - You could look at something like: System.Globalization.CultureInfo c = new System.Globalization.CultureInfo("en-ZA");//I think? i don't really know much about this so I can't really help. Or you could format your data in your query before it gets returned to the front end e.g. select convert(varchar(10),DateCaptured,101)... would return me my date already in the format i want - you could use response.redirect("pageb.aspx?txtFName=" + txtFName.Text + "); and then at page b you could use Request["txtFName"].ToString();

            B Offline
            B Offline
            Brendan Vogt
            wrote on last edited by
            #5

            Thanks. I'm still not 100% sure how to format this in the datagrid to the value I want.

            1 Reply Last reply
            0
            • B Brendan Vogt

              Thanks. If the return type is Object, and I do return the decimal value, as for example 10.5, would I still need to convert the value to a decimal where it is being called, or will it assume that it is of type decimal? Lets say the called is used like this: paramPound.Value = Globals.CheckDecimalForDB(objProperty.Pound); Does .value contain a decimal, or must I still use Convert.ToDecimal();?? Do you have any comments on formatting dates and decimal numbers in the datagrid.

              R Offline
              R Offline
              Rob Philpott
              wrote on last edited by
              #6

              Hi again,

              ma se wrote:

              ...would I still need to convert the value to a decimal where it is being called...

              This depends on what you're assigning to. Ultimately the return type is Object even if its a boxed decimal, so unless your assigning to another Object you'd need to cast it. For instance if your paramPound.Value is of type decimal you would have to do this: paramPound.Value = (decimal)Globals.CheckDecimalForDb(objProperty.Pound) but this line would throw an invalid cast exception in the instance where you return DBNull. If paramPound.Value is of type Object, you can leave the cast out, and no exceptions are thrown, but this presumes that paramPound.Value can deal with an object which could either be a boxed decimal or DBNull. I hope that sort of answers your question... BTW - I think you are correct to use decimal for currency. As for the datagrid, I'm afraid I'm unfamiliar with this and have no suggestions. Rob Philpott.

              B 1 Reply Last reply
              0
              • R Rob Philpott

                Hi again,

                ma se wrote:

                ...would I still need to convert the value to a decimal where it is being called...

                This depends on what you're assigning to. Ultimately the return type is Object even if its a boxed decimal, so unless your assigning to another Object you'd need to cast it. For instance if your paramPound.Value is of type decimal you would have to do this: paramPound.Value = (decimal)Globals.CheckDecimalForDb(objProperty.Pound) but this line would throw an invalid cast exception in the instance where you return DBNull. If paramPound.Value is of type Object, you can leave the cast out, and no exceptions are thrown, but this presumes that paramPound.Value can deal with an object which could either be a boxed decimal or DBNull. I hope that sort of answers your question... BTW - I think you are correct to use decimal for currency. As for the datagrid, I'm afraid I'm unfamiliar with this and have no suggestions. Rob Philpott.

                B Offline
                B Offline
                Brendan Vogt
                wrote on last edited by
                #7

                Hi again yes, This how I create my parameter: SqlParameter paramPound = new SqlParameter("@Pound", SqlDbType.Money, 8); paramPound.Value = Globals.CheckDecimalForDB(objProperty.Pound); objCmd.Parameters.Add(paramPound); I just have 2 questions: Firstly, because the SQL Server table field is of type money, can I use a decimal? And secondly, with regards to your cast to decimal, it will throw an exception. So the best way to do this is to leave the cast out? For if you must know, this is my check function: public static Object CheckDecimalForDB(decimal decCheckDecimal) { if(decCheckDecimal != 0) return decCheckDecimal; else return DBNull.Value; } How would you do it? Would you just do a test for this when you create your parameters? Cheers.

                R 1 Reply Last reply
                0
                • B Brendan Vogt

                  Hi again yes, This how I create my parameter: SqlParameter paramPound = new SqlParameter("@Pound", SqlDbType.Money, 8); paramPound.Value = Globals.CheckDecimalForDB(objProperty.Pound); objCmd.Parameters.Add(paramPound); I just have 2 questions: Firstly, because the SQL Server table field is of type money, can I use a decimal? And secondly, with regards to your cast to decimal, it will throw an exception. So the best way to do this is to leave the cast out? For if you must know, this is my check function: public static Object CheckDecimalForDB(decimal decCheckDecimal) { if(decCheckDecimal != 0) return decCheckDecimal; else return DBNull.Value; } How would you do it? Would you just do a test for this when you create your parameters? Cheers.

                  R Offline
                  R Offline
                  Rob Philpott
                  wrote on last edited by
                  #8

                  OK. I think you're code is fine as it is. SqlParameter.Value is of type Object which expects either a DBNull or a value (which is what you give it), and the type SqlDbType.Money is decimal. No casting is necessary. One point though - this implementation means that a value of 0.00 can't be put into the database. DBNull is used to mean 'not applicable here' not zero. So for instance if you wanted to maintain the balance of a bank account without any funds in it you couldn't. The balance would not be 0.00, the balance would not exist. Rob Philpott.

                  1 Reply Last reply
                  0
                  • B Brendan Vogt

                    Hi, I just have a couple of questions: If I have the following, what is the value of decEuro if the textbox txtEuro is empty? decimal decEuro = Convert.ToDecimal(txtEuro.Text.Trim()); Is decimal the best data type used for a currency value? /********************************************************/ Check the following function: public static Object CheckDecimalForDB(decimal decCheckDecimal) { if(decCheckDecimal != 0) return decCheckDecimal; else return DBNull.Value; } The datatype/type returned when decCheckDecimal contains a value, is decimal, or Object? /********************************************************/ I need to format data in a data grid, firstly time based on Culture info (South Africa), and secondly, decimal values to contain only 2 decimal places after the comma. This is how I populate my datagrid with the 2 values: "DateRequested" has a datetime datatype in SQL Server, and "Euro" is of type money. /********************************************************/ I need to post data from one form to another form. I used Server.Transfer but then the new page doesn't display in the url "bar". Basically I just want the data to go from page A to page B. Usually in classic ASP I used Request.Form("txtFName") to get the value of the textbox in page A. I'm not sure if the Response.Redirect method does this, don't think so. This is all for now, I hope to here from the experts soon. Regards. ma se :cool:

                    B Offline
                    B Offline
                    Brian Leach
                    wrote on last edited by
                    #9

                    ma se wrote:

                    If I have the following, what is the value of decEuro if the textbox txtEuro is empty? decimal decEuro = Convert.ToDecimal(txtEuro.Text.Trim());

                    The Visual Studio help file tells you (use MSDN if you don't have Visual Studio) that you will throw an ArgumentException if the value is null, FormatException if "value does not consist of an optional sign followed by a sequence of digits (zero through nine) and an optional decimal point symbol.'

                    ma se wrote:

                    Is decimal the best data type used for a currency value?

                    I don't work with currencies, I can't help you there.

                    ma se wrote:

                    public static Object CheckDecimalForDB(decimal decCheckDecimal) { if(decCheckDecimal != 0) return decCheckDecimal; else return DBNull.Value; } The datatype/type returned when decCheckDecimal contains a value, is decimal, or Object?

                    When it contains a value, it will be a decimal. You will need to caste it as such. The proper technique is:

                    decimal value = (decimal)CheckDecimalFOrDB( d ) as decimal;
                    if ( value != null ) {
                    ....
                    }

                    Again the help file has this documented. Look up 'as operator'. Can't help you with the Euro Formatting My suspicion is that you should use the Response.Redirect. The Server.Transfer is performed strictly on the server side, the browser is unaware that it has been directed to a new page. If the user does a refresh, they will be redirected back to the original page. Response.Redirect causes the browser to request the new page. Values will need to be passed by other means such as a cookie or session state. If session state is used and memory is an issue, be sure to recover the session memory by setting the values to null after you have extracted them. See the Visual studio help file at ms-help://MS.VSCC.2003/MS.MSDNQTR.2003FEB.1033/dv_vbcode/html/vbtskCodeExamplePassingDataFromOneWebPageToAnother.htm Brian Leach

                    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