Click here to Skip to main content
15,888,401 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
i am not able to insert decimal value in c# using .net framework.
every time these error is coming


An unhandled exception of type 'System.InvalidCastException' occurred in mscorlib.dll

Additional information: Unable to cast object of type 'System.Data.SqlClient.SqlParameter' to type 'System.IConvertible'.


table
CREATE TABLE [dbo].[itemmaster]
(
	[itemcode] [varchar](33) NULL,
	[name] [varchar](50) NULL,
	[salesprice] [decimal](18, 2) NULL,
	[salestax] [decimal](18, 2) NULL,
	[profit] [decimal](18, 2) NULL,
	[quantityonhand] [decimal](18, 2) NULL
) ON [PRIMARY]


What I have tried:

SqlConnection con = new SqlConnection("Data Source=.;Initial Catalog=DelhiEMP;Integrated Security=True");
            SqlCommand cmd = new SqlCommand("insert into itemmaster(itemcode,name) values(@itemcode,@name)", con);
            con.Open();
            itemmaster itemmastr = new itemmaster();
            itemmastr.itemcode = Convert.ToString(cmd.Parameters.AddWithValue("@itemcode", txtitemcode.Text.Trim()));
            itemmastr.name = Convert.ToString(cmd.Parameters.AddWithValue("@name", txtname.Text.Trim()));
            itemmastr.salesprice = Convert.ToDecimal(cmd.Parameters.AddWithValue("@salesprice", txtsaleprice.Text));
            
           
            SqlDataAdapter da = new SqlDataAdapter(cmd);  
            DataSet ds = new DataSet();
            da.Fill(ds, "itemmaster");
            dataGridView1.DataSource = null;
Posted
Updated 21-May-17 20:22pm
Comments
Maciej Los 22-May-17 2:30am    
What are you trying to achieve?

Please, do yourself a favour, and look at the documentation.
You should be converting user input to native types first (with error handling to report problems to the user) and then passing the native values to SQL via cmd.Parameters.AddWithValue.

That code looks like you guessed what to put and hoped for the best rather than looking up the function (which include examples) and implementing them.
 
Share this answer
 
I suspect adding parameters to an SqlCommand does not return the value added. You would be better to convert your values into itemmastr then add the parameters from itemmastr. Also no need to convert to string what is already a string.

Cannot see the rest of your code for the decimal field you must also be certain that the data entered is a number, better to use decimal.tryparse to verify first and stop if it fails.

C#
itemmastr.itemcode = txtitemcode.Text.Trim();
itemmastr.name = txtname.Text.Trim();
itemmastr.salesprice = Convert.ToDecimal(txtsaleprice.Text);

cmd.Parameters.AddWithValue("@itemcode", itemmastr.itemcode);
cmd.Parameters.AddWithValue("@name", itemmastr.name);
cmd.Parameters.AddWithValue("@salesprice", itemmastr.salesprice);


Forgot to add:
Your insert command does not include salesprice, it only inserts itemcode and name so you'll still get a failure on the cmd.parameters line for salesprice.
 
Share this answer
 
v2
Comments
faizy001 22-May-17 2:14am    
thank you :)
Void routines do not return values. You are trying to pass a non-existent return value to the Convert class. Your command object for an insert should look like more like this:
using (SqlConnection connection = new SqlConnection("Data Source=.;Initial Catalog=DelhiEMP;Integrated Security=True")){
                using (SqlCommand command = new SqlCommand("insert into itemmaster(itemcode,name) values(@itemcode,@name)", connection))
                {
                    command.Parameters.AddWithValue("@itemcode", txtitemcode.Text.Trim());
                    command.Parameters.AddWithValue("@name", txtname.Text.Trim());
                    command.Parameters.AddWithValue("@salesprice", txtsaleprice.Text);
                    connection.Open();
                    command.ExecuteNonQuery();
                    connection.Close();
                }
            }


The use of itemmaster has me confused on what your goal is. Also opening the db connection before you finish creating your command object is incorrect, and the assignment of null to to dataGridView1.DataSource doesn't necessarily seem like a great way to populate a datagrid.
 
Share this answer
 
v3

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