Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

Professional Opinions
Ask a Question
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

troubleshooting Question

Error: Unable to cast object of type 'System.String' to type 'System.Byte[]'

Avatar of JessyRobinson1234
JessyRobinson1234 asked on
ASP.NETVisual Basic.NETMicrosoft SQL Server 2005
33 Comments1 Solution1701 ViewsLast Modified:
My database and sample data looks like:

File:                              File_Name:
<Binary data>              C:\Documents and Settings\DL1ITJJR\Desktop\Budget_Actual 1 .xls

I cureently have

Response.BinaryWrite(DirectCast(rdr("File"), Byte()))  ---> tjhis returns an empty excel file
I need the uploaded excel file so replaced it with:

Response.BinaryWrite(DirectCast(rdr("File_Name"), Byte()))

which gives me the Unable to cast object of type 'System.String' to type 'System.Byte[]'
Private Sub bLink_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles bLink.Click

        Dim conString As String = ConfigurationManager.ConnectionStrings("FCConnectionString").ToString
        Dim objConn As SqlConnection = New SqlConnection(conString)
        Dim rdr As SqlDataReader


        Dim cmd = New SqlCommand("View_CT", objConn)

        cmd.CommandType = CommandType.StoredProcedure

        Dim CT_Key As SqlParameter = cmd.Parameters.Add("@CT_Key", SqlDbType.Int)
        CT_Key.Value = lblCTKey.Text

        rdr = cmd.ExecuteReader()
        If rdr.HasRows Then
            Response.ContentType = "application/vnd.ms-excel"
            Response.BinaryWrite(DirectCast(rdr("File_Name"), Byte()))
        End If

    End Sub
Avatar of pradyahuja
pradyahujaFlag of Australia image

Our community of experts have been thoroughly vetted for their expertise and industry experience.

This problem has been solved!
Unlock 1 Answer and 33 Comments.
See Answers