Emmmanuel FIADUFE

Emmmanuel FIADUFE

  • 782
  • 973
  • 44.1k

Failed to convert parameter value from a string to a DateTime

May 22 2024 3:05 PM

Hello Team,

In my windows Form project I haave been encountring this error and tried serveral means but no avail.

this is the error message: Failed to covert parameter value from a string to a DateTime, 

my DataType for yhe date columns is date.

 private void btnAdd_Click(object sender, EventArgs e)
        {
            if (txtProduct.Text == "" || txtProductDetails.Text == "" || txtSellingPrice.Text == "" || txtBuyingPrice.Text == "" || txtQty.Text == "" || txtQuantityInHistory.Text == "")
            {
                MessageBox.Show("Missing Information !!!");
            }
            else
            {
                try
                {
                    decimal Profit = Convert.ToDecimal(txtSellingPrice.Text) - Convert.ToDecimal(txtBuyingPrice.Text);
                    con.Open();
                    SqlCommand cmd = new SqlCommand("insert into tblItem(ItName, ItCat, ItQty,ItQtyInHistory,ItBprice,ItSprice,ItProfit,ItDetails,ItStockInDate)values(@name,@catid, @qty,@qtyInHistory, @bPrice,@sPrice,@profit,@details,@stockinDate)", con);
                    cmd.Parameters.AddWithValue("@id", id);
                    cmd.Parameters.AddWithValue("@name", txtProduct.Text);
                    cmd.Parameters.AddWithValue("@catid", Convert.ToInt32(cmbCategory.SelectedValue));
                    cmd.Parameters.AddWithValue("@qty", txtQty.Text);
                    cmd.Parameters.AddWithValue("@qtyInHistory", txtQuantityInHistory.Text);
                    cmd.Parameters.AddWithValue("@bPrice", txtBuyingPrice.Text);
                    cmd.Parameters.AddWithValue("@sPrice", txtSellingPrice.Text);
                    cmd.Parameters.AddWithValue("@profit", Profit);
                    cmd.Parameters.AddWithValue("@details", txtProductDetails.Text);                  
                    cmd.Parameters.Add("@StockInDate", SqlDbType.Date, 50).Value = dtStockInDate.Value.ToString("dd-MM-yyyy");
                    cmd.ExecuteNonQuery();
                    con.Close();
                    MessageBox.Show("Product successfully saved !!!");
                    Clear();
                    GetCategory();
                    
                }

                catch (Exception Ex)
                {

                    con.Close();
                    MessageBox.Show(Ex.Message);
                }
            }
        }
 


Answers (2)