Conversion Problem

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • suganya
    New Member
    • Dec 2006
    • 39

    Conversion Problem

    Hi

    I have to insert two textbox values into the database. In sql server 2005, I have the table named Rs with the following fields

    Rs decimal(2, 2)
    date1 datetime

    In front end I have given the coding as

    Code:
    Imports System.Data
    Imports System.Configuration
    Imports System.Data.SqlClient
    Partial Class commision1
        Inherits System.Web.UI.Page
        Public strConnection As String
    
        Public Conn As SqlConnection
        Public Sql As String
        Public AdditionalSQL As String
    
        Protected Sub btncommission_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btncommission.Click
            If Page.IsValid Then
                Dim cmdItem As New SqlCommand
                Dim Rs As Decimal = txtRs.Text
                Dim Date1 As Date = txtDate1.Text
    
                cmdItem.Connection = GetConnection()
    
                cmdItem.CommandType = CommandType.StoredProcedure
                cmdItem.CommandText = "sp_RsAdd"
    
                Dim paramRs As New SqlParameter("@Rs", SqlDbType.Decimal)
                paramRs.Value = Convert.ToDecimal(Rs)
                cmdItem.Parameters.Add(paramRs)
    
                Dim paramDate1 As New SqlParameter("@Date1", SqlDbType.DateTime)
                paramDate1.Value =Date1.ToString
                cmdItem.Parameters.Add(paramDate1)
                cmdItem.ExecuteNonQuery()
    
            End If
        End Sub
        Public Function GetConnection() As SqlConnection
            strConnection = ConfigurationManager.AppSettings.Get("ConnectString")
            Conn = New SqlConnection(strConnection)
            Conn.Open()
            Return Conn
        End Function
    End Class
    The stored procedure I have given is

    Code:
    set ANSI_NULLS ON
    set QUOTED_IDENTIFIER ON
    go
    
    
    
    ALTER Procedure [dbo].[sp_RsAdd]
    	@Rs decimal(2,2),
    	@Date1 datetime
    	
    as
    Begin
    
    INSERT INTO Rs (Rs, Date1)
    VALUES (@rs, @Date1)
    
    End
    But when I run the project, I get the error as

    Error converting data type numeric to decimal.
    Last edited by kenobewan; Jul 31 '08, 11:37 AM. Reason: PLEASE USE [CODE] TAGS
  • myqueries
    New Member
    • Jul 2008
    • 11

    #2
    Hi..

    try converting txtRs.Text into decimal format only.May be the value in the text box is numeric.Hope this should be resolved.

    Comment

    • Curtis Rutland
      Recognized Expert Specialist
      • Apr 2008
      • 3264

      #3
      Lines 15 and 16 seem to be your problem. You should explicitly cast the decimal value and the date value using the Convert class.

      Comment

      Working...