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. Database & SysAdmin
  3. Database
  4. Sudden cast problem using executescalar

Sudden cast problem using executescalar

Scheduled Pinned Locked Moved Database
helpdatabasequestionsysadmin
3 Posts 2 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.
  • G Offline
    G Offline
    Gennady Oster
    wrote on last edited by
    #1

    Hi ! Here is the snip of code in question:

    .......
    sql = "select " + picField + " from " + table + " where " + where;
    conn = new SqlConnection(connectionstring);
    byte[] picture = (byte[])ExecuteScalar(sql); // Shows error
    Image source = Image.FromStream(new MemoryStream(picture));
    Image th = source.GetThumbnailImage(w, h, null, IntPtr.Zero);
    .......
    public object ExecuteScalar(string strCommand)
    {
    object res=null;
    conn.Open();
    SqlCommand cmd = new SqlCommand(strCommand, conn);
    try
    {
    res = cmd.ExecuteScalar();
    }
    catch(Exception e)
    { res = e.Message; }
    finally
    {
    conn.Close();
    }
    return res;
    }

    It worked like a sharm some time ago. Code was not changed. The table definition also was not changed and field declaration is as follows: [Picture_Image] [image] NOT NULL But now I get error: unable to cast object of type 'system.string' to type 'system.byte ' I suppose that something may be changed in the server and/or DB settings. But what ?? Can somebody please shed a light on this issue? Thanks in advance. Regards, Gennady

    My English is permanently under construction. Be patient !!

    Richard DeemingR 1 Reply Last reply
    0
    • G Gennady Oster

      Hi ! Here is the snip of code in question:

      .......
      sql = "select " + picField + " from " + table + " where " + where;
      conn = new SqlConnection(connectionstring);
      byte[] picture = (byte[])ExecuteScalar(sql); // Shows error
      Image source = Image.FromStream(new MemoryStream(picture));
      Image th = source.GetThumbnailImage(w, h, null, IntPtr.Zero);
      .......
      public object ExecuteScalar(string strCommand)
      {
      object res=null;
      conn.Open();
      SqlCommand cmd = new SqlCommand(strCommand, conn);
      try
      {
      res = cmd.ExecuteScalar();
      }
      catch(Exception e)
      { res = e.Message; }
      finally
      {
      conn.Close();
      }
      return res;
      }

      It worked like a sharm some time ago. Code was not changed. The table definition also was not changed and field declaration is as follows: [Picture_Image] [image] NOT NULL But now I get error: unable to cast object of type 'system.string' to type 'system.byte ' I suppose that something may be changed in the server and/or DB settings. But what ?? Can somebody please shed a light on this issue? Thanks in advance. Regards, Gennady

      My English is permanently under construction. Be patient !!

      Richard DeemingR Offline
      Richard DeemingR Offline
      Richard Deeming
      wrote on last edited by
      #2

      Gennady Oster wrote:

      catch(Exception e){ res = e.Message; } return res;

      There is an error in your query which is causing the ExecuteScalar method to throw an exception. Due to the incredibly poor decision to return the exception message as a valid result from your ExecuteScalar method, the calling code is trying to cast the string containing the error message to a byte[], which will never work. Remove the catch clause from your method to allow the exception to propagate to the calling code. The fact that you're specifying a WHERE clause but not passing any parameters suggests that your code is susceptible to SQL Injection[^]. Update your code to use parameterized queries before you get a visit from little Bobby Tables[^].


      "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

      "These people looked deep within my soul and assigned me a number based on the order in which I joined" - Homer

      G 1 Reply Last reply
      0
      • Richard DeemingR Richard Deeming

        Gennady Oster wrote:

        catch(Exception e){ res = e.Message; } return res;

        There is an error in your query which is causing the ExecuteScalar method to throw an exception. Due to the incredibly poor decision to return the exception message as a valid result from your ExecuteScalar method, the calling code is trying to cast the string containing the error message to a byte[], which will never work. Remove the catch clause from your method to allow the exception to propagate to the calling code. The fact that you're specifying a WHERE clause but not passing any parameters suggests that your code is susceptible to SQL Injection[^]. Update your code to use parameterized queries before you get a visit from little Bobby Tables[^].


        "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

        G Offline
        G Offline
        Gennady Oster
        wrote on last edited by
        #3

        Thanks, Richard. Saying truth, I've already understand where I made a mistake. I anyway want ExecuteScalar to return db error, so I simply added the test is byte for result before casting:

                    Object sqlResult = ExecuteScalar(sql);
                    if (!(sqlResult is byte))
                    {
                        return sqlResult.ToString();
                    }
        

        This is the internal module, and WHERE clause is generated programmatically, so I don't see the ability to SQL Injection. Thanks once more. Regards, Gennady

        My English is permanently under construction. Be patient !!

        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