Click here to Skip to main content
13,765,544 members

The Weird and The Wonderful

   

The Weird and The Wonderful forum is a place to post Coding Horrors, Worst Practices, and the occasional flash of brilliance.

We all come across code that simply boggles the mind. Lazy kludges, embarrassing mistakes, horrid workarounds and developers just not quite getting it. And then somedays we come across - or write - the truly sublime.

Post your Best, your worst, and your most interesting. But please - no programming questions . This forum is purely for amusement and discussions on code snippets. All actual programming questions will be removed.

 
GeneralRe: Isomorphic Git Pin
GenJerDan16-May-18 20:41
memberGenJerDan16-May-18 20:41 
GeneralRe: Isomorphic Git Pin
Marc Clifton17-May-18 4:38
protectorMarc Clifton17-May-18 4:38 
GeneralRe: Isomorphic Git Pin
Super Lloyd18-May-18 21:12
memberSuper Lloyd18-May-18 21:12 
GeneralRe: Isomorphic Git Pin
megaadam21-May-18 6:41
professionalmegaadam21-May-18 6:41 
GeneralRe: Isomorphic Git Pin
Super Lloyd21-May-18 7:16
memberSuper Lloyd21-May-18 7:16 
GeneralRe: Isomorphic Git Pin
Richard Deeming21-May-18 8:23
mvpRichard Deeming21-May-18 8:23 
GeneralRe: Isomorphic Git Pin
Super Lloyd21-May-18 8:47
memberSuper Lloyd21-May-18 8:47 
GeneralIs it my fault? Pin
PIEBALDconsult14-May-18 15:31
protectorPIEBALDconsult14-May-18 15:31 
I do a lot with ADO.net, and I use parameters as much as I can. Recently I was working on this method:

private static System.Data.IDbDataParameter
GetParameter
(
  this System.Data.IDbCommand CMD
,
  string                      Name
)
{
  System.Data.IDbDataParameter result ;

  if ( CMD.Parameters.Contains ( Name ) )
  {
    result = (System.Data.IDbDataParameter) CMD.Parameters [ Name ] ;
  }
  else
  {
    result = CMD.CreateParameter() ;

    result.ParameterName = Name ;

    CMD.Parameters.Add ( result ) ;
  }

  return ( result ) ;
}


It gets a parameter by name or creates one with the provided name if there isn't one already.
After a while I decided to change it to avoid the call to Contains (the getter and Add have to check for existence anyway) and use try/catch to detect the absence of the parameter:

private static System.Data.IDbDataParameter
GetParameter
(
  this System.Data.IDbCommand CMD
,
  string                      Name
)
{
  System.Data.IDbDataParameter result ;

  try
  {
    result = (System.Data.IDbDataParameter) CMD.Parameters [ Name ] ;
  }
  catch ( System.IndexOutOfRangeException err )
  {
    result = CMD.CreateParameter() ;

    result.ParameterName = Name ;

    CMD.Parameters.Add ( result ) ;
  }

  return ( result ) ;
}


And it was good.

Until today.

And Oracle.

Oracle.DataAccess.Client.OracleParameterCollection returns NULL when the parameter doesn't exist! Mad | :mad: Mad | :mad: Mad | :mad:

I had to revert back to the earlier code. Sigh | :sigh:


Edit: After a little more experimentation, I find that other ADO.net Providers I have handy also differ from Microsoft's lead -- four vendors, four outcomes. But, then again, MSDN doesn't say what should happen in this situation!

Anyway, I have since decided that the whole concept of the above code was a bad idea -- I know whether or not the parameter exists and I should act accordingly.
In any case, I rarely have to get a parameter by name -- it's a code smell.

Here, then, is an improved GetParameter (by Name), with no trying to Create and Add missing parameters -- I don't intend to call this when I know the parameter doesn't exist.
When a parameter doesn't exist, it is consistent in its behavior, so the caller can react effectively.

private static System.InvalidOperationException
    GetParameterException
    (
      string           Name
    ,
      System.Exception Exception
    )
    {
      Exception = new System.InvalidOperationException 
      ( 
        "A Parameter with ParameterName '" + Name + "' is not contained by this ParameterCollection." 
      , 
        Exception 
      ) ;

      Exception.Data [ "ParameterName" ] = Name ;

      return ( (System.InvalidOperationException) Exception ) ;
    }
 
    public static System.Data.IDbDataParameter
    GetParameter
    (
      this System.Data.IDbCommand CMD
    ,
      string                      Name
    )
    {
      object result ;
 
      try
      {
        result = CMD.Parameters [ Name ] ;
      }
      /* Microsoft's Providers                     throw  System.IndexOutOfRangeException    */
      /* FirebirdSql.Data.FirebirdClient.FbCommand throws System.ArgumentOutOfRangeException */
      /* Devart.Data.Oracle.OracleCommand          throws System.ArgumentException           */
      catch ( System.Exception err )
      {
        throw ( GetParameterException ( Name , err ) ) ;
      }
 
      /* Oracle.DataAccess.Client.OracleCommand returns NULL */
      if ( result == null )
      {
        throw ( GetParameterException ( Name , null ) ) ;
      }
      
      return ( (System.Data.IDbDataParameter) result ) ;
    }



<rhetorical>
Why do Microsoft's implementations throw System.IndexOutOfRangeException ?
Do they simply iterate the collection until they find the item or hit the end? OMG | :OMG:
</rhetorical>


Edit 2 :
Ca.Ingres.Client.IngresCommand throws System.ArgumentOutOfRangeException
InterSystems.Data.CacheClient.CacheCommand throws System.IndexOutOfRangeException
MySql.Data.MySqlClient.MySqlCommand throws System.ArgumentException

modified 17-May-18 1:56am.

GeneralRe: Is it my fault? Pin
Super Lloyd14-May-18 17:51
memberSuper Lloyd14-May-18 17:51 
GeneralRe: Is it my fault? Pin
Jon McKee14-May-18 19:17
professionalJon McKee14-May-18 19:17 
GeneralRe: Is it my fault? Pin
Eddy Vluggen15-May-18 2:01
professionalEddy Vluggen15-May-18 2:01 
GeneralRe: Is it my fault? Pin
Jon McKee15-May-18 9:36
professionalJon McKee15-May-18 9:36 
GeneralRe: Is it my fault? Pin
Eddy Vluggen16-May-18 1:46
professionalEddy Vluggen16-May-18 1:46 
GeneralRe: Is it my fault? Pin
F-ES Sitecore16-May-18 2:21
mvpF-ES Sitecore16-May-18 2:21 
GeneralRe: Is it my fault? Pin
Eddy Vluggen16-May-18 2:39
professionalEddy Vluggen16-May-18 2:39 
GeneralRe: Is it my fault? Pin
F-ES Sitecore16-May-18 3:33
mvpF-ES Sitecore16-May-18 3:33 
GeneralRe: Is it my fault? Pin
Eddy Vluggen16-May-18 4:03
professionalEddy Vluggen16-May-18 4:03 
GeneralRe: Is it my fault? Pin
F-ES Sitecore16-May-18 4:28
mvpF-ES Sitecore16-May-18 4:28 
GeneralRe: Is it my fault? Pin
Eddy Vluggen16-May-18 4:56
professionalEddy Vluggen16-May-18 4:56 
GeneralRe: Is it my fault? Pin
F-ES Sitecore16-May-18 5:09
mvpF-ES Sitecore16-May-18 5:09 
GeneralRe: Is it my fault? Pin
Eddy Vluggen16-May-18 11:35
professionalEddy Vluggen16-May-18 11:35 
GeneralRe: Is it my fault? Pin
F-ES Sitecore16-May-18 23:27
mvpF-ES Sitecore16-May-18 23:27 
GeneralRe: Is it my fault? Pin
Eddy Vluggen18-May-18 1:03
professionalEddy Vluggen18-May-18 1:03 
GeneralRe: Is it my fault? Pin
F-ES Sitecore18-May-18 1:10
mvpF-ES Sitecore18-May-18 1:10 
GeneralRe: Is it my fault? Pin
Eddy Vluggen18-May-18 1:16
professionalEddy Vluggen18-May-18 1:16 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.


Advertise | Privacy | Cookies | Terms of Service
Web01-2016 | 2.8.181114.1 | Last Updated 15 Nov 2018
Copyright © CodeProject, 1999-2018
All Rights Reserved.
Layout: fixed | fluid