I'm customizing a legacy ASP.NET MVC application that uses both raw SQL and models. I have some data to be committed that has two decimal places for example 4,615.38, 11.51. When I attempt to commit data im getting an error
Error converting datatype varchar to numeric
I need help on how to properly define my table and stored procedure. Should I use any casting in table definition or LEFT function?
In TaxTableController.cs i have :
Models.TaxTable.Zimra zimra = new Models.TaxTable.Zimra();
zimra.TableName = Helpers.SanitiseInput(Convert.ToString(formcollection["TableName"]));
zimra.TierName = Helpers.SanitiseInput(Convert.ToString(formcollection["TierName"]));
zimra.MinSalary = Convert.ToDouble(formcollection["MinSalary"]);
zimra.MaxSalary = Convert.ToDouble(formcollection["MaxSalary"]);
zimra.CreatedOn = DateTime.Now;
zimra.CreatedBy = Convert.ToString(Session["UserId"]);
if (ModelState.IsValid)
{
using (SqlConnection conn = new SqlConnection(Helpers.DatabaseConnect))
{
SqlCommand cmd = new SqlCommand("SaveZimraTable", conn);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@TableName", zimra.TableName);
cmd.Parameters.AddWithValue("@TierName", zimra.TierName);
cmd.Parameters.AddWithValue("@MaxSalary", zimra.MaxSalary);
cmd.Parameters.AddWithValue("@MinSalary", zimra.MinSalary);
cmd.Parameters.AddWithValue("@CreatedBy", zimra.CreatedBy);
cmd.Parameters.AddWithValue("@CreatedOn", zimra.CreatedOn);
My table definition is as below (using Script Table As--Create) :
[Id] [int] IDENTITY(1,1) NOT NULL,
[MinSalary] [decimal](18, 2) NOT NULL,
[MaxSalary] [decimal](18, 2) NOT NULL,
[CreatedBy] [varchar](50) NULL,
[TableName] [varchar](50) NULL,
[TierName] [varchar](50) NOT NULL,
[CreatedOn] [datetime] NULL,
CONSTRAINT [PK__Zimra__3214EC07397C51AA] PRIMARY KEY CLUSTERED
(
[Id] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
And the stored procedure is as below:
CREATE PROCEDURE [dbo].[SaveZimraTable]
@TableName varchar(50),
@TierName varchar(50),
@MinSalary decimal(18,2),
@MaxSalary decimal(18,2),
@CreatedBy varchar(50),
@CreatedOn datetime
AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
INSERT INTO Zimra VALUES (@TableName,@TierName,@MinSalary,@MaxSalary,@CreatedBy,@CreatedOn)
END
GO