Reputation: 191
I have a number in C# which is double longitude =1.041970849797e-05
.
When I try to insert it into a decimal(9,6)
column, I get the error message:
Error converting data type nvarchar to numeric
How do I correctly save the above as a decimal?
C# Application Code:
static void Main(string[] args)
{
double lat=1.041970849797e-05;
insertRecs(lat);
}
private static void insertRecs(double latitude)
{
Int32 rowsAffected = 0;
string connectionString = GetConnectionString();
using (SqlConnection connection = new SqlConnection(connectionString))
{
connection.Open();
SqlCommand cmd = new SqlCommand("dbo.usp_temp", connection);
cmd.CommandType = CommandType.StoredProcedure;
cmd.CommandTimeout = 90;
try
{
rowsAffected = cmd.ExecuteNonQuery();
}
catch (Exception ep)
{
Console.WriteLine("Exception in Insertrecords ");
Console.WriteLine(ep.Message);
}
}
}
SQL Stored Procedure:
create PROCEDURE [dbo].[usp_temp]
--The parameters for the stored procedure
@latitude decimal(9,6)
AS
BEGIN
insert into temptest(latitude) values(@latitude);
END
Upvotes: 0
Views: 112
Reputation: 3443
Looking at the sample of code you gave us you don't actually map the latitude
variable when running the Stored Procedure.
You would need to add a couple of lines like so:
SqlParameter param = cmd.Parameters.Add("@latitude", SqlDbType.Decimal);
param.Direction = ParameterDirection.Input;
param.Value = latitude;
And edited into your code:
using (SqlConnection connection = new SqlConnection(connectionString))
{
connection.Open();
SqlCommand cmd = new SqlCommand("dbo.usp_temp", connection);
cmd.CommandType = CommandType.StoredProcedure;
cmd.CommandTimeout = 90;
// Map the parameters into the Stored Procedure
SqlParameter param = cmd.Parameters.Add("@latitude", SqlDbType.Decimal);
param.Direction = ParameterDirection.Input;
param.Value = latitude;
try
{
rowsAffected = cmd.ExecuteNonQuery();
}
catch (Exception ep)
{
Console.WriteLine("Exception in Insertrecords ");
Console.WriteLine(ep.Message);
}
}
Upvotes: 1