script task error: unable to cast object of type system.string to system.byte.
Hi Guru's,
I am using a script task for downloading the template file from the sql server into the local disk. While geting inage data from DB I encountered following error
script task error: unable to cast object of type system.string to system.byte.
Please help me out
Roy
June 1st, 2012 12:02pm
To convert a string to a byte array in c# you can do it this way
static byte[] GetBytes(string str)
{
byte[] bytes = new byte[str.Length * sizeof(char)];
System.Buffer.BlockCopy(str.ToCharArray(), 0, bytes, 0, bytes.Length);
return bytes;
}
or this way:
byte[] utf8 = System.Text.Encoding.UTF8.GetBytes (yourstring);
Chuck Pedretti | Magenic North Region | magenic.com
Free Windows Admin Tool Kit Click here and download it now
June 1st, 2012 12:04pm
Chuck,
Thank you for the response., but I am doing it in script task component in SSIS.
June 1st, 2012 12:17pm
Chuck,
Thank you for the response., but I am doing it in script task component in SSIS.
can you provide your code? Last time I checked script task components were in VB or c# so the code above should work for youChuck Pedretti | Magenic North Region | magenic.com
Free Windows Admin Tool Kit Click here and download it now
June 1st, 2012 12:22pm
'Get image data from DB
Dim fileData
As
Byte() =
DirectCast(sqlCom.ExecuteScalar(),
Byte())
IF
Not fileData
Is
Nothing
Then
'Read image data into a file stream
Using fs
As
New FileStream(sTempFileName, FileMode.OpenOrCreate, FileAccess.Write)
fs.Write(fileData, 0, fileData.Length)
'Set image variable value using memory stream.
fs.Flush()
fs.Close()
End
Using
End
If
June 1st, 2012 12:26pm
Can I assume that this: sqlCom.ExecuteScalar() returns string?Chuck Pedretti | Magenic North Region | magenic.com
Free Windows Admin Tool Kit Click here and download it now
June 1st, 2012 12:31pm
yes it does
June 1st, 2012 12:34pm
Then try this: You will need to add an Imports System.Text to your class if it is not already there.
'Get image data from DB
Dim results as string = sqlCom.ExecuteScalar()
Dim fileData As Byte() = System.Text.Encoding.UTF8.GetBytes(results)
IF Not fileData Is Nothing Then
'Read image data into a file stream
Using fs As New FileStream(sTempFileName, FileMode.OpenOrCreate, FileAccess.Write)
fs.Write(fileData, 0, fileData.Length)
'Set image variable value using memory stream.
fs.Flush()
fs.Close()
End Using
End If
Chuck Pedretti | Magenic North Region | magenic.com
Free Windows Admin Tool Kit Click here and download it now
June 1st, 2012 12:36pm
I got it figured..Its all with the execute scalar(), which retrieves the first column in the resultset. But in my stored proc, template colmn was the third field..so finally changed it to the first column and executed fine...Thanks alot
Roy
June 1st, 2012 1:39pm
I got this error while installing MS SQL Server 2008 R2 Express. Any ideas?
Thanks.
Free Windows Admin Tool Kit Click here and download it now
September 19th, 2012 6:49pm