Converting bytes to string

Hi guys,

I'm currently trying to insert image into my SQL db.  I have tried a number of methods that were posted online, and so far with no luck.

My current code reads:

 
                    Dim conn As New Data.SqlClient.SqlConnection()
                    conn.ConnectionString = ConfigurationManager.ConnectionStrings("MainDBConnection").ToString
                    conn.Open()

                    Dim cmd As New Data.SqlClient.SqlCommand("SP_SAVEImage", conn)

                    cmd.CommandType = Data.CommandType.StoredProcedure

                    Dim sImageName As New Data.SqlClient.SqlParameter("@sImageName", Data.SqlDbType.VarChar, 50)
                    sImageName.Value = sImageName

                    Dim sImageType As New Data.SqlClient.SqlParameter("@sImageType", Data.SqlDbType.VarChar, 50)
                    sImageType.Value = fileType

                    Dim sImageData As New Data.SqlClient.SqlParameter("@sImageData", Data.SqlDbType.Image, uploadedFile.Length)
                    sImageData.Value = uploadedFile

                    cmd.Parameters.Add(sImageName)
                    cmd.Parameters.Add(sImageType)
                    cmd.Parameters.Add(sImageData)

                    Dim reader1 As Data.SqlClient.SqlDataReader

                    reader1 = cmd.ExecuteReader

Running through debug, everything runs up until the last line, where an error is caught saying : Failed to convert parameter value from a SqlParameter to a String

 I reckon it's to do with the input sImageData being input as a byte array - but I can't seem to find a way around it. Angry


Any help greatly appreciated!!

0
rich_frost
5/3/2007 10:29:43 PM
asp.net.sql-datasource 29906 articles. 0 followers. Follow

1 Replies
837 Views

Similar Articles

[PageSpeed] 38
Get it on Google Play
Get it on Apple App Store

In http://www.codeproject.com/useritems/images_in_sql_server.asp
private void GuardarImagen(byte[] matriz)
{
  this.cmd.CommandText = "insert into tabla(DESCRIPCION, IMAGEN) " +
                    "VALUES(@DESCRIPCION, @IMAGEN)";
  cmd.Parameters.Clear();
  cmd.Parameters.AddWithValue("Descripcion", this.NombreDeArchivoCorto).SqlDbType = SqlDbType.VarChar;
  cmd.Parameters.AddWithValue("Imagen", matriz).SqlDbType = SqlDbType.Image;
  cmd.ExecuteNonQuery();
}

suggests that yours should be
 Dim sImageName As New Data.SqlClient.SqlParameter("@sImageName", Data.SqlDbType.VarChar, 50)
 sImageName.Value = sImageName
 Dim sImageType As New Data.SqlClient.SqlParameter("@sImageType", Data.SqlDbType.VarChar, 50)
 sImageType.Value = fileType
 Dim sImageData As New Data.SqlClient.SqlParameter("@sImageData", Data.SqlDbType.Image)
 sImageData.Value = uploadedFile

If you are using SQL 2005
cmd.Parameters.AddWithValue("sImageName", sImageName).SqlDbType = SqlDbType.VarChar;
cmd.Parameters.AddWithValue("sImageType", sImageType).SqlDbType = SqlDbType.VarChar;
cmd.Parameters.AddWithValue("sImageData", uploadedFile).SqlDbType = SqlDbType.Image;

 


Don't forget to click "Mark as Answer" on the post that helped you.
This credits that member, earns you a point and marks your thread as Resolved so we will all know you have been helped.
0
TATWORTH
5/4/2007 8:23:42 AM
Reply:

Similar Artilces:

String to Linq.Byte and Linq.Byte to String convertion?
 Hi, I have a field in my database as image type. I am using Linq to read/insert records.I am having little hard time inserting text(string) into the image field and then converting back to string.Do you have any suggestions? Thanks in advance,   The below example shows how to convert Linq Binary to byte If Not String.IsNullOrEmpty(HttpContext.Current.Request.QueryString("imageID")) Then Dim imageID = HttpContext.Current.Request.QueryString("imageID") Dim db As New AuctionsDataContext Dim result = From img In db.Image...

Converting a gridView using an Sql Datasource to a gridview who is not using an SQL DataSource
How can i rewrite this page so that i doesnt make use of an SqlDataSource?  <%@ Control Language="C#" AutoEventWireup="true" CodeFile="LoginHistory.ascx.cs" Inherits="Private_UserControls_WebUserControl" %> <html> <head> <%--<link rel="stylesheet" href="BaldwinPortal.css" mce_href="BaldwinPortal.css" type="text/css" />--%> <title>Login History</title> </head> <body style="margin:0 0 0 0;padding:0 0 0 0"> ...

convert byte[] to String and string to byte[] note encoding and decoding required....
hi all,        As the subject, can I have solution ofr the same.......  e'i Convert byte[] into string and strting into byte[] , can i have teh sample page.        Thanks & Regards,Murthy.          System.Text.Encoding.Unicode.GetString(ByteArray);//Byte Array to String             System.Text.Encoding.Unicode.GetBytes(String);//String to Byte ArrayWe All move to our original place.Don't forget.Imran Baloch thanks for...

RSA Encryption
Here is my goal:1. Take a string2. Encrypt it 3. Pass it as a parameter in the QueryString4. Decrypt it The value starts as a string, then is converted to a byte[] and then encrypted.  The resulting byte[] is converted to a string and send as a parameter.  The recieving page decrypts the string (creates a byte[], decrypts to a new byte[], and the value is finally parsed for its values) I am using http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfsystemsecuritycryptographyrsacryptoserviceproviderclasstopic.asp with a small modification t...

How to append string to Response.OutputStream after string converted to byte array
Hi, I am trying to append string to Response.OutputStream property in PostRequestHandlerExecute event handler inside custom httpmodule. The goal of this implementation is to append string to output stream after the page is rendered when the response object is still held on the server. The pseudo code I wrote is the following: private void PostRequestHandlerExecute(object sender, EventArgs e) { HttpApplication application = ( HttpApplication ) sender; HttpContext context = application.Context;   string s = "This is test!"; byte[] buffer = Encoding.ASCII.GetByt...

Is is possible to run a .SQL file on SQL Server without converting file contents to a string?
Is it possible to run a .SQL file without converting the contents of the file to a string and then executing the string? Is it possible to run or execute the file? Thanks You can use the .sql file as an input file to ISQL using the -i switch "Kevin Wiebe" <kevin.wiebe@riseinc.com> wrote in message news:BjSh5G7GCHA.196@forums.sybase.com... > Is it possible to run a .SQL file without converting the contents of the > file to a string and then executing the string? Is it possible to run or > execute the file? > Thanks > > ...

System.Net.Mail is giving error CS0029: Cannot implicitly convert type 'string' to 'System.Net.Mail.MailAddress'
I'm getting an error while trying to use the wizard to create a form and than pass it the vaibles to Mail. Any help would be much appreciated. SmtpClient smtpClient = new SmtpClient(); MailMessage message = new MailMessage(); try {   message.From = txtEmail.Text; message.To.Add("admin1@yoursite.com"); message.Subject = "Feedback"; message.CC.Add("admin1@yoursite.com"); message.CC.Add("admin2@yoursite.com"); message.IsBodyHtml = false; message.Body = txtMessage.Text; smtpClient.Send(message); lblStatus.Text = "Email successfully sent."; } catch (Exception ex) { lbl...

Converting an Int to a String in SQL
Is there (is there) a command in t-sql to convert an Integer value to a string value?  Thank you, in advance for your help. select convert(nvarchar, intfield)Brian"Trust in the Lord and do what is good; dwell in the land and live securely. Take delight in the Lord, and He will give you your heart's desires" (Psalm 37: 3-4). Syntax for CAST: CAST ( expression AS data_type [ (length ) ]) Syntax for CONVERT: CONVERT ( data_type [ ( length ) ] , expression [ , style ] )http://msdn.microsoft.com/en-us/library/ms187928.aspxA fine is a tax for doing wrong. A tax is a f...

how to convert a number to string in SQL?
I search the help for T-sql, still don't know how to convert a number, for example 12345 to a string with formating like 12,345 Thanks for your help. The best place to do such formatting is in the front end, not at the database level.That being said, you could use a combination of the CONVERT and CAST functions (see CAST and CONVERT) to cast the number as a money type, then convert it to a varchar, and then strip off the decimal point and 2 0's at the end:DECLARE @test intSELECT @test = 12345SELECT REPLACE(CONVERT(varchar(50), CAST(@test AS money), 1),'.00','')Or, you could make use of a ...

Converting String to Minutes in .net
hi all, i have a string value(Ex:"915"),i need to convert in to minutes(EX:15:15) please provide me the code for that. Hi, As per my calculations 915 comes to 15:25 that is 15 hr 25 min and not 15:15. If you think I am correct, here is the code:Dim hr As Integer = CInt("915") / 60 Dim min As Integer = CInt("915") Mod 60 Regards...

Cannot Convert SQL IMAGE to Byte[]
I need help. code below. I have a pdf file stored as an image and when I try to download from the server and post to the page a get "System.InvalidCastException: Specified cast is not valid." <code> //upload file private void Button1_Click(object sender, System.EventArgs e) { //Get the filename of the pdf file to be uploaded. string strFilename = File1.PostedFile.FileName.Substring(File1.PostedFile.FileName.LastIndexOf("\\") +1); //Get the file type string strFileType = File1.PostedFile.ContentType; //Get the file si...

How do I convert string to a byte array?
[see also http://forums.sybase.com/cgi-bin/webnews.cgi?cmd=item-232695&group=sybase.public.powerbuilder.general] Working with an old OLE object for TCP comm, and the unicode vs ANSI charset change at PB 10 seems to be causing issues. BUT! the old object avoids "'wide' to ANSI" conversion and just sends what it's handed if it's handed a BYTE Array. However, getting there from a string seems to involve conversion to and parsing from a Blob, something that I'm fumbling. Some help on this conversion process would be appreciated. Convert the string to ...

converting to vb.net from c#.net authorize.net
authorize.net offered me some sample code when I signed up with them the only problem is the sample code is in c#.net but my page that they type all of their credit card into is vb.net <code><%@ Import Namespace="System.Net" %> <%@ Import Namespace="System.IO" %> <script language="C#" runat="server"> void Page_Load(Object Src, EventArgs E) { myPage.Text = readHtmlPage("https://certification.authorize.net/gateway/transact.dll"); } private String readHtmlPage(string url) { ...

how to convert hex string to hex bytes?
Hi I've been trying and haven't found a solution for this yet and hopefully some of you can give me a direction to solve this problem. I have a string in hex representation i.e, 64 chars 'AB23FFFE.....' How can I convert it to 32 chars String where each byte is $AB $23 $FF $FE .... ? Even if I do it manually, can I store the hex(non ASCII) to String? Thanks for looking!! edwin wrote: > I have a string in hex representation i.e, 64 chars 'AB23FFFE.....' > How can I convert it to 32 chars String where each byte is $AB $23 $FF > $FE .... ? Th...

Web resources about - Converting bytes to string - asp.net.sql-datasource

Facebook Begins Converting Users To HTTPS
Are you willing to sacrifice a little bit of speed for a lot more safety? Facebook is asking that very question with its announcement that it ...

Facebook No Longer Converting Groups Into Pages
Back when Facebook first launched Facebook Pages, many businesses and brands who had built up substantial audiences in their Facebook Groups ...

Vert - simply converting for iPhone, iPad, and iPod touch on the iTunes App Store
Get Vert - simply converting on the App Store. See screenshots and ratings, and read customer reviews.

Converting SIM Card to Micro SIM Card - Flickr - Photo Sharing!
Place new Micro SIM into the iPhone SIM card tray

Ayaan Hirsi Ali on Converting Muslims to Christianity - YouTube
Complete video at: http://fora.tv/2010/07/29/Nomad_From_Islam_to_America_with_Ayaan_Hirsi_Ali Ayaan Hirsi Ali explains her support of missionary ...

Click go fears of converting print files
Is there a way to convert a print queue item to a .RTF or .PDF file? I like to save or email them. - The Sydney Morning Herald

Sudanese woman ordered to hang under sharia law for converting to Christianity gives birth
Khartoum, Sudan: A Christian Sudanese woman sentenced to hang for apostasy has given birth in jail, a Western diplomat said on Tuesday.

Imams warn against radicalism to Aboriginal inmates converting to Islam
The prison system has enlisted the help of ASIO to crack down on radicalisation behind bars amid revelations that Aboriginals are converting ...

Converting the world's companies one by one - The Science Show - ABC Radio National (Australian Broadcasting ...
Image: Trucks carrying logs make their way up a road in Jambi, Indonesia. A vast area of the Sumatran forest, and orangutan habitat, is being ...

Rothesay building new arena, converting existing rink to fieldhouse
The Town of Rothesay plans to build a new arena and convert the existing one into a fieldhouse.

Resources last updated: 1/11/2016 7:43:44 PM