Click here to Skip to main content
15,887,410 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I am using stored procedure and calling it on asp.net.I am getting this error "Error converting data type nvarchar to decimal." there is table call art in which i have parameter as:
create procedure arts(
@name varchar(50),
@description varchar(50),
@Price decimal(10,2)
)
as
   begin
       insert into arts(name,description,Price) values(@name,@description,@Price)
   end

and I am calling it as:
C#
SqlCommand cmd=new SqlCommand("spInsertArts",con);
cmd.CommandType=System.Data.CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@name",txtname.Text);
cmd.Parameters.AddWithValue("@description",txtdesc.Text);
cmd.Parameters.AddWithValue("@Price",txtprice.Text);
con.Open();
cmd.ExecuteNonQuery();


When I execute it,it is giving me above exception.please help
Posted
Updated 28-Apr-14 19:08pm
v2

That is because Price is decimal type in Stored Procedure, but you are passing string value.
C#
cmd.Parameters.AddWithValue("@Price",txtprice.Text);

Refer answer Using SqlDBType.Decimal in Prepared Statement C#[^] to know how to pass decimal value.
 
Share this answer
 
Comments
Member 10724668 29-Apr-14 0:21am    
thanx for the link.I have modified my code but I am getting exception as An SqlParameter with ParameterName 'Price' is not contained by this SqlParameterCollection.
Can you post the modifies code here?
Member 10724668 29-Apr-14 1:32am    
decimal value = Convert.ToDecimal(txtprice.Text);<br><br>
cmd.Parameters.AddWithValue("@Price",value);<br><br>
No, do like below...

decimal price;

if (Decimal.TryParse(txtprice.Text, out price))
{
SqlParameter priceParameter = new SqlParameter("@Price", SqlDbType.Decimal);
priceParameter.Precision = 10;
priceParameter.Scale = 2;
priceParameter.Value = price;
cmd.Parameters.Add(priceParameter);
}
Аslam Iqbal 29-Apr-14 0:31am    
good link
you need to set the parameter values with correct type. try below for Decimal parameter with AddWithValue method
C#
decimal yourValue = Convert.ToDecimal(txtprice.Text);
cmd.Parameters.AddWithValue("@Price",yourValue);


Also note that your stored procedure name is arts but in your code you put as spInsertArts, make sure that you have given correct stored procedure name as in database

SqlCommand cmd=new SqlCommand("spInsertArts",con);
 
Share this answer
 
v2
Comments
Member 10724668 29-Apr-14 1:35am    
i have tried this but i am getting error as Input string was not in a correct format.
DamithSL 29-Apr-14 1:41am    
what is the value you have in txtprice.Text?
Hi try this. In this code, first we check is it int or not.

A. if it is int we convert it in decimal,
B. else we can pass null or can show any warning message.


int number;
string yourVale = txtprice.Text;

bool result = int.TryParse(yourValue, out number);

if (result)
{
cmd.Parameters.AddWithValue("@Price", Convert.ToDecimal(yourValue));
}
else
{
cmd.Parameters.AddWithValue("@Price", null); // or you can show any warning message.

}
 
Share this answer
 
v3
Comments
Member 10724668 29-Apr-14 2:19am    
thanx a lot.it worked :)
Santosh K. Tripathi 29-Apr-14 4:25am    
hi, if it worked for you plz, accept answer and rate it.
Member 10724668 29-Apr-14 4:31am    
ok.But there is one problem. when I try to insert decimal value like 4.5 or 3.2.It gives error as Procedure or function 'spInsertArts' expects parameter '@Price', which was not supplied.
Santosh K. Tripathi 29-Apr-14 5:34am    
i made some change in it. define two functions convertToDecimal() & convertToString they will return you needed value. you can use this function in you application any where you need a value without error.

SqlCommand cmd=new SqlCommand("spInsertArts",con);
cmd.CommandType=System.Data.CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@name",convertToString(txtname.Text));
cmd.Parameters.AddWithValue("@description",convertToString(txtdesc.Text));
cmd.Parameters.AddWithValue("@Price",convertToDecimal (txtprice.Text));
con.Open();
cmd.ExecuteNonQuery();


---------------------------------------------------

function 1.

private decimal convertToDecimal(string testValue)
{
decimal result = decimal.Zero;

if (testValue != null || testValue != "")
{
decimal number;
if (decimal.TryParse(testValue, out number))
{
result = Convert.ToDecimal(testValue);
}
}
else
{
result = Convert.ToDecimal(DBNull.Value);
// Note if you will pass Alphabet or other characters it will return you 0 (Zero).
}

return result;
}



function 2.

private string convertToString(string testValue)
{
string result = string.Empty;

if (testValue != null || testValue != "")
{
result = Convert.ToString(testValue);
}
else
{
result = Convert.ToString(DBNull.Value);
}

return result;
}
Santosh K. Tripathi 29-Apr-14 5:38am    
try this one
SQL
cmd.CommandType = CommandType.StoredProcedure;
           cmd.Parameters.Add("@Price", SqlDbType.Decimal).Value = Convert.ToDecimal(text1.text);
 
Share this answer
 
you change txtprice.Text into decimal and try using the converted variable to pass as parameter.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900