ERROR: Object must implement IConvertible.

C

charleswesley

I think the problem is with the strPrice variable in the AddToCart()
sub. I've been messing with it all morning and can't seem to figure it
out. Any ideas?

+++++++++++++++++++++++++++++++++++++++++++
+ EXCEPTION +
+++++++++++++++++++++++++++++++++++++++++++

Object must implement IConvertible.
Description: An unhandled exception occurred during the execution of
the current web request. Please review the stack trace for more
information about the error and where it originated in the code.

Exception Details: System.InvalidCastException: Object must implement
IConvertible.

Source Error:

Line 56:
Line 57: oConnection.Open()
Line 58: oCommand.ExecuteNonQuery()
Line 59: oConnection.Close()
Line 60: End Sub

+++++++++++++++++++++++++++++++++++++++++++
+ CODE +
+++++++++++++++++++++++++++++++++++++++++++

<script language="VB" runat="server">
Dim strPrice As String

Sub Page_Load(Sender As Object, e As EventArgs)
GetSingleProduct()
If Not IsPostBack Then
ddlQuantity.SelectedIndex = -1
End If
End Sub

Sub GetSingleProduct()
Dim strSQL As String
Dim oConnection As SqlConnection
Dim oCommand As SqlCommand
Dim oReader As SqlDataReader

strSQL = "SELECT [ID], prdName, prdPrice, prdDesc FROM PRODUCT WHERE
[ID]=" & Request("ID")
oConnection = New
SqlConnection(ConfigurationSettings.AppSettings("SQLConnection"))
oCommand = New SqlCommand(strSQL, oConnection)
oConnection.Open()
oReader = oCommand.ExecuteReader()

lblProduct.Text = ""

If oReader.Read
lblProduct.Text &= "<h1>" & oReader("prdName") & "</h1>" &
oReader("prdDesc")
lblPrice.Text = "<p>" & FormatCurrency(oReader("prdPrice")) & "</p>"
strPrice = oReader("prdPrice")
End If

oReader.Close()
oConnection.Close()
End Sub

Sub AddToCart(s As Object, e As EventArgs)
Dim strSQL AS String = "INSERT INTO CART (GUID, product, quantity,
cost) VALUES (@GUID, @product, @quantity, @cost)"
Dim oConnection As New
SqlConnection(ConfigurationSettings.AppSettings("SQLConnection"))
Dim oCommand As New SqlCommand(strSQL, oConnection)

oCommand.Parameters.Add(New SqlParameter("@GUID", SqlDbType.NVarChar))
oCommand.Parameters.Add(New SqlParameter("@product", SqlDbType.Int))
oCommand.Parameters.Add(New SqlParameter("@quantity", SqlDbType.Int))
oCommand.Parameters.Add(New SqlParameter("@cost", SqlDbType.Money))

oCommand.Parameters("@GUID").Value = Session("GUID")
oCommand.Parameters("@product").Value = Request("id")
oCommand.Parameters("@quantity").Value =
ddlQuantity.SelectedItem.Value
oCommand.Parameters("@cost").Value = strPrice

oConnection.Open()
oCommand.ExecuteNonQuery()
oConnection.Close()
End Sub
</script>

++++++++++++++++++++++++++++++++++++++++++++++
 
C

charleswesley

UPDATE:

+++ Exception

Disallowed implicit conversion from data type nvarchar to data type
money, table 'DB_128959.dbo.CART', column 'cost'. Use the CONVERT
function to run this query.
Description: An unhandled exception occurred during the execution of
the current web request. Please review the stack trace for more
information about the error and where it originated in the code.

Exception Details: System.Data.SqlClient.SqlException: Disallowed
implicit conversion from data type nvarchar to data type money, table
'DB_128959.dbo.CART', column 'cost'. Use the CONVERT function to run
this query.

Source Error:

Line 51:
Line 52: oConnection.Open()
Line 53: oCommand.ExecuteNonQuery()
Line 54: oConnection.Close()
Line 55: End Sub

+++Code

Sub AddToCart(s As Object, e As EventArgs)
Dim strSQL AS String = "INSERT INTO CART (GUID, product, quantity,
cost) VALUES (@GUID, @product, @quantity, @cost)"
Dim oConnection As New
SqlConnection(ConfigurationSettings.AppSettings("SQLConnection"))
Dim oCommand As New SqlCommand(strSQL, oConnection)

oCommand.Parameters.Add("@GUID", Session("GUID"))
oCommand.Parameters.Add("@product", Request("ID"))
oCommand.Parameters.Add("@quantity",ddlQuantity.SelectedItem.Value)
oCommand.Parameters.Add("@cost", strPrice)

oConnection.Open()
oCommand.ExecuteNonQuery()
oConnection.Close()
End Sub
 
C

charleswesley

SOLUTION:

Dim strSQL AS String = "INSERT INTO CART (GUID, product, quantity,
cost) VALUES (@GUID, @product, @quantity, CAST(@cost As Money))"
 
M

Mr Newbie

And I bet you are earning 650 a day writing e-commerce applications for some
blue chip ??

LOL


I think the problem is with the strPrice variable in the AddToCart()
sub. I've been messing with it all morning and can't seem to figure it
out. Any ideas?

+++++++++++++++++++++++++++++++++++++++++++
+ EXCEPTION +
+++++++++++++++++++++++++++++++++++++++++++

Object must implement IConvertible.
Description: An unhandled exception occurred during the execution of
the current web request. Please review the stack trace for more
information about the error and where it originated in the code.

Exception Details: System.InvalidCastException: Object must implement
IConvertible.

Source Error:

Line 56:
Line 57: oConnection.Open()
Line 58: oCommand.ExecuteNonQuery()
Line 59: oConnection.Close()
Line 60: End Sub

+++++++++++++++++++++++++++++++++++++++++++
+ CODE +
+++++++++++++++++++++++++++++++++++++++++++

<script language="VB" runat="server">
Dim strPrice As String

Sub Page_Load(Sender As Object, e As EventArgs)
GetSingleProduct()
If Not IsPostBack Then
ddlQuantity.SelectedIndex = -1
End If
End Sub

Sub GetSingleProduct()
Dim strSQL As String
Dim oConnection As SqlConnection
Dim oCommand As SqlCommand
Dim oReader As SqlDataReader

strSQL = "SELECT [ID], prdName, prdPrice, prdDesc FROM PRODUCT WHERE
[ID]=" & Request("ID")
oConnection = New
SqlConnection(ConfigurationSettings.AppSettings("SQLConnection"))
oCommand = New SqlCommand(strSQL, oConnection)
oConnection.Open()
oReader = oCommand.ExecuteReader()

lblProduct.Text = ""

If oReader.Read
lblProduct.Text &= "<h1>" & oReader("prdName") & "</h1>" &
oReader("prdDesc")
lblPrice.Text = "<p>" & FormatCurrency(oReader("prdPrice")) & "</p>"
strPrice = oReader("prdPrice")
End If

oReader.Close()
oConnection.Close()
End Sub

Sub AddToCart(s As Object, e As EventArgs)
Dim strSQL AS String = "INSERT INTO CART (GUID, product, quantity,
cost) VALUES (@GUID, @product, @quantity, @cost)"
Dim oConnection As New
SqlConnection(ConfigurationSettings.AppSettings("SQLConnection"))
Dim oCommand As New SqlCommand(strSQL, oConnection)

oCommand.Parameters.Add(New SqlParameter("@GUID", SqlDbType.NVarChar))
oCommand.Parameters.Add(New SqlParameter("@product", SqlDbType.Int))
oCommand.Parameters.Add(New SqlParameter("@quantity", SqlDbType.Int))
oCommand.Parameters.Add(New SqlParameter("@cost", SqlDbType.Money))

oCommand.Parameters("@GUID").Value = Session("GUID")
oCommand.Parameters("@product").Value = Request("id")
oCommand.Parameters("@quantity").Value =
ddlQuantity.SelectedItem.Value
oCommand.Parameters("@cost").Value = strPrice

oConnection.Open()
oCommand.ExecuteNonQuery()
oConnection.Close()
End Sub
</script>

++++++++++++++++++++++++++++++++++++++++++++++
 

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

Members online

Forum statistics

Threads
473,769
Messages
2,569,577
Members
45,052
Latest member
LucyCarper

Latest Threads

Top