473,287 Members | 1,659 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,287 software developers and data experts.

uploading image to mysql table

I know that uploading an image to a database has been covered, oh, about 3
trillion times. However, I haven't found anything covering uploading to a
MySQL database with .net. Please don't recommend storing the image to the
filesystem and only keeping a pointer to that in the table. I want to dump
the image to a table. My code dumps the data into the table, however, I get
the following error when trying to view the image "the image ... cannot be
displayed because it contains errors".

The image data is stored in a large-blob field, extension is stored in
varchar, and a description is stored in a varchar. I think the problem
has to do with special characters in the image data. MySQL seems to be real
picky about that stuff. Looking at
http://dev.mysql.com/doc/mysql/en/String_syntax.html, there's a blurb " If
you want to insert binary data into a string column (such as a BLOB), the
following characters must be represented by escape sequences: " towards the
bottom.

So, I guess I need to somehow parse the inputstream and/or byte array and
replace those characters as needed. This is where I'm stuck. I could write
a function to replace the data with the proper escape sequence but each byte
in the arrFile array contains an integer value as shown with the loop:

***********
for (int i=0; i<=intFileLen-1; i++)
Response.Write(arrFile[i]);
***********

Is there anyway to view the raw data or is that the raw data? Any ideas?
Thanks in advance. The code follows
// Determine File Type
int fileLen = txtFileContents.PostedFile.FileName.Length;
strFileExtension =
txtFileContents.PostedFile.FileName.Substring(file Len-4,4);

// Grab the contents of uploaded file
intFileLen = txtFileContents.PostedFile.ContentLength;

byte[] arrFile = new byte[intFileLen];
objStream = txtFileContents.PostedFile.InputStream;
objStream.Read( arrFile, 0, intFileLen );
objStream.Close();

// Add Uploaded file to database
strInsert = "Insert Into pics ( TITLE, IMAGE_TYPE, IMAGE ) Values (\""+
txtFileTitle.Text+"\", \""+strFileType+"\", \""+arrFile+"\" )";

cmdInsert = new OdbcCommand( strInsert, conMyData );

conMyData.Open();
cmdInsert.ExecuteNonQuery();

conMyData.Close();
}
Nov 16 '05 #1
10 7357
Hi,
I haven't done that, but as I see, you are using the ODBC driver for
MySQL. Have you tried their ADO.Net provider as well. As I looked thru
code, it supports binary blobs, so maybe this is the way to go.

Sunny
In article <uy**************@TK2MSFTNGP09.phx.gbl>,
no*********@hotmail.com says...
I know that uploading an image to a database has been covered, oh, about 3
trillion times. However, I haven't found anything covering uploading to a
MySQL database with .net. Please don't recommend storing the image to the
filesystem and only keeping a pointer to that in the table. I want to dump
the image to a table. My code dumps the data into the table, however, I get
the following error when trying to view the image "the image ... cannot be
displayed because it contains errors".

The image data is stored in a large-blob field, extension is stored in
varchar, and a description is stored in a varchar. I think the problem
has to do with special characters in the image data. MySQL seems to be real
picky about that stuff. Looking at
http://dev.mysql.com/doc/mysql/en/String_syntax.html, there's a blurb " If
you want to insert binary data into a string column (such as a BLOB), the
following characters must be represented by escape sequences: " towards the
bottom.

So, I guess I need to somehow parse the inputstream and/or byte array and
replace those characters as needed. This is where I'm stuck. I could write
a function to replace the data with the proper escape sequence but each byte
in the arrFile array contains an integer value as shown with the loop:

***********
for (int i=0; i<=intFileLen-1; i++)
Response.Write(arrFile[i]);
***********

Is there anyway to view the raw data or is that the raw data? Any ideas?
Thanks in advance. The code follows
// Determine File Type
int fileLen = txtFileContents.PostedFile.FileName.Length;
strFileExtension =
txtFileContents.PostedFile.FileName.Substring(file Len-4,4);

// Grab the contents of uploaded file
intFileLen = txtFileContents.PostedFile.ContentLength;

byte[] arrFile = new byte[intFileLen];
objStream = txtFileContents.PostedFile.InputStream;
objStream.Read( arrFile, 0, intFileLen );
objStream.Close();

// Add Uploaded file to database
strInsert = "Insert Into pics ( TITLE, IMAGE_TYPE, IMAGE ) Values (\""+
txtFileTitle.Text+"\", \""+strFileType+"\", \""+arrFile+"\" )";

cmdInsert = new OdbcCommand( strInsert, conMyData );

conMyData.Open();
cmdInsert.ExecuteNonQuery();

conMyData.Close();
}

Nov 16 '05 #2
Sunny--

Thanks for your reply. Are you talking about the ByteFX provider? No I
hadn't tried that but I just did. Same thing.

I did manage to figure out how to replace special characters in the data
with escape sequences...I think. Though, it still doesn't work. What I did
was convert the image data byte array into an ascii encoded string, used the
Replace method to replace the special chars with escape sequences and then
converted that back to a byte array. Like I said, though, it still didn't
work. Code is below:

// txtFileContents is the text upload control
//grab contents and put into byte array
intFileLen = txtFileContents.PostedFile.ContentLength;
byte[] arrFile = new byte[intFileLen];
objStream = txtFileContents.PostedFile.InputStream;
objStream.Read( arrFile, 0, intFileLen );
objStream.Close();

// now convert that to string and process escape sequences as needed
Encoding encEncoder = ASCIIEncoding.ASCII;
string str = encEncoder.GetString(arrFile,0,arrFile.GetLength(0 ));
string str2 =
str.Replace("\"","\\\"").Replace("'","\\'").Replac e("\\","\\\\").Replace(((c
har)0).ToString(),"\\0");

// now convert back to byte array
byte[] arrCrap = unicode.GetBytes(str2);

// insert into table...left out the ado connection stuff
strInsert = "Insert Into pics ( TITLE, IMAGE_TYPE, IMAGE ) Values (\""+
txtFileTitle.Text+"\", \""+strFileType+"\", \""+arrCrap+"\" )";

// end
//////////////////////////////

"Sunny" <su******@icebergwireless.com> wrote in message
news:O5**************@TK2MSFTNGP09.phx.gbl...
Hi,
I haven't done that, but as I see, you are using the ODBC driver for
MySQL. Have you tried their ADO.Net provider as well. As I looked thru
code, it supports binary blobs, so maybe this is the way to go.

Sunny
In article <uy**************@TK2MSFTNGP09.phx.gbl>,
no*********@hotmail.com says...
I know that uploading an image to a database has been covered, oh, about 3 trillion times. However, I haven't found anything covering uploading to a MySQL database with .net. Please don't recommend storing the image to the filesystem and only keeping a pointer to that in the table. I want to dump the image to a table. My code dumps the data into the table, however, I get the following error when trying to view the image "the image ... cannot be displayed because it contains errors".

The image data is stored in a large-blob field, extension is stored in
varchar, and a description is stored in a varchar. I think the problem has to do with special characters in the image data. MySQL seems to be real picky about that stuff. Looking at
http://dev.mysql.com/doc/mysql/en/String_syntax.html, there's a blurb " If you want to insert binary data into a string column (such as a BLOB), the following characters must be represented by escape sequences: " towards the bottom.

So, I guess I need to somehow parse the inputstream and/or byte array and replace those characters as needed. This is where I'm stuck. I could write a function to replace the data with the proper escape sequence but each byte in the arrFile array contains an integer value as shown with the loop:

***********
for (int i=0; i<=intFileLen-1; i++)
Response.Write(arrFile[i]);
***********

Is there anyway to view the raw data or is that the raw data? Any ideas? Thanks in advance. The code follows
// Determine File Type
int fileLen = txtFileContents.PostedFile.FileName.Length;
strFileExtension =
txtFileContents.PostedFile.FileName.Substring(file Len-4,4);

// Grab the contents of uploaded file
intFileLen = txtFileContents.PostedFile.ContentLength;

byte[] arrFile = new byte[intFileLen];
objStream = txtFileContents.PostedFile.InputStream;
objStream.Read( arrFile, 0, intFileLen );
objStream.Close();

// Add Uploaded file to database
strInsert = "Insert Into pics ( TITLE, IMAGE_TYPE, IMAGE ) Values (\""+ txtFileTitle.Text+"\", \""+strFileType+"\", \""+arrFile+"\" )";

cmdInsert = new OdbcCommand( strInsert, conMyData );

conMyData.Open();
cmdInsert.ExecuteNonQuery();

conMyData.Close();
}

Nov 16 '05 #3
Hi John,

In article <uC**************@TK2MSFTNGP09.phx.gbl>,
no*********@hotmail.com says...
Sunny--

Thanks for your reply. Are you talking about the ByteFX provider? No I
hadn't tried that but I just did. Same thing.
Yes, that was my idea.
// now convert back to byte array
byte[] arrCrap = unicode.GetBytes(str2);

// insert into table...left out the ado connection stuff
strInsert = "Insert Into pics ( TITLE, IMAGE_TYPE, IMAGE ) Values (\""+
txtFileTitle.Text+"\", \""+strFileType+"\", \""+arrCrap+"\" )";

And how this works??? How can you add byte[] a part of string
concatenation? Does the last line compile at all?

Sunny
Nov 16 '05 #4
Sunny--

Yup. The program compiles and runs fine. I don't know how or why it works
but this is the same code I got from my ASP.Net Unleashed book. Of course,
they are using a MSSQL server for the dbms and it has an "image" data type
built in. MySQL has only the BLOB type.

"Sunny" <su******@icebergwireless.com> wrote in message
news:%2****************@TK2MSFTNGP09.phx.gbl...
Hi John,

In article <uC**************@TK2MSFTNGP09.phx.gbl>,
no*********@hotmail.com says...
Sunny--

Thanks for your reply. Are you talking about the ByteFX provider? No I
hadn't tried that but I just did. Same thing.


Yes, that was my idea.
// now convert back to byte array
byte[] arrCrap = unicode.GetBytes(str2);

// insert into table...left out the ado connection stuff
strInsert = "Insert Into pics ( TITLE, IMAGE_TYPE, IMAGE ) Values (\""+
txtFileTitle.Text+"\", \""+strFileType+"\", \""+arrCrap+"\" )";

And how this works??? How can you add byte[] a part of string
concatenation? Does the last line compile at all?

Sunny

Nov 16 '05 #5
Hi John,

this code is broken. After executing this line the content of the
strInsert is:

Insert Into pics ( TITLE, IMAGE_TYPE, IMAGE ) Values ("myfie.txt",
"myImageType", "System.Byte[]" )

As you can see, putting a byte[] in the string concatenation actually
invokes the .ToString method of the Array class, which in this case
outputs only "System.Byte[]", but not the actual content of the array.

So, try to replace this:
strInsert = "Insert Into pics ( TITLE, IMAGE_TYPE, IMAGE ) Values (\""+
txtFileTitle.Text+"\", \""+strFileType+"\", \""+arrCrap+"\" )";

with this:
strInsert = "Insert Into pics ( TITLE, IMAGE_TYPE, IMAGE ) Values (\""+
txtFileTitle.Text+"\", \""+strFileType+"\", \""+str2+"\" )";

Now your modified string will be included in the insert statement.

I can not test it now, but at least removes an obvious problem.
Sunny
In article <ek*************@TK2MSFTNGP10.phx.gbl>,
no*********@hotmail.com says...
Sunny--

Yup. The program compiles and runs fine. I don't know how or why it works
but this is the same code I got from my ASP.Net Unleashed book. Of course,
they are using a MSSQL server for the dbms and it has an "image" data type
built in. MySQL has only the BLOB type.

"Sunny" <su******@icebergwireless.com> wrote in message
news:%2****************@TK2MSFTNGP09.phx.gbl...
Hi John,

In article <uC**************@TK2MSFTNGP09.phx.gbl>,
no*********@hotmail.com says...
Sunny--

Thanks for your reply. Are you talking about the ByteFX provider? No I
hadn't tried that but I just did. Same thing.


Yes, that was my idea.
// now convert back to byte array
byte[] arrCrap = unicode.GetBytes(str2);

// insert into table...left out the ado connection stuff
strInsert = "Insert Into pics ( TITLE, IMAGE_TYPE, IMAGE ) Values (\""+
txtFileTitle.Text+"\", \""+strFileType+"\", \""+arrCrap+"\" )";

And how this works??? How can you add byte[] a part of string
concatenation? Does the last line compile at all?

Sunny


Nov 16 '05 #6
Sunny--

I had wondered about that. The data that's actually in the table does indeed
show as "System.Byte[]". But when I double-clicked that in the MySQL
control center, it loaded the image viewer app. That led me to think there
was something else there. The book I'm using has this:

/////////////////////////////////////
// Add Uploaded file to database
conMyData = new SqlConnection( @"Server=localhost;Integrated
Security=SSPI;Database=myData" );
strInsert = "Insert Uploads ( u_title, u_documentType, u_document ) " +
"Values (@title, @fileType, @document )";
cmdInsert = new SqlCommand( strInsert, conMyData );
cmdInsert.Parameters.Add( "@title", txtFileTitle.Text );
cmdInsert.Parameters.Add( "@fileType", strFileType );
cmdInsert.Parameters.Add( "@document", arrFile );
conMyData.Open();
cmdInsert.ExecuteNonQuery();
conMyData.Close();
/////////////////////////////////////

But using the Parameters.Add failed when I tried so I ended up with what you
see. Is the code above not doing the exact same thing? Regardless I did
try the using the str2 variable. It didn't work either. I'm assuming
because it's a string and not binary data? Can you tell I don't know what
I'm doing? :)

Thanks for your willingness to help me on this.


"Sunny" <su******@icebergwireless.com> wrote in message
news:Om**************@TK2MSFTNGP09.phx.gbl...
Hi John,

this code is broken. After executing this line the content of the
strInsert is:

Insert Into pics ( TITLE, IMAGE_TYPE, IMAGE ) Values ("myfie.txt",
"myImageType", "System.Byte[]" )

As you can see, putting a byte[] in the string concatenation actually
invokes the .ToString method of the Array class, which in this case
outputs only "System.Byte[]", but not the actual content of the array.

So, try to replace this:
strInsert = "Insert Into pics ( TITLE, IMAGE_TYPE, IMAGE ) Values (\""+
txtFileTitle.Text+"\", \""+strFileType+"\", \""+arrCrap+"\" )";

with this:
strInsert = "Insert Into pics ( TITLE, IMAGE_TYPE, IMAGE ) Values (\""+
txtFileTitle.Text+"\", \""+strFileType+"\", \""+str2+"\" )";

Now your modified string will be included in the insert statement.

I can not test it now, but at least removes an obvious problem.
Sunny
In article <ek*************@TK2MSFTNGP10.phx.gbl>,
no*********@hotmail.com says...
Sunny--

Yup. The program compiles and runs fine. I don't know how or why it works but this is the same code I got from my ASP.Net Unleashed book. Of course, they are using a MSSQL server for the dbms and it has an "image" data type built in. MySQL has only the BLOB type.

"Sunny" <su******@icebergwireless.com> wrote in message
news:%2****************@TK2MSFTNGP09.phx.gbl...
Hi John,

In article <uC**************@TK2MSFTNGP09.phx.gbl>,
no*********@hotmail.com says...
> Sunny--
>
> Thanks for your reply. Are you talking about the ByteFX provider? No I > hadn't tried that but I just did. Same thing.

Yes, that was my idea.

> // now convert back to byte array
> byte[] arrCrap = unicode.GetBytes(str2);
>
> // insert into table...left out the ado connection stuff
> strInsert = "Insert Into pics ( TITLE, IMAGE_TYPE, IMAGE ) Values (\""+ > txtFileTitle.Text+"\", \""+strFileType+"\", \""+arrCrap+"\" )";
>
And how this works??? How can you add byte[] a part of string
concatenation? Does the last line compile at all?

Sunny


Nov 16 '05 #7
Now I see the first mistake :)
Parameters.Add does a lot more work than just replacing strings in the
command text. Now, lets try to solve the problem. I have no time now to
install MySql and test, so, please, check this:

1. After you read the file into the array, can you create an image from
this array? Without any manipulations?

2. If step one is OK: using ByteFX lib, recreate the last part of code
you have posted:

conMyData = new MySqlConnection ....

cmdInsert = new MySqlCommand.....

all the Add commands, etc.

If something fails, please report the exact exception you receive.

3. If step 2 is OK, and you can see some data in the DB, but still can
not view it, please read it back using ByteFX library and compare the
received data to originally stored data. You may try to create an image,
or you can write it to a file and compare both files to see what is the
difference.

Sunny

In article <Oa*************@tk2msftngp13.phx.gbl>,
no*********@hotmail.com says...
Sunny--

I had wondered about that. The data that's actually in the table does indeed
show as "System.Byte[]". But when I double-clicked that in the MySQL
control center, it loaded the image viewer app. That led me to think there
was something else there. The book I'm using has this:

/////////////////////////////////////
// Add Uploaded file to database
conMyData = new SqlConnection( @"Server=localhost;Integrated
Security=SSPI;Database=myData" );
strInsert = "Insert Uploads ( u_title, u_documentType, u_document ) " +
"Values (@title, @fileType, @document )";
cmdInsert = new SqlCommand( strInsert, conMyData );
cmdInsert.Parameters.Add( "@title", txtFileTitle.Text );
cmdInsert.Parameters.Add( "@fileType", strFileType );
cmdInsert.Parameters.Add( "@document", arrFile );
conMyData.Open();
cmdInsert.ExecuteNonQuery();
conMyData.Close();
/////////////////////////////////////

But using the Parameters.Add failed when I tried so I ended up with what you
see. Is the code above not doing the exact same thing? Regardless I did
try the using the str2 variable. It didn't work either. I'm assuming
because it's a string and not binary data? Can you tell I don't know what
I'm doing? :)

Thanks for your willingness to help me on this.


"Sunny" <su******@icebergwireless.com> wrote in message
news:Om**************@TK2MSFTNGP09.phx.gbl...
Hi John,

this code is broken. After executing this line the content of the
strInsert is:

Insert Into pics ( TITLE, IMAGE_TYPE, IMAGE ) Values ("myfie.txt",
"myImageType", "System.Byte[]" )

As you can see, putting a byte[] in the string concatenation actually
invokes the .ToString method of the Array class, which in this case
outputs only "System.Byte[]", but not the actual content of the array.

So, try to replace this:
strInsert = "Insert Into pics ( TITLE, IMAGE_TYPE, IMAGE ) Values (\""+
txtFileTitle.Text+"\", \""+strFileType+"\", \""+arrCrap+"\" )";

with this:
strInsert = "Insert Into pics ( TITLE, IMAGE_TYPE, IMAGE ) Values (\""+
txtFileTitle.Text+"\", \""+strFileType+"\", \""+str2+"\" )";

Now your modified string will be included in the insert statement.

I can not test it now, but at least removes an obvious problem.
Sunny
In article <ek*************@TK2MSFTNGP10.phx.gbl>,
no*********@hotmail.com says...
Sunny--

Yup. The program compiles and runs fine. I don't know how or why it works but this is the same code I got from my ASP.Net Unleashed book. Of course, they are using a MSSQL server for the dbms and it has an "image" data type built in. MySQL has only the BLOB type.

"Sunny" <su******@icebergwireless.com> wrote in message
news:%2****************@TK2MSFTNGP09.phx.gbl...
> Hi John,
>
>
>
> In article <uC**************@TK2MSFTNGP09.phx.gbl>,
> no*********@hotmail.com says...
> > Sunny--
> >
> > Thanks for your reply. Are you talking about the ByteFX provider? No I > > hadn't tried that but I just did. Same thing.
>
> Yes, that was my idea.
>
> > // now convert back to byte array
> > byte[] arrCrap = unicode.GetBytes(str2);
> >
> > // insert into table...left out the ado connection stuff
> > strInsert = "Insert Into pics ( TITLE, IMAGE_TYPE, IMAGE ) Values (\""+ > > txtFileTitle.Text+"\", \""+strFileType+"\", \""+arrCrap+"\" )";
> >
>
>
> And how this works??? How can you add byte[] a part of string
> concatenation? Does the last line compile at all?
>
> Sunny


Nov 16 '05 #8
Success!!!! Many, many thanks Sunny. That was the problem.

Final code for anyone interested is:
// upload.aspx
//////////////////////////////////////////////////////////////
void Button_Click(Object sender , EventArgs e)
{
string strFileExtension;
string strFileType;
int intFileLen;
Stream objStream;
string strInsert;

if ( txtFileContents.PostedFile != null )
{
// Determine File Type
int fileLen = txtFileContents.PostedFile.FileName.Length;
strFileExtension =
txtFileContents.PostedFile.FileName.Substring(file Len-4,4);
switch (strFileExtension.ToLower()) {
case ".bmp":
strFileType = "bmp";
break;
case ".jpg":
strFileType = "jpg";
break;
case ".gif":
strFileType = "gif";
break;
}

// Grab the contents of uploaded file
intFileLen = txtFileContents.PostedFile.ContentLength;
byte[] arrFile = new byte[intFileLen];
objStream = txtFileContents.PostedFile.InputStream;
objStream.Read( arrFile, 0, intFileLen );
objStream.Close();

// Add Uploaded file to database
string myConnectionString = "Database=a_mysql_db;Data
Source=localhost;User Id=userid;Password=passwd";

MySqlConnection myConnection = new MySqlConnection(myConnectionString);

string myInsertQuery = "Insert Into pics (TITLE,IMAGE_TYPE,IMAGE ) Values
(@title,@imagetype,@image)";
MySqlCommand myCommand = new MySqlCommand(myInsertQuery);
myCommand.Parameters.Add("@title","blah");
myCommand.Parameters.Add("@imagetype","jpg");
myCommand.Parameters.Add("@image",arrFile);

myCommand.Connection = myConnection;
myConnection.Open();
myCommand.ExecuteNonQuery();
myCommand.Connection.Close();

}

}
//////////////////////////////////////////////////////////////
// viewimage.aspx
void Page_Load(Object sender , EventArgs e)
{
int intItemID;
OdbcConnection conMyData;
string strSelect;
OdbcCommand cmdSelect;
OdbcDataReader dtrSearch;

intItemID = Convert.ToInt32(Request.Params["id"]);

conMyData = new OdbcConnection("DSN=a_dsn"); // this hasn't been switched
to the ado.net provider

strSelect = "SELECT IMAGE_TYPE, IMAGE, TITLE From pics "
+ "WHERE PIC_ID=35"; // hard coded record...will really pull from
querystring

cmdSelect = new OdbcCommand( strSelect, conMyData );
conMyData.Open();

dtrSearch = cmdSelect.ExecuteReader();

if ( dtrSearch.Read())
{
// set Content Type
Response.ClearHeaders();
Response.AppendHeader("Content-Transfer-Encoding","binary");

Response.ContentType = "image/jpeg";
Response.BinaryWrite( (byte[])dtrSearch["IMAGE"] );
}

dtrSearch.Close();
conMyData.Close();
Response.End();
}

// now you can call viewimage.aspx within an <img> tag like so:
<img src="viewimage.aspx?image_id=x" />

///////////////////////////////////////////////////////////////


"Sunny" <su******@icebergwireless.com> wrote in message
news:uJ**************@TK2MSFTNGP09.phx.gbl...
Now I see the first mistake :)
Parameters.Add does a lot more work than just replacing strings in the
command text. Now, lets try to solve the problem. I have no time now to
install MySql and test, so, please, check this:

1. After you read the file into the array, can you create an image from
this array? Without any manipulations?

2. If step one is OK: using ByteFX lib, recreate the last part of code
you have posted:

conMyData = new MySqlConnection ....

cmdInsert = new MySqlCommand.....

all the Add commands, etc.

If something fails, please report the exact exception you receive.

3. If step 2 is OK, and you can see some data in the DB, but still can
not view it, please read it back using ByteFX library and compare the
received data to originally stored data. You may try to create an image,
or you can write it to a file and compare both files to see what is the
difference.

Sunny

In article <Oa*************@tk2msftngp13.phx.gbl>,
no*********@hotmail.com says...
Sunny--

I had wondered about that. The data that's actually in the table does indeed show as "System.Byte[]". But when I double-clicked that in the MySQL
control center, it loaded the image viewer app. That led me to think there was something else there. The book I'm using has this:

/////////////////////////////////////
// Add Uploaded file to database
conMyData = new SqlConnection( @"Server=localhost;Integrated
Security=SSPI;Database=myData" );
strInsert = "Insert Uploads ( u_title, u_documentType, u_document ) " + "Values (@title, @fileType, @document )";
cmdInsert = new SqlCommand( strInsert, conMyData );
cmdInsert.Parameters.Add( "@title", txtFileTitle.Text );
cmdInsert.Parameters.Add( "@fileType", strFileType );
cmdInsert.Parameters.Add( "@document", arrFile );
conMyData.Open();
cmdInsert.ExecuteNonQuery();
conMyData.Close();
/////////////////////////////////////

But using the Parameters.Add failed when I tried so I ended up with what you see. Is the code above not doing the exact same thing? Regardless I did try the using the str2 variable. It didn't work either. I'm assuming
because it's a string and not binary data? Can you tell I don't know what I'm doing? :)

Thanks for your willingness to help me on this.


"Sunny" <su******@icebergwireless.com> wrote in message
news:Om**************@TK2MSFTNGP09.phx.gbl...
Hi John,

this code is broken. After executing this line the content of the
strInsert is:

Insert Into pics ( TITLE, IMAGE_TYPE, IMAGE ) Values ("myfie.txt",
"myImageType", "System.Byte[]" )

As you can see, putting a byte[] in the string concatenation actually
invokes the .ToString method of the Array class, which in this case
outputs only "System.Byte[]", but not the actual content of the array.

So, try to replace this:
strInsert = "Insert Into pics ( TITLE, IMAGE_TYPE, IMAGE ) Values (\""+ txtFileTitle.Text+"\", \""+strFileType+"\", \""+arrCrap+"\" )";

with this:
strInsert = "Insert Into pics ( TITLE, IMAGE_TYPE, IMAGE ) Values (\""+ txtFileTitle.Text+"\", \""+strFileType+"\", \""+str2+"\" )";

Now your modified string will be included in the insert statement.

I can not test it now, but at least removes an obvious problem.
Sunny
In article <ek*************@TK2MSFTNGP10.phx.gbl>,
no*********@hotmail.com says...
> Sunny--
>
> Yup. The program compiles and runs fine. I don't know how or why it

works
> but this is the same code I got from my ASP.Net Unleashed book. Of

course,
> they are using a MSSQL server for the dbms and it has an "image" data
type
> built in. MySQL has only the BLOB type.
>
>
>
> "Sunny" <su******@icebergwireless.com> wrote in message
> news:%2****************@TK2MSFTNGP09.phx.gbl...
> > Hi John,
> >
> >
> >
> > In article <uC**************@TK2MSFTNGP09.phx.gbl>,
> > no*********@hotmail.com says...
> > > Sunny--
> > >
> > > Thanks for your reply. Are you talking about the ByteFX
provider? No I
> > > hadn't tried that but I just did. Same thing.
> >
> > Yes, that was my idea.
> >
> > > // now convert back to byte array
> > > byte[] arrCrap = unicode.GetBytes(str2);
> > >
> > > // insert into table...left out the ado connection stuff
> > > strInsert = "Insert Into pics ( TITLE, IMAGE_TYPE, IMAGE )
Values (\""+
> > > txtFileTitle.Text+"\", \""+strFileType+"\",

\""+arrCrap+"\" )"; > > >
> >
> >
> > And how this works??? How can you add byte[] a part of string
> > concatenation? Does the last line compile at all?
> >
> > Sunny
>
>
>


Nov 16 '05 #9
Hi John,

this is good that it works.

Now, final words:

Always put your data access code in try/finaly block. Always.

Your last lines in the oNClick method shoud be:

try
{
myConnection.Open();
myCommand.ExecuteNonQuery();
}
catch //this is optional, if you want to
//report the error
{
// report the error
}
finally
{
myConnection.Close();
}

This way you quarantee that even if something goes wrong, the connection
will always be closed.
Good luck
Sunny
Nov 16 '05 #10
Cool.

Thanks again for your help!

"Sunny" <su******@icebergwireless.com> wrote in message
news:u1**************@TK2MSFTNGP10.phx.gbl...
Hi John,

this is good that it works.

Now, final words:

Always put your data access code in try/finaly block. Always.

Your last lines in the oNClick method shoud be:

try
{
myConnection.Open();
myCommand.ExecuteNonQuery();
}
catch //this is optional, if you want to
//report the error
{
// report the error
}
finally
{
myConnection.Close();
}

This way you quarantee that even if something goes wrong, the connection
will always be closed.
Good luck
Sunny

Nov 16 '05 #11

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Ralph Freshour | last post by:
I'm trying to code the ability for my users to upload up to photo's to mysql database - can someone point me in the right direction as to how this might be done in php? Perhaps a tutorial or some...
7
by: lion | last post by:
I get these errors when uploading images via a web page: (the page still uploads the images but reports these errors?) Warning: fopen(D:\php\uploadtemp\php1FC7.tmp) : failed to create stream: No...
3
by: Rubal Jain | last post by:
Hello, How do I upload images under MySQL DB using PHP Script. Any Sample script to upload and retrive images to MySQL DB would be appreciated. Thanks Rubal Jain www.Rubal.Net
3
by: Christopher Mouton | last post by:
We regularly make drive images of our entire server and store it at a backup site. To be honest I have never fully understood how the imaging programs deal with open files. My question is will the...
11
by: bissatch | last post by:
Hi, I am trying to upload an image, create a new file based on that image and then store the base64 encoded image data in a database. I dont really know where my code is going wrong so I will...
1
by: jimmyg123 | last post by:
Hey everyone, I've just written some basic code to upload a pdf file from a browser to a server. It works fine when I use Firefox, however when I use IE after clicking the upload button it returns...
221
Atli
by: Atli | last post by:
You may be wondering why you would want to put your files “into” the database, rather than just onto the file-system. Well, most of the time, you wouldn’t. In situations where your PHP application...
3
by: jatin299 | last post by:
hi ..problem in uploading image..using servlet to upload image in mysql..use html form so user given the path of image..but giving error.here is the code..help me on this. import java.sql.*;...
2
by: matech | last post by:
I have a problem with uploading special characters from excel files to mysql 5. It doesn't matter if I use UTF-8 or iso-8859-1 when uploading the trademark ™ symbol. htmlspecialchars() or...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...
0
by: marcoviolo | last post by:
Dear all, I would like to implement on my worksheet an vlookup dynamic , that consider a change of pivot excel via win32com, from an external excel (without open it) and save the new file into a...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.