Hello Team,
When I try saving into the database it work perfectly but return this respond saying that input string was not in the correct format.
private void btn_Save_Click(object sender, EventArgs e)
{
if (txt_Productname.Text == "" || txt_CostPrice.Text == "" || txt_SalesPrice.Text == "")
{
MessageBox.Show("Missing Information !!!");
}
else
{
try
{
if (MessageBox.Show("Please confirm if you want to save this product?", "Confirm", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
{
decimal Profit = Convert.ToDecimal(txt_SalesPrice.Text) - Convert.ToDecimal(txt_CostPrice.Text);
cn.Open();
cm = new SqlCommand("INSERT INTO tblProduct(PCode,ProductDate,ProductName, Category, CostPrice,Profit,SalesPrice,Tax,TotalPrice,Barcode)VALUES(@pCode,@productDate,@productName, @category, @costPrice,@profit,@salesPrice,@tax,@totalPrice,@barcode)", cn);
cm.Parameters.AddWithValue("@pCode", txt_ProductCode.Text);
cm.Parameters.AddWithValue("@productDate", DbType.Date).Value = dtProductDate.Value;
cm.Parameters.AddWithValue("@productName", txt_Productname.Text);
cm.Parameters.AddWithValue("@category", cbo_Category.Text);
cm.Parameters.AddWithValue("@costPrice", txt_CostPrice.Text);
cm.Parameters.AddWithValue("@profit", Profit);
cm.Parameters.AddWithValue("@salesPrice", txt_SalesPrice.Text);
cm.Parameters.AddWithValue("@tax", cbo_Tax.Text);
cm.Parameters.AddWithValue("@totalPrice", txt_TotalPrice.Text);
// cm.Parameters.AddWithValue("@stockIn", txt_Stock.Text);
cm.Parameters.AddWithValue("@barcode", txt_Barcode.Text);
cm.ExecuteNonQuery();
cn.Close();
MessageBox.Show("Product has been successfully saved!", "POS", MessageBoxButtons.OK, MessageBoxIcon.Information);
Clear();
fm.LoadCategory();
}
}
catch (Exception ex)
{
cn.Close();
MessageBox.Show(ex.Message);
}
}
}
and
private void cbo_Tax_SelectedIndexChanged(object sender, EventArgs e)
{
try
{
decimal salesPrice = decimal.Parse(txt_SalesPrice.Text);
decimal tax = decimal.Parse(cbo_Tax.Text);
decimal total = (salesPrice * tax / 100) + salesPrice;
txt_TotalPrice.Text = total.ToString("#,##0.00");
}
catch (Exception ex)
{
throw ex;
}
}
Jithu ThomasPosted May 30, 2024, 6:03 PM
Mr. Emmanual
As per prasad we connected and solve your problem please accept my answer if you found the same was helpfull
Emmmanuel FIADUFEPosted May 30, 2024, 6:28 PM
Yes Thomas that's true, thank you so much and I really appreciate your help
Emmmanuel FIADUFEPosted May 29, 2024, 10:18 AM
Thank you Prasad, it is the clear function that is causing the problem
Prasad RaveendranPosted May 29, 2024, 12:12 AM
It's possible that the
Clear()orfm.LoadCategory()methods might be causing issues, especially if they are clearing or reloading values in a way that interferes with subsequent event handlers. To investigate, let's look at what these methods do and make sure they don't inadvertently clear necessary data or trigger events that cause parsing to fail.Please set breakpoints in these two methods and then click the Save button. Debug these methods to identify the cause of the error message.
Please share the code for Clear() as well here.
Emmmanuel FIADUFEPosted May 28, 2024, 10:20 AM
Hello Prasad Raveendran
The response that is coming from the combobox after trying your new code is saying that salesPrice and tax must be numeric value but I enter numeric value and it did save in the database as numeric value so how come this after save error response is coming
Prasad RaveendranPosted May 27, 2024, 10:37 PM
Hello Emmanuel,
My apologies. My first question should have been which C# version you are using. The syntax I provided was introduced in C# 7.0.
Here is the modified code. Please try and let me know
Emmmanuel FIADUFEPosted May 26, 2024, 2:13 PM
Hello Thomas.
Please your format isn't working form me
Emmmanuel FIADUFEPosted May 26, 2024, 2:02 PM
Hello Prasad
This is the error that is poping up
The combobox
The save button
The save buttons
Jithu ThomasPosted May 26, 2024, 8:52 AM
I tried this way and it works for me: -
Jithu ThomasPosted May 26, 2024, 6:59 AM
Mr. Emmaanuel
The problem as you are not converting the text value into decimal. try this -
Convert.ToDecimal(textBoxPrice.Text);
This will solve the issue.
Prasad RaveendranPosted May 26, 2024, 3:47 AM
Could you please try this way.
Save Button Click Event
Tax ComboBox SelectedIndexChanged Event
LoadCategory method looks fine, but ensure that the category names in your tblCategory table are all valid strings and there's no unexpected data.
Final Notes
Parameter Types in SQL: Use the correct data types when adding parameters to your SQL command, especially for dates and decimals.
User Input Validation: Always validate user input before processing it, ensuring that non-numeric values don't cause exceptions when converting to numeric types.
Error Handling: Handle parsing errors gracefully to provide clear feedback to the user instead of allowing exceptions to occur.
Emmmanuel FIADUFEPosted May 25, 2024, 5:55 PM
Hello Mr. Thomas,
Please as I change the way you said it, this is the error that popup
Emmmanuel FIADUFEPosted May 25, 2024, 3:47 PM
Hello Thomas.
Error Message is this and the table struction
Jithu ThomasPosted May 25, 2024, 2:43 PM
Mr. Emmmanuel FIADUFE
Can u show me a screenshot of the error. also check the stucture of the database table.
Regards,
Emmmanuel FIADUFEPosted May 25, 2024, 10:47 AM
Hello Thomas,
I tried that format before but the error still came
Emmmanuel FIADUFEPosted May 25, 2024, 10:45 AM
Hello Kumar
public void LoadCategory()
{
cbo_Category.Items.Clear();
try
{
int i = 0;
// dataGridView1.Rows.Clear();
cn.Open();
cm = new SqlCommand("select * from tblCategory", cn);
dr = cm.ExecuteReader();
while (dr.Read())
{
i++;
cbo_Category.Items.Add(dr["CategoryName"].ToString());
}
dr.Close();
cn.Close();
}
catch (Exception ex)
{
cn.Close();
MessageBox.Show(ex.Message);
}
}
Jithu ThomasPosted May 25, 2024, 6:22 AM
Dear Emmmanuel FIADUFE,
i strongly guess the below line of code is creating the problem.
cm.Parameters.AddWithValue("@productDate", DbType.Date).Value = dtProductDate.Value;
please change the same as follows: -
cm.Parameters.AddWithValue("@productDate", dtProductDate.Value.ToString("yyyy-MM-dd"));
Jignesh KumarPosted May 25, 2024, 6:17 AM
Hi,
Then you need to check in this method fm.LoadCategory();
Could you please share the code for this method ?