Stack Exchange
@ProductCategory : Garments - Input string was not in a correct format
Back again.
 Here is my code. The error is, while adding data to the database using the form, the combobox disappoints me.
Can anyone help me out here. The problem is with this line "com.Parameters.AddWithValue("@ProductCategory", cmbProductCategory.SelectedItem.ToString());"
            SqlCeCommand com = new SqlCeCommand("INSERT INTO Products_Master(ProductCode, ProductName, ProductDescription, ProductCategory, LandingPrice, SellingPrice, Stock) VALUES(@ProductCode, @ProductName, @ProductDescription, @ProductCategory, @LandingPrice, @SellingPrice, @Stock)", con);
            com.Parameters.AddWithValue("@ProductCode", txtProductCode.Text);
            com.Parameters.AddWithValue("@ProductName", txtProductName.Text);
            com.Parameters.AddWithValue("@ProductDescription", txtProductDescription.Text);
            com.Parameters.AddWithValue("@ProductCategory", cmbProductCategory.SelectedItem.ToString());
            com.Parameters.AddWithValue("@LandingPrice", txtLandingPrice.Text);
            com.Parameters.AddWithValue("@SellingPrice", txtSellingPrice.Text);
            com.Parameters.AddWithValue("@Stock", txtStock.Text);

            try
            {
                int affectedRows = com.ExecuteNonQuery();
                if (affectedRows > 0)
                {
                    System.Windows.Forms.MessageBox.Show("Insert Success !", System.Windows.Forms.Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Information);
                    txtCategoryName.Text = "";
                    Category_Master_Changed();
                }


Can you post the error?

Its in the heading. "@ProductCategory : Garments - Input string was not in a correct format". Here Garments is the value i selected from the combobox.

what is the data type of ProductCategory in your data table and how you bind cmbProductCategory?

Can you add a line string cmbSelected = cmbProductCategory.SelectedItem.ToString(); and then tell us what is in the cmbSelected

I would check if the fields in the database are all of text type. Using AddWithValue means that the parameter is passed accordingly to the datatype of the value. So if you have a decimal (price) and you pass a string it is possible to get this error

I am sorry guys... Thanks @Damith and @ Steve for making me remember. I had kept my productCategory datatype to int, so that i could enter just the code for the category. later i tought it is confusing and changed the plan to just category name.

Please add your solution into an answer and mark it as correct so that other users know that this question has been answered.

Solution was simple. I deleted the column from database and made it as type "nvarchar".
Coding is the same.
