Insert records using ExecuteNonQuery, showing exception that invalid column name

Posted by tina on Stack Overflow See other posts from Stack Overflow or by tina
Published on 2011-01-11T04:16:40Z Indexed on 2011/01/11 5:53 UTC
Read the original article Hit count: 324

Filed under:
|

Hi all,

I am using SQL Server 2008.

I want to insert records into a table using ExecuteNonQuery, for that I have written:

customUtility.ExecuteNonQuery("insert into furniture_ProductAccessories(Product_id, Accessories_id, SkuNo, Description1, Price, Discount) values(" + prodid + "," + strAcc + "," + txtSKUNo.Text + "," + txtAccDesc.Text + "," + txtAccPrices.Text + "," + txtAccDiscount.Text + ")");

& following is ExecuteNonQuery function:

    public static bool ExecuteNonQuery(string SQL)
    {
        bool retVal = false;

        using (SqlConnection con = new SqlConnection(System.Web.Configuration.WebConfigurationManager.ConnectionStrings["dbConnect"].ToString()))
        {
            con.Open();
            SqlTransaction trans = con.BeginTransaction();
            SqlCommand command = new SqlCommand(SQL, con, trans);
            try
            {
                command.ExecuteNonQuery();
                trans.Commit();
                retVal = true;
            }
            catch(Exception ex)
            {
                //HttpContext.Current.Response.Write(SQL + "<br>" + ex.Message);
                //HttpContext.Current.Response.End();
            }
            finally
            {
                // Always call Close when done reading.
                con.Close();
            }
            return retVal;
        }
    }

but it showing exception that invalid column name to Description1 and even it's value which coming from txtAccDesc.Text. I have tried by removing Description1 column, other records are getting inserted successfully.

Could you please help me?

Thanks.

© Stack Overflow or respective owner

Related posts about sql

Related posts about sql-server-2008