0
votes

Getting error while inserting values into database (SQL Server 2008)

Implicit conversion from data type varchar to varbinary is not allowed. Use the CONVERT function to run this query.

Code:

da.InsertCommand = new SqlCommand("insert into tblProductMaster ([Name],[Type],[Height],[Width],[Length],[Price]) values( @Name , @Type , @Height , @Width , @Length , @Price )", con);

da.InsertCommand.Parameters.Add("@Name", SqlDbType.VarChar).Value = txtName.Text;
da.InsertCommand.Parameters.Add("@Type", SqlDbType.VarChar, 10).Value = Convert.ToString(ddlType.SelectedItem);
da.InsertCommand.Parameters.Add("@Height", SqlDbType.Float).Value = float.Parse(txtHeight.Text);
da.InsertCommand.Parameters.Add("@Width", SqlDbType.Float).Value = float.Parse(txtWidth.Text);
da.InsertCommand.Parameters.Add("@Length", SqlDbType.Float).Value = float.Parse(txtLength.Text);
da.InsertCommand.Parameters.Add("@Price", SqlDbType.SmallMoney).Value = int.Parse(txtPrice.Text);

da.InsertCommand.ExecuteNonQuery();
1
Can we see the CREATE TABLE script for tblProductMaster? - mgnoonan
Same than mgnoonan. Please export table schema in SQL and paste it here so we can check that. To do that you can use Sql Server Management Studio, right click on db > Task > Generate Script > Select specific object > then select the table etc. - Fabske
can we see schema for tblProductMaster ? - Dhananjay
Please don't tack things like "C# MSSQL" at the ends of your titles. That's what the tags are for. - John Saunders
@marc_s - in 2005 you don't have to, is that something new in 2008 ? - JonH

1 Answers

0
votes

I would imagine that the Name or Type columns in your table are of type varbinary and you are trying to set them to be a varchar