Problems with paramerters with and Insert Statement....

M

Manuel Canas

Hi there,

This is the Code ;
cnSQL = New SqlConnection(ConnectionString)
cmSQL = New SqlCommand(strSQL, cnSQL)

strSQL = "INSERT tb_product VALUES(@ServiceCode,@ServiceName,@Price)"

cmSQL = New SqlCommand(strSQL, cnSQL)

Dim m As New SqlParameter("@Price", txtPrice.Text)

m.DbType = System.Data.SqlDbType.Money

cmSQL.Parameters.Add(m)

Dim s As New SqlParameter("@ServiceCode", txtServiceCode.Text)

m.DbType = System.Data.SqlDbType.Char

cmSQL.Parameters.Add(s)

Dim n As New SqlParameter("@ServiceName", txtServiceName.Text)

m.DbType = System.Data.SqlDbType.Char

cmSQL.Parameters.Add(n)

cnSQL.Open()

cmSQL.ExecuteNonQuery()

When I execute it, it throws an error saying "String was not recognized as
a valid boolean"

Any body out there that can tell what am I doing wrong here.



Thanks very much,

Manny
 
W

William Ryan eMVP

Manuel Canas said:
Hi there,

This is the Code ;
cnSQL = New SqlConnection(ConnectionString)
cmSQL = New SqlCommand(strSQL, cnSQL)

strSQL = "INSERT tb_product VALUES(@ServiceCode,@ServiceName,@Price)"

cmSQL = New SqlCommand(strSQL, cnSQL)

Dim m As New SqlParameter("@Price", txtPrice.Text)

m.DbType = System.Data.SqlDbType.Money

cmSQL.Parameters.Add(m)

Dim s As New SqlParameter("@ServiceCode", txtServiceCode.Text)

m.DbType = System.Data.SqlDbType.Char

Is this definitely what you want to do? I'm guessing you want to set s's
type but instead you are resetting m's type
http://www.knowdotnet.com/articles/parametergotcha.html . YOu have a few
ways to declare those guys and I'd shoot for the concise ones so they're
more clesar
cmSQL.Parameters.Add("@Price", SqlDbType.Money).Value = txtPrice.Text
'However I think you should be casting this - make sure that you have Option
Strict On

then cmSQL.Parameters.Add("@ServiceCode", SqlDbType.Char).Value =
txtServiceCode.Text
I think this should fix it but let's look at one more thing
cmSQL.Parameters.Add(s)

Dim n As New SqlParameter("@ServiceName", txtServiceName.Text)

m.DbType = System.Data.SqlDbType.Char

cmSQL.Parameters.Add(n)
Try
cnSQL.Open()
Catch ex as SqlException
Debug.Assert(false, ex.ToString())
End Try
Try
cmSQL.ExecuteNonQuery()
Catch exc as SqlException
Debug.Assert(false, ex.ToString())
End Try

This should help eliminate where the problem is although I'm certain it's on
ExecuteNonquery. Anyway, see if that doesn't fix it and let me know if it
doesn't.
When I execute it, it throws an error saying "String was not recognized as
a valid boolean"

Any body out there that can tell what am I doing wrong here.



Thanks very much,

Manny

--
W.G. Ryan MVP Windows - Embedded

http://forums.devbuzz.com
http://www.knowdotnet.com/dataaccess.html
http://www.msmvps.com/williamryan/
 
M

Manuel Canas

Thanks William for your answer.

In what you proposed here,
cmSQL.Parameters.Add("@Price", SqlDbType.Money).Value = txtPrice.Text
'However I think you should be casting this - make sure that you have Option
Strict On

then cmSQL.Parameters.Add("@ServiceCode", SqlDbType.Char).Value =
txtServiceCode.Text

Just a problem though, now I get a error saying that I have not initialized
my commandtext. I have looked at some samples but I just can't figure it
out.

Can you tell me where the error is? I'll tell you is falling on the
ExecuteNonQuery() for sure.

Thanks William.

Manuel
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top