Showing posts with label convert. Show all posts
Showing posts with label convert. Show all posts

Wednesday, March 21, 2012

Posting an Image to SQL Server 2005

Hello Everyone I am trying to write image files to sql server using the following code. I have recieved the following error message

Failed to convert parameter value from a String to a Byte[]. Please help. I am sure it is a problem gathering the iostream. I am not very familiar. Any help is gretaly appreciated.

Thanks in advance

Here is the code:

'Dim User As MembershipUser = Membership.GetUser(CreateUserWizard1.UserName)

'Dim t As TextBox = DirectCast(FormView1.FindControl("aspnet_UserID"), TextBox)

Dim objConnAs SqlConnection

Dim objComAs SqlCommand

IfMe.FileUpload1.HasFileThen

Dim fileExtensionAsString

Dim fileOKAsBoolean =False

fileExtension = System.IO.Path. _

GetExtension(FileUpload1.FileName).ToLower()

Dim allowedExtensionsAsString() = _

{".jpg",".jpeg",".png",".gif",".mwv"}

For iAsInteger = 0To allowedExtensions.Length - 1

If fileExtension = allowedExtensions(i)Then

fileOK =True

EndIf

Next

'Try

Dim imagestreamAs System.IO.Stream = FileUpload1.FileContent

Dim data()AsByte

ReDim data(imagestream.Length - 1)

imagestream.Read(data, 0, imagestream.Length)

imagestream.Close()

objConn =New SqlConnection(strNewConnection)

objCom =New SqlCommand("insert into ProfileImagesAndDocs(aspnet_userid,img_name,img_data,img_contenttype)values(@.aspnet_userid,@.imagename,@.Picture,@.CategoryName)", objConn)

'--------------

'this is the aspnet_userid

Dim useridparameterAs SqlParameter =New SqlParameter("@.aspnet_userid", SqlDbType.VarChar)useridparameter.Value =Me.aspnet_userid.Text

objCom.Parameters.Add(useridparameter)

'--------------

'This is the image name

Dim imagenameparameterAs SqlParameter =New SqlParameter("@.imagename", SqlDbType.VarChar)imagenameparameter.Value =Me.FileUpload1.FileName

objCom.Parameters.Add(imagenameparameter)

'--------------

'this is the picture data

Dim pictureParameterAs SqlParameter =New SqlParameter("@.Picture", SqlDbType.Image)

pictureParameter.Value = data

objCom.Parameters.Add(pictureParameter)

'--------------

'this is the profile area or category i.e. video intro

Dim categorynameParameterAs SqlParameter =New SqlParameter("@.CategoryName", SqlDbType.VarChar)

pictureParameter.Value ="IntroVideo"

objCom.Parameters.Add(categorynameParameter)

objConn.Open()

objCom.ExecuteNonQuery()

objConn.Close()

'bookmark

Label1.Text ="File uploaded!"

'Catch ex As Exception

Label1.Text ="File could not be uploaded."

EndIf

Hi,

You may use FileStream to read the image content and save it as SqlDbType.Image into your database. See the following sample.

Private Function GetFile()As String''' GetFileDim fileAs HttpPostedFile = File1.PostedFilefileName = file.FileNameReturn fileNameEnd Function''' ReadFilePrivate Function ReadFile()As Byte()Dim fileAs FileStream = File.OpenRead(GetFile())Dim contentAs Byte() =New Byte(file.Length - 1) {}file.Read(content, 0, content.Length)file.Close()Return contentEnd FunctionPrivate Sub WriteImage()Dim commAs SqlCommand = conn.CreateCommand()comm.CommandText ="insert into images(image,type) values(@.image,@.type)"comm.CommandType = CommandType.TextDim paramAs SqlParameter = comm.Parameters.Add("@.image", SqlDbType.Image)param.Value = ReadFile()param = comm.Parameters.Add("@.type", SqlDbType.NVarChar)param.Value = GetContentType(New FileInfo(fileName).Extension.Remove(0, 1))If comm.ExecuteNonQuery() = 1ThenResponse.Write("Successful")ElseResponse.Write("Fail")End Ifconn.Close()End SubPrivate Function GetContentType(ByVal extensionAs String)As StringDim typeAs String =""If extension.Equals("jpg")OrElse extension.Equals("JPG")Thentype ="jpeg"Elsetype = extensionEnd IfReturn"image/" + typeEnd Function

Thanks.
|||

Thanks the code above worked very well. I am having one other difficulty when I select a file with a *.wmv extension the file is not read and the script halts. Any Ideas?! and thanks again for the solution.

PostgreSQL to XML!

Hi, everybody!.
I=B4m very new using XML and PostgreSQL so I have some questions:
1) I would like to know if there is a way to convert a query SQL and
return the request in an XML document. I=B4m using PostgreSQL to
devoloping the database. How I could to do this?
2) What is the principal advantage to do this?
3) Somebody know if there is somebody working with this? (study case,
articles, papers, journals, universities).
Thanks.
JoanaJoana G. Malaverri wrote:
> Hi, everybody!.
> Im very new using XML and PostgreSQL so I have some questions:
> 1) I would like to know if there is a way to convert a query SQL and
> return the request in an XML document. Im using PostgreSQL to
> devoloping the database. How I could to do this?
See the documentation
<URL:http://www.postgresql.org/docs/8.2/...tatype-xml.html>.
Note that this newsgroup is about XML in Microsoft SQL Server, if you
need further help with Postgres then a newsgroup, forum or mailing list
dedicated to Postgres seems a better place to look for.
Martin Honnen -- MVP XML
http://JavaScript.FAQTs.com/

Postgresql migration tool?

I have a postgres db and would like to convert to SQL Express or standard edition. Is there a conversion or migration tool available?

I can dump the postgres db to a sql file but MS has slightly different syntax and datatypes so I have to go in a manually modify the sql file for every table and data type. Is there an easier way? Thanks!

I'm not aware of any such tool from Microsoft but I've moved this thread to the database engine forum where you're more likely to find a knowledgeable answer.

Paul

Saturday, February 25, 2012

Possible memory leak definite error when using CONVERT statement

Hi all,
I have a problem with my SQL Server 2000 installation that I can only think
might be a memory leak.
I was wondering if any of you have experienced similar problems and if so
how you overcame them?
Within 10 days of the server running we are having to restart the server to
flush a variable test out of memory.
The error appears when running SQL from Query Analyser, Stored procedures
called from aspx pages etc.
The error looks something like
"Unable to convert variable test to an integer variable"
As I'm sure you can guess this error only occurs when using CONVERT but rest
assured there is no variable test declared by any of the scripts we are
running on the server.
BRB
That's strange
Can you show us this script?
"BPBForum" <BPBForum@.discussions.microsoft.com> wrote in message
news:CE1DC59F-5035-429D-887A-0DAA55C0E102@.microsoft.com...
> Hi all,
> I have a problem with my SQL Server 2000 installation that I can only
think
> might be a memory leak.
> I was wondering if any of you have experienced similar problems and if so
> how you overcame them?
> Within 10 days of the server running we are having to restart the server
to
> flush a variable test out of memory.
> The error appears when running SQL from Query Analyser, Stored procedures
> called from aspx pages etc.
> The error looks something like
> "Unable to convert variable test to an integer variable"
> As I'm sure you can guess this error only occurs when using CONVERT but
rest
> assured there is no variable test declared by any of the scripts we are
> running on the server.
|||Hi
SQL Version and SP level, and we need to see the code where this occurs?
Don't forget, SQL Server does a lot of implicit conversions so explicit use
of CONVERT is not required.
Regards
Mike
"BPBForum" wrote:

> Hi all,
> I have a problem with my SQL Server 2000 installation that I can only think
> might be a memory leak.
> I was wondering if any of you have experienced similar problems and if so
> how you overcame them?
> Within 10 days of the server running we are having to restart the server to
> flush a variable test out of memory.
> The error appears when running SQL from Query Analyser, Stored procedures
> called from aspx pages etc.
> The error looks something like
> "Unable to convert variable test to an integer variable"
> As I'm sure you can guess this error only occurs when using CONVERT but rest
> assured there is no variable test declared by any of the scripts we are
> running on the server.

Possible memory leak definite error when using CONVERT statement

Hi all,
I have a problem with my SQL Server 2000 installation that I can only think
might be a memory leak.
I was wondering if any of you have experienced similar problems and if so
how you overcame them?
Within 10 days of the server running we are having to restart the server to
flush a variable test out of memory.
The error appears when running SQL from Query Analyser, Stored procedures
called from aspx pages etc.
The error looks something like
"Unable to convert variable test to an integer variable"
As I'm sure you can guess this error only occurs when using CONVERT but rest
assured there is no variable test declared by any of the scripts we are
running on the server.BRB
That's strange
Can you show us this script?
"BPBForum" <BPBForum@.discussions.microsoft.com> wrote in message
news:CE1DC59F-5035-429D-887A-0DAA55C0E102@.microsoft.com...
> Hi all,
> I have a problem with my SQL Server 2000 installation that I can only
think
> might be a memory leak.
> I was wondering if any of you have experienced similar problems and if so
> how you overcame them?
> Within 10 days of the server running we are having to restart the server
to
> flush a variable test out of memory.
> The error appears when running SQL from Query Analyser, Stored procedures
> called from aspx pages etc.
> The error looks something like
> "Unable to convert variable test to an integer variable"
> As I'm sure you can guess this error only occurs when using CONVERT but
rest
> assured there is no variable test declared by any of the scripts we are
> running on the server.|||Hi
SQL Version and SP level, and we need to see the code where this occurs?
Don't forget, SQL Server does a lot of implicit conversions so explicit use
of CONVERT is not required.
Regards
Mike
"BPBForum" wrote:
> Hi all,
> I have a problem with my SQL Server 2000 installation that I can only think
> might be a memory leak.
> I was wondering if any of you have experienced similar problems and if so
> how you overcame them?
> Within 10 days of the server running we are having to restart the server to
> flush a variable test out of memory.
> The error appears when running SQL from Query Analyser, Stored procedures
> called from aspx pages etc.
> The error looks something like
> "Unable to convert variable test to an integer variable"
> As I'm sure you can guess this error only occurs when using CONVERT but rest
> assured there is no variable test declared by any of the scripts we are
> running on the server.

Possible memory leak definite error when using CONVERT statement

Hi all,
I have a problem with my SQL Server 2000 installation that I can only think
might be a memory leak.
I was wondering if any of you have experienced similar problems and if so
how you overcame them?
Within 10 days of the server running we are having to restart the server to
flush a variable test out of memory.
The error appears when running SQL from Query Analyser, Stored procedures
called from aspx pages etc.
The error looks something like
"Unable to convert variable test to an integer variable"
As I'm sure you can guess this error only occurs when using CONVERT but rest
assured there is no variable test declared by any of the scripts we are
running on the server.BRB
That's strange
Can you show us this script?
"BPBForum" <BPBForum@.discussions.microsoft.com> wrote in message
news:CE1DC59F-5035-429D-887A-0DAA55C0E102@.microsoft.com...
> Hi all,
> I have a problem with my SQL Server 2000 installation that I can only
think
> might be a memory leak.
> I was wondering if any of you have experienced similar problems and if so
> how you overcame them?
> Within 10 days of the server running we are having to restart the server
to
> flush a variable test out of memory.
> The error appears when running SQL from Query Analyser, Stored procedures
> called from aspx pages etc.
> The error looks something like
> "Unable to convert variable test to an integer variable"
> As I'm sure you can guess this error only occurs when using CONVERT but
rest
> assured there is no variable test declared by any of the scripts we are
> running on the server.|||Hi
SQL Version and SP level, and we need to see the code where this occurs?
Don't forget, SQL Server does a lot of implicit conversions so explicit use
of CONVERT is not required.
Regards
Mike
"BPBForum" wrote:

> Hi all,
> I have a problem with my SQL Server 2000 installation that I can only thin
k
> might be a memory leak.
> I was wondering if any of you have experienced similar problems and if so
> how you overcame them?
> Within 10 days of the server running we are having to restart the server t
o
> flush a variable test out of memory.
> The error appears when running SQL from Query Analyser, Stored procedures
> called from aspx pages etc.
> The error looks something like
> "Unable to convert variable test to an integer variable"
> As I'm sure you can guess this error only occurs when using CONVERT but re
st
> assured there is no variable test declared by any of the scripts we are
> running on the server.