Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6833569
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T22:59:40+00:00 2026-05-26T22:59:40+00:00

I have a little question .. I am using blob to upload my files

  • 0

I have a little question .. I am using blob to upload my files and I can download them too when they are movies or mp3 for example, but when I try to download a photo i always have this exception … Can someone tell me why ?

Unable to cast object of type 'System.DBNull' to type 'System.Byte[]'.

Description: An unhandled exception occurred during the execution of the current
web request. Please review the stack trace for more information about the error 
and where it originated in the code. 

Exception Details: System.InvalidCastException: Unable to cast object of type 
'System.DBNull' to type 'System.Byte[]'.

Source Error: 

 An unhandled exception was generated during the execution of the current web requt.
 Information regarding the origin and location of the exception can be identified using
 the exception stack trace below.

Thank you for helping.

    This is the code to retrieve them . 

       string filename = Request["file"].ToString();
        var conString = ConfigurationManager.ConnectionStrings["LocalSqlServer"];
        string strConnString = conString.ConnectionString;
        SqlConnection dbConnection = new SqlConnection(strConnString);
        dynamic queryString = ("SELECT Data,ContentType FROM Files WHERE Name = '" + filename + "'");
        SqlCommand theCommand = new SqlCommand(queryString, dbConnection);
        dbConnection.Open();
        SqlDataReader reader = theCommand.ExecuteReader();

        if (reader.Read() && reader != null)
        {

            Byte[] bytes;
            bytes = Encoding.UTF8.GetBytes(String.Empty);
            bytes = (Byte[])reader["Data"];
                            string contentType = reader["ContentType"].ToString();
            Response.ContentType = contentType;
            Response.AddHeader("content-disposition", "attachment;  filename=" + filename + "");
            Response.BinaryWrite(bytes);
            reader.Close();
        }

        dbConnection.Close();

And to upload them :

   dbConnection.Open();
                        string queryString = "INSERT INTO Files (Name,Path,UserUpload,Date,Data,ContentType,Size) VALUES (@Name,@Path,@UserUpload,@Date,@Data,@ContentType,@Size);" + "SELECT CAST(scope_identity() AS int)";
                        SqlCommand theCommand = new SqlCommand(queryString, dbConnection);
                        theCommand.Parameters.AddWithValue("@Name", FileUpload1.FileName);
                        theCommand.Parameters.AddWithValue("@Path", GetTheCurrentDirectory(MyTreeView.SelectedNode));
                        theCommand.Parameters.AddWithValue("@UserUpload", Request.Cookies["UserSettings"]["UserName"]);
                        theCommand.Parameters.AddWithValue("@Date", DateTime.Now);
                        theCommand.Parameters.AddWithValue("@Data", FileUpload1.FileBytes);
                        theCommand.Parameters.AddWithValue("@ContentType", FileUpload1.PostedFile.ContentType);
                        theCommand.Parameters.AddWithValue("@Size", FileUpload1.PostedFile.ContentLength);

                        int newFid = (Int32)theCommand.ExecuteScalar();

                        dynamic queryStringFolder = "INSERT INTO FILES_FOLDERS (File_Id,Folder_Id) VALUES (@FileId,@FolderId);";
                        theCommand = new SqlCommand(queryStringFolder, dbConnection);
                        theCommand.Parameters.AddWithValue("@FileId", newFid);
                        theCommand.Parameters.AddWithValue("@FolderId", MyTreeView.SelectedValue);
                        theCommand.ExecuteNonQuery();
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T22:59:41+00:00Added an answer on May 26, 2026 at 10:59 pm

    It sounds like the blob you are attempting to retrieve is NULL. If this is an expected condition, you’ll need to place a check for DBNull before you attempt to cast to a byte array.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have only a little question: Why the CFPreferences-API creates multiple files in my
I have a little question about creating a table using another tables. my code:
Ні, all! I have a little question about jQuery.Validation plugin: - Can I complete
I have a little question. I am using Codeigniter 2.1.0 and I have the
I have little question about how web browser retrieve webpage? I know this User
I have little question. I found this on internet: Type t = typeof(MyClass); MethodInfo[]
I have a little question to community. Does ASP.NET MVC allow to execute an
I have this little question that's been on my mind for a while now.
i have a little question, regarding to slideup and slidedown, the problem is in
Following the good jQuery Plugins/Authoring instructions I have a little question (function($){ // Default

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.