Platform used : Microsoft Visual Studio 2010 (C#)
Database : SQL Server 2008 R2 Express
I want to get the text from the drop down and put it into database table. I am getting error saying
Invalid column name ‘Type’.
though column is present in database table.
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 = ddlType.SelectedItem.ToString();
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();
I believe your problem is that you are using a SQL Server reserved word (“Size”) and you have not properly encapsulated it in your
INSERTstatement.Try this:
Notice the
[&]around the column names… this will tell SQL Server to treat them as columns.WARNING: I highly recommend you change these column names if you can. Using reserved words of any product is very poor database design and will continue to cause you problems and/or extra work. Better to fix it sooner than later.