Print Page | Close Window

Converting a Money Value to Update a DB

Printed From: Web Wiz Forums
Category: General Discussion
Forum Name: ASP.NET Discussion
Forum Description: Discussion and chat on ASP.NET related topics.
URL: https://forums.webwiz.net/forum_posts.asp?TID=15317
Printed Date: 28 March 2026 at 11:07pm
Software Version: Web Wiz Forums 12.08 - https://www.webwizforums.com


Topic: Converting a Money Value to Update a DB
Posted By: Misty
Subject: Converting a Money Value to Update a DB
Date Posted: 01 June 2005 at 2:05am
I have an editable datagrid. I am getting the following error message every time I try to update the datagrid: Exception Details: System.Data.SqlClient.SqlException: Disallowed implicit conversion from data type nvarchar to data type money, table '.dbo.WebHosting', column 'Cost'. Use the CONVERT function to run this query.

Source Error:

Line 73:   cmdSql.Parameters.Add( "@WebHostingID", intWebHostingID )
Line 74:   conHosting.Open()
Line 75:   cmdSql.ExecuteNonQuery()


I need help with converting the datatype. Cost is a money datatype.

Here's the code that is causing the problem:

Dim txtCost As TextBox

Dim strCost As String

 

  intWebHostingID = dtrWebHosting.DataKeys( e.Item.ItemIndex )

  txtPlanName = e.Item.Cells( 1 ).Controls( 0 )

  txtDiskspace = e.Item.Cells( 2 ).Controls( 0 )

  txtCost = e.Item.Cells( 3 ).Controls( 0 )

  strPlanName = txtPlanName.Text

  strDiskspace = txtDiskspace.Text

  strCost = txtCost.Text

  strSql = "Update WebHosting Set PlanName=@PlanName, Diskspace=@Diskspace, Cost=@Cost Where WebHostingID=@WebHostingID"

  cmdSql = New SqlCommand( strSql, conHosting )

  cmdSql.Parameters.Add( "@PlanName", strPlanName )

  cmdSql.Parameters.Add( "@Diskspace", strDiskspace )

  cmdSql.Parameters.Add( "@Cost", strCost )

  cmdSql.Parameters.Add( "@WebHostingID", intWebHostingID )

  conHosting.Open()

  cmdSql.ExecuteNonQuery()

Here's my code for the boundcolumn:

  <asp:BoundColumn
    HeaderText="Cost"
    DataField="Cost"
    DataFormatString="{0:C}"/>




Replies:
Posted By: Mart
Date Posted: 01 June 2005 at 4:28am
try changing

cmdSql.Parameters.Add( "@Cost", strCost )

to

cmdSql.Parameters.Add( "@Cost", strCost, SqlDbType.Money )


Posted By: Misty
Date Posted: 01 June 2005 at 4:31pm
Unfortunately, it didn't work. Does anyone have any ideas on how to solve this?
I am getting the following error message now: Compiler Error Message: BC30451: Name 'SqlDbType' is not declared.

Source Error:

 
Line 78:   cmdSql.Parameters.Add( "@Cost", strCost, SqlDbType.Money )


Posted By: Mart
Date Posted: 01 June 2005 at 4:41pm
Have you imported System.Data?

put

<%@ Import Namespace="System.Data" %>

at the top, you may have only imported System.Data.SqlClient


Posted By: Misty
Date Posted: 01 June 2005 at 9:25pm
Originally posted by Mart Mart wrote:

Have you imported System.Data?

put

<%@ Import Namespace="System.Data" %>

at the top, you may have only imported System.Data.SqlClient
 
Thank you for reminding me about System.Data! I added it. I'm getting closer to fix the problem. I am still getting the following error message when I try to update the record: Exception Details: System.ArgumentOutOfRangeException: Specified argument was out of the range of valid values. Parameter name: @Cost: Invalid SqlDbType enumeration value: 45.

Source Error:

Line 77:   'objData.AddParam(oParam)
Line 78:  
Line 79:   cmdSql.Parameters.Add( "@Cost", strCost, SqlDbType.Money )


Posted By: Mart
Date Posted: 02 June 2005 at 4:15am
Does your stored procedure/query have a parameter called cost? and have you passed on all other relevant parameters? Does you query contain an error?


Posted By: Misty
Date Posted: 02 June 2005 at 12:27pm
Originally posted by Mart Mart wrote:

Does your stored procedure/query have a parameter called cost? and have you passed on all other relevant parameters? Does you query contain an error?
 
Yes, there's a parameter called cost. It is also the name of the database field in myh database. When I removed this particular parameter for adding cost, I have no problem with updating the database. I know that the problem is with the cost. I thought that the line of code that you gave me would work. I'm not sure what's wrong.


Posted By: Misty
Date Posted: 02 June 2005 at 4:38pm
I just solved the problem by using cmdSql.Parameters.Add("@Cost", CSng(strCost)).



Print Page | Close Window

Forum Software by Web Wiz Forums® version 12.08 - https://www.webwizforums.com
Copyright ©2001-2026 Web Wiz Ltd. - https://www.webwiz.net