Re: SqlDataReader.GetBytes Results in an System.InvalidCastExcepti

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance



Hotwheels,

For the bytes arrays you need a memorystreem to convert them to images

http://msdn.microsoft.com/en-us/library/system.io.memorystream(VS.80).aspx

Cor


"Hotwheels" <Hotwheels@xxxxxxxxxxxxxxxxxxxxxxxxx> wrote in message news:6513E2BB-B3CF-49A4-B0CC-3EF9F933B194@xxxxxxxxxxxxxxxx
What I am trying to do is download the Attachments dirctly from Microsoft CRM
Interaction Table ActivityMimeAttachment. The code below does work for 70%
of the Attachments that have a type of 'application/octet-stream'. But it
does not work for the other 30% that have a type of image/jpeg,
application/pdf, image/gif, application/vnd.ms-excel, application/msword,
text/plain. Is there someplace where these MimeTypes are Defined So I can
convert them.

Help

private void SqlBlob2File(string DestFilePath, string id)
{

SqlConnection cn = new SqlConnection("server=localhost;integrated
security=yes;database=RelevantSales_MSCRM");
SqlCommand cmd = new SqlCommand("SELECT Body FROM ActivityMimeAttachment
WHERE ActivityMimeAttachmentID = '852161B4-D911-DB11-9EE5-0011434DCFE4'", cn);
cn.Open();
SqlDataReader dr = cmd.ExecuteReader();
bool bResult = dr.Read();

FileStream fs = new FileStream(DestFilePath, System.IO.FileMode.Create,
System.IO.FileAccess.Write);
try
{
byte[] value;
string s8Data;

s8Data = dr.GetString(0);
value = System.Convert.FromBase64String(s8Data);

fs.Write(value, 0, value.Length);

dr.Close();
cn.Close();

fs.Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
dr.Close();
cn.Close();

fs.Close();
}
}

"Cor Ligthert[MVP]" wrote:

Hi,

Maybe it is easier as you tell the goal you want to achieve too.

Do you want to read a part of text, do you want to retrieve an image etc.

Cor


"Hotwheels" <Hotwheels@xxxxxxxxxxxxxxxxxxxxxxxxx> wrote in message
news:ADDD20B4-30D9-4818-A489-B56B7E46B9EB@xxxxxxxxxxxxxxxx
> Trying to write a routine to take bytes from a text field and put them
> into a
> file.
>
> I am using VS 2003 and SQL 2005. I created a table called TestText and
> added two fields ID as a UID and MyText of type Text.
>
> I then added one row with the ID 8ac11006-26ad-da11-9ec8-00059a3c7800 > and
> MyText value of "1234".
>
> If I put dr.GetValue(0) in the watch area it show the correct result of
> "1234".
>
> So the DataReader is working correct.
>
> What am I doing wrong with SqlDataReader.GetBytes Method?
>
> Also how would I wirte the command if I just wanted return the length > of
> bytes
> example maybe "long retval = dr.GetBytes(0, 0, null, 0, 0);"
>
> Thanks
> Mike
>
> private void SqlBlob2File(string DestFilePath, string id)
> {
>
> SqlConnection cn = new SqlConnection("server=localhost;integrated
> security=yes;database=RelevantSales_MSCRM");
> SqlCommand cmd = new SqlCommand("SELECT MyText FROM TestText WHERE ID > =
> '8ac11006-26ad-da11-9ec8-00059a3c7800'", cn);
> cn.Open();
> SqlDataReader dr = cmd.ExecuteReader();
> bool bResult = dr.Read();
> FileStream fs = new FileStream(DestFilePath, > System.IO.FileMode.Create,
> System.IO.FileAccess.Write);
> try
> {
> byte[] value = new byte[8];
> long retval = dr.GetBytes(0, 0, value, 0, value.Length);
> fs.Write(value, 0, value.Length);
>
> dr.Close();
> cn.Close();
>
> fs.Close();
> }
> catch (Exception ex)
> {
> MessageBox.Show(ex.Message);
> dr.Close();
> cn.Close();
>
> fs.Close();
> }
> }



.



Relevant Pages

  • Re: SqlDataReader.GetBytes Results in an System.InvalidCastExcepti
    ... private void SqlBlob2File(string DestFilePath, string id) ... SqlCommand cmd = new SqlCommand("SELECT Body FROM ActivityMimeAttachment ... MyText value of "1234". ... So the DataReader is working correct. ...
    (microsoft.public.dotnet.framework.adonet)
  • Re: Loading image in Orca/Pascal
    ... necessary to load images using Orca - again, ... Also required is the P2GSString to convert the Pascal string into a GS/ ... if ToolError = 0 then begin ... srcHdl:= NewHandle(srcSize, ...
    (comp.sys.apple2.programmer)
  • Re: How to reference a Table in a Sub procedure
    ... If you have a table named Images in the current db then "SELECT * FROM ... make sure you assign your sql string to a variable: ... completed strSQL. ... figured out how passing the variables to the sub procedure works ...
    (microsoft.public.access.formscoding)
  • Re: storing path only to database
    ... is about 5gb..the type of file that i have to store is images ... cannot store all the images because of the limitation...now...i want to ... Dim MyFolder As String ...
    (microsoft.public.access.modulesdaovba)
  • Re: how to set background of TD without an ID to none
    ... So whatever is assigned to the document.body.background attribute must be a string. ... table cell, but nothing instead, or second option, a transparent gif ... document.write which does something with images messes it up, ... happyimages = el.getElementsByTagName; ...
    (comp.lang.javascript)