Showing posts with label online. Show all posts
Showing posts with label online. Show all posts

Tuesday, March 27, 2012

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 farwith 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

Runningthrough debug, everything runs up until the last line, where an erroris caught saying : Failed to convert parameter value from aSqlParameter 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!!

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;

sqlsql

Sunday, February 19, 2012

Convert Int Values into words..?

Hi,

I'm doing an online invoice system with ASP and MS SQL.

So you got the price and total amount.

And let say the amount is $4000.00 , there is also a portion where the invoice needs to mention it in words, "Fourty Thousand"

Can it be "cast" ? "conver" ? or anything?
Or is there any workaround??$4000.00 = "Fourty Thousand"

I better should be careful while doing any transactions with you ;).

Maybe a function can be written to get it done ... will try it out ...

Highly unlikely you will get a readymade function for the same ...|||Opps..sorry...I was kindy sleepy..:)

thanks...I'll be trying out some code too...so many numbers condition to check in the if then else....|||don't write your own, there are so many scripts out there already, i'm sure you can adapt the logic, even if they aren't all written in wahtever language you plan to use...

http://developer.asna.com/Articles/b000106.asp
http://www.idinews.com/sourcecode/IntegerFunction.html
http://www.mvps.org/access/modules/mdl0001.htm
http://www.vbce.com/code/functions/convert_numbers_to_words/index.asp
http://www.vbexplorer.com/VBExplorer/tips/src36.asp

how did i find these so quickly?

google

Sunday, February 12, 2012

Convert decimal to VarChar without trailing 0's?

According to Books online...
"To remove trailing zeros from a result set when you convert from numeric or
decimal data to character data, use the value 128 for style."
So how come this code returns 10.2500000000? And more to the point how do I
get 10.25?
create table #test(
Val Decimal(19,10)
)
Insert into #test( Val ) Values ( 10.25 )
Select
Val,
Convert( VarChar, Val, 128 )
From #test
Any ideas?
Colin.Colin,
Try function STR instead.
select ltrim(str(cast(10.25 as Decimal(19,10)), 8, 2))
AMB
"Colin Dawson" wrote:

> According to Books online...
> "To remove trailing zeros from a result set when you convert from numeric
or
> decimal data to character data, use the value 128 for style."
> So how come this code returns 10.2500000000? And more to the point how do
I
> get 10.25?
> create table #test(
> Val Decimal(19,10)
> )
> Insert into #test( Val ) Values ( 10.25 )
> Select
> Val,
> Convert( VarChar, Val, 128 )
> From #test
>
> Any ideas?
> Colin.
>
>|||That works fine for the specific example, but if I have a number with a
different number of decimal places the results are not correct.
10.125 will get rounded, and this cannot be allowed to happen in the Medical
application that we're developing.
So far, the best solution that I've found is
Replace( RTrim( Replace( Replace( RTrim( Replace( Val, '0', ' ' ) ), ' ',
'0' ), '.', ' ' ) ), ' ', '.' ),
But this is rather CPU intensive. Looks like, I may be starting my own CLR
function library that contains all the stuff that MS missed from SQL2005.
Colin.
"Alejandro Mesa" <AlejandroMesa@.discussions.microsoft.com> wrote in message
news:09C08477-DDA9-40C6-8CAF-208AC263B1F5@.microsoft.com...
> Colin,
> Try function STR instead.
> select ltrim(str(cast(10.25 as Decimal(19,10)), 8, 2))
>
> AMB
> "Colin Dawson" wrote:
>