Loading a char value from an SQL parameter - Crashing!

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

    #1

    Loading a char value from an SQL parameter - Crashing!

    Hi:

    Newby Question:

    I have the following code in a class library that performs a query on an SQL
    database and is returning a class that contains "locatestat us":

    if (p_locatestatus .Value != System.DBNull.V alue)
    { rtrec.locatesta tus = (char)p_locates tatus.Value; };

    rtrec.locatesta tus is a char.

    This compiles just fine however it causes an error and is trapped by my
    try/catch everytime. There must be something that I need to do to convert a
    char - all other types are converting just fine.

    Any info on the steps I'm missing would be greatly appreciated!

    Thanks,

    Fred


  • Bruce Wood

    #2
    Re: Loading a char value from an SQL parameter - Crashing!

    What exception does it generate?

    Comment

    • Ignacio Machin \( .NET/ C# MVP \)

      #3
      Re: Loading a char value from an SQL parameter - Crashing!

      Hi,

      why don't you set a breakpoint in the if line and see the type of
      p_locatestatus. Value, a quickwatch will do the trick.

      Also what about using Convert.ToChar instead of the cast?


      cheers,

      --
      Ignacio Machin,
      ignacio.machin AT dot.state.fl.us
      Florida Department Of Transportation



      "Fred Nelson" <fred@smartybir d.com> wrote in message
      news:eHjCti9WFH A.2980@TK2MSFTN GP10.phx.gbl...[color=blue]
      > Hi:
      >
      > Newby Question:
      >
      > I have the following code in a class library that performs a query on an
      > SQL
      > database and is returning a class that contains "locatestat us":
      >
      > if (p_locatestatus .Value != System.DBNull.V alue)
      > { rtrec.locatesta tus = (char)p_locates tatus.Value; };
      >
      > rtrec.locatesta tus is a char.
      >
      > This compiles just fine however it causes an error and is trapped by my
      > try/catch everytime. There must be something that I need to do to convert
      > a
      > char - all other types are converting just fine.
      >
      > Any info on the steps I'm missing would be greatly appreciated!
      >
      > Thanks,
      >
      > Fred
      >
      >[/color]


      Comment

      • Fred Nelson

        #4
        Re: Loading a char value from an SQL parameter - Crashing!

        Hi Bruce:

        Good question - I should have included this!

        It generates: System.InvalidC astException - Specified cast it not valid.

        Thanks,

        Fred


        "Bruce Wood" <brucewood@cana da.com> wrote in message
        news:1116442321 .214541.31050@g 14g2000cwa.goog legroups.com...[color=blue]
        > What exception does it generate?
        >[/color]


        Comment

        • Fred Nelson

          #5
          Re: Loading a char value from an SQL parameter - Crashing!

          Ignacio:

          Even though the parameter is defined as being SqlDbType.Char it was showing
          as a string at the breakpoint.

          The Convert.ToChar solved the problem for me!

          Thank you very much for your help!

          Fred


          "Ignacio Machin ( .NET/ C# MVP )" <ignacio.mach in AT dot.state.fl.us > wrote
          in message news:%23frhsr9W FHA.3864@tk2msf tngp13.phx.gbl. ..[color=blue]
          > Hi,
          >
          > why don't you set a breakpoint in the if line and see the type of
          > p_locatestatus. Value, a quickwatch will do the trick.
          >
          > Also what about using Convert.ToChar instead of the cast?
          >
          >
          > cheers,
          >
          > --
          > Ignacio Machin,
          > ignacio.machin AT dot.state.fl.us
          > Florida Department Of Transportation
          >
          >
          >
          > "Fred Nelson" <fred@smartybir d.com> wrote in message
          > news:eHjCti9WFH A.2980@TK2MSFTN GP10.phx.gbl...[color=green]
          > > Hi:
          > >
          > > Newby Question:
          > >
          > > I have the following code in a class library that performs a query on an
          > > SQL
          > > database and is returning a class that contains "locatestat us":
          > >
          > > if (p_locatestatus .Value != System.DBNull.V alue)
          > > { rtrec.locatesta tus = (char)p_locates tatus.Value; };
          > >
          > > rtrec.locatesta tus is a char.
          > >
          > > This compiles just fine however it causes an error and is trapped by my
          > > try/catch everytime. There must be something that I need to do to[/color][/color]
          convert[color=blue][color=green]
          > > a
          > > char - all other types are converting just fine.
          > >
          > > Any info on the steps I'm missing would be greatly appreciated!
          > >
          > > Thanks,
          > >
          > > Fred
          > >
          > >[/color]
          >
          >[/color]


          Comment

          Working...