CONVERT c# decimal to SQL ???

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • trint

    CONVERT c# decimal to SQL ???

    I have a decimal amount in c#:

    decimal amounT = 62.75;

    and I need INSERT it into a SQL record as a money value.

    string strSQL2 = "INSERT INTO trvlDetail(amou ntfield) ""VALUES ('" +
    amounT + "') ";

    Should I use a CONVERT in SQL (if so, how?) or what would be the
    equivelent in c#?
    Thanks,
    Trint

  • Morten Wennevik

    #2
    Re: CONVERT c# decimal to SQL ???

    Hi Trint,

    Use an SqlParameter, it will convert for you


    decimal amounT = 62.75;

    SqlParameter param = new SqlParameter(@a mounT, amounT);

    string strSQL2 = "INSERT INTO trvlDetail(amou ntfield) VALUES (@amounT)";

    SqlCommand insertCommand = new SqlCommand(strS QL2, SqlConnection1) ;

    insertCommand.P arameters.Add(p aram);

    SqlConnection1. Open();
    insertCommand.E xecuteNonQuery( );
    SqlConnection1. Close();


    On Wed, 20 Apr 2005 14:22:49 +0200, trint <trinity.smith@ gmail.com> wrote:
    [color=blue]
    > decimal amounT = 62.75;[/color]



    --
    Happy coding!
    Morten Wennevik [C# MVP]

    Comment

    • Morten Wennevik

      #3
      Re: CONVERT c# decimal to SQL ???

      Hi Trint,

      Use an SqlParameter, it will convert for you


      decimal amounT = 62.75;

      SqlParameter param = new SqlParameter(@a mounT, amounT);

      string strSQL2 = "INSERT INTO trvlDetail(amou ntfield) VALUES (@amounT)";

      SqlCommand insertCommand = new SqlCommand(strS QL2, SqlConnection1) ;

      insertCommand.P arameters.Add(p aram);

      SqlConnection1. Open();
      insertCommand.E xecuteNonQuery( );
      SqlConnection1. Close();


      On Wed, 20 Apr 2005 14:22:49 +0200, trint <trinity.smith@ gmail.com> wrote:
      [color=blue]
      > decimal amounT = 62.75;[/color]



      --
      Happy coding!
      Morten Wennevik [C# MVP]

      Comment

      • Trint Smith

        #4
        Re: CONVERT c# decimal to SQL ???

        Thanks!

        ..Net programmer
        trinity.smith@g mail.com

        *** Sent via Developersdex http://www.developersdex.com ***

        Comment

        • Trint Smith

          #5
          Re: CONVERT c# decimal to SQL ???

          Thanks!

          ..Net programmer
          trinity.smith@g mail.com

          *** Sent via Developersdex http://www.developersdex.com ***

          Comment

          • Trint Smith

            #6
            Re: CONVERT c# decimal to SQL ???

            Ok, I did as you said and got this error:

            "Disallowed implicit conversion from data type varchar to data type
            money, table 'tsNess.dbo.trv lDetail', column 'amount'. Use the CONVERT
            function to run this query."

            ..Net programmer
            trinity.smith@g mail.com

            *** Sent via Developersdex http://www.developersdex.com ***

            Comment

            • Trint Smith

              #7
              Re: CONVERT c# decimal to SQL ???

              Ok, I did as you said and got this error:

              "Disallowed implicit conversion from data type varchar to data type
              money, table 'tsNess.dbo.trv lDetail', column 'amount'. Use the CONVERT
              function to run this query."

              ..Net programmer
              trinity.smith@g mail.com

              *** Sent via Developersdex http://www.developersdex.com ***

              Comment

              • Morten Wennevik

                #8
                Re: CONVERT c# decimal to SQL ???

                Did you remove the ''?

                VALUE(@amounT) without ''


                On Wed, 20 Apr 2005 15:44:20 +0200, Trint Smith <trinity.smith@ gmail.com> wrote:
                [color=blue]
                > Ok, I did as you said and got this error:
                >
                > "Disallowed implicit conversion from data type varchar to data type
                > money, table 'tsNess.dbo.trv lDetail', column 'amount'. Use the CONVERT
                > function to run this query."
                >
                > .Net programmer
                > trinity.smith@g mail.com
                >
                > *** Sent via Developersdex http://www.developersdex.com ***
                >[/color]



                --
                Happy coding!
                Morten Wennevik [C# MVP]

                Comment

                • Morten Wennevik

                  #9
                  Re: CONVERT c# decimal to SQL ???

                  Did you remove the ''?

                  VALUE(@amounT) without ''


                  On Wed, 20 Apr 2005 15:44:20 +0200, Trint Smith <trinity.smith@ gmail.com> wrote:
                  [color=blue]
                  > Ok, I did as you said and got this error:
                  >
                  > "Disallowed implicit conversion from data type varchar to data type
                  > money, table 'tsNess.dbo.trv lDetail', column 'amount'. Use the CONVERT
                  > function to run this query."
                  >
                  > .Net programmer
                  > trinity.smith@g mail.com
                  >
                  > *** Sent via Developersdex http://www.developersdex.com ***
                  >[/color]



                  --
                  Happy coding!
                  Morten Wennevik [C# MVP]

                  Comment

                  • trint

                    #10
                    Re: CONVERT c# decimal to SQL ???

                    Sorry, forgot to remove that. It works now.

                    Morten Wennevik wrote:[color=blue]
                    > Did you remove the ''?
                    >
                    > VALUE(@amounT) without ''
                    >
                    >
                    > On Wed, 20 Apr 2005 15:44:20 +0200, Trint Smith[/color]
                    <trinity.smith@ gmail.com> wrote:[color=blue]
                    >[color=green]
                    > > Ok, I did as you said and got this error:
                    > >
                    > > "Disallowed implicit conversion from data type varchar to data type
                    > > money, table 'tsNess.dbo.trv lDetail', column 'amount'. Use the[/color][/color]
                    CONVERT[color=blue][color=green]
                    > > function to run this query."
                    > >
                    > > .Net programmer
                    > > trinity.smith@g mail.com
                    > >
                    > > *** Sent via Developersdex http://www.developersdex.com ***
                    > >[/color]
                    >
                    >
                    >
                    > --
                    > Happy coding!
                    > Morten Wennevik [C# MVP][/color]

                    Comment

                    • trint

                      #11
                      Re: CONVERT c# decimal to SQL ???

                      Sorry, forgot to remove that. It works now.

                      Morten Wennevik wrote:[color=blue]
                      > Did you remove the ''?
                      >
                      > VALUE(@amounT) without ''
                      >
                      >
                      > On Wed, 20 Apr 2005 15:44:20 +0200, Trint Smith[/color]
                      <trinity.smith@ gmail.com> wrote:[color=blue]
                      >[color=green]
                      > > Ok, I did as you said and got this error:
                      > >
                      > > "Disallowed implicit conversion from data type varchar to data type
                      > > money, table 'tsNess.dbo.trv lDetail', column 'amount'. Use the[/color][/color]
                      CONVERT[color=blue][color=green]
                      > > function to run this query."
                      > >
                      > > .Net programmer
                      > > trinity.smith@g mail.com
                      > >
                      > > *** Sent via Developersdex http://www.developersdex.com ***
                      > >[/color]
                      >
                      >
                      >
                      > --
                      > Happy coding!
                      > Morten Wennevik [C# MVP][/color]

                      Comment

                      Working...