Click here to Skip to main content
15,888,113 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
con.Open();
              SqlCommand cmd = new SqlCommand("INSERT INTO tblPurchase (Pur_No,Bill_Challan_No,Pur_Date,Sup_Name,Item_Name,Qty,Rate,Total)VALUES('" + textBox1.Text + "','" + textBox2.Text + "','" + dateTimePicker1.Value.ToString("MM/dd/yyyy") + "','" + textBox6.Text + "','" + textBox3.Text + "','" + textBox4.Text + "','" + textBox5.Text + "','" + textBox7.Text + "')", con);
              cmd.ExecuteNonQuery();
              MessageBox.Show("Purchase Succesfully", "Successfully", MessageBoxButtons.OK, MessageBoxIcon.Information);
              Gridview();
              con.Close();


What I have tried:

I want to show result before save database in Total field.
Like Quantity*Rate=Total
Posted
Updated 20-Jan-17 2:10am
Comments
[no name] 13-Jan-17 9:03am    
"I want to show result before save database in Total field.", okay so go ahead and do that if that is what you want to do. There is nothing stopping you. What might stop you though, is that SQL injection attack invitation you have coded up there.
So where is the issue?

Don't do it like that! Never concatenate strings to build a SQL command. It leaves you wide open to accidental or deliberate SQL Injection attack which can destroy your entire database. Use Parametrized queries instead:
C#
using (SqlCommand cmd = new SqlCommand("INSERT INTO tblPurchase (Pur_No,Bill_Challan_No,Pur_Date,Sup_Name,Item_Name,Qty,Rate,Total)VALUES(@PN, @BCN, @PD, @SN, @IN, @QTY, @RA, @TOT), con))
   {
   cmd.Parameters.AddWithValue("@PN", textBox1.Text);
   cmd.Parameters.AddWithValue("@BCN", textBox2.Text);
   cmd.Parameters.AddWithValue("@PD", dateTimePicker1.Value);
   cmd.Parameters.AddWithValue("@SN", textBox6.Text);
   cmd.Parameters.AddWithValue("@IN", textBox3.Text);
   cmd.Parameters.AddWithValue("@QTY", textBox4.Text);
   cmd.Parameters.AddWithValue("@RA", textBox5.Text);
   cmd.Parameters.AddWithValue("@TOT", textBox7.Text);
   cmd.ExecuteNonQuery();
   ...

But even then, that's bad. Instead, your text boxes should be changed to values using TryParse methods and errors reported to the user first.
C#
int qty;
if (!int.TryParse(textBox4.Text, out qty))
   {
   ... report problem to user
   return;
If you do that for all numeric values, then your problem of displaying (or just checking) that quantity times rate equaling the total becomes trivial - you already know how to do that.

BTW: Do yourself a favour, and stop using Visual Studio default names for everything - you may remember that "TextBox8" is the mobile number today, but when you have to modify it in three weeks time, will you then? Use descriptive names - "tbMobileNo" for example - and your code becomes easier to read, more self documenting, easier to maintain - and surprisingly quicker to code because Intellisense can get to to "tbMobile" in three keystrokes, where "TextBox8" takes thinking about and 8 keystrokes...
 
Share this answer
 
Comments
Methoun Ahmed 27-Jan-17 8:12am    
Please,show full code

int qty;
if (!int.TryParse(textBox4.Text, out qty))
{
... report problem to user
return;
OriginalGriff 27-Jan-17 8:24am    
I can't show you how to report a problem to the user: I have no idea what environment you are working in! :laugh:
How would you normally tell a user "that's not a valid input" in your code?
As pointed out by OriginalGriff, always use parameters. One reason is to be safe from SQL injections, but there are a few other thing you should note

- at the moment you rely on implicit conversions on date and numeric values. For example what happens if the default date format for the database isn't MM/dd/yyyy, your conversion from dateTimePicker1.Value.ToString("MM/dd/yyyy") will fail. The same happens for example if 1,5 is entered in rate.
- you don't examine errors at all, you should have proper try..catch blocks to handle common errors, for example unique constraint violations
- you don't dispose objects. The easiest way is to use using blocks in your code in order to ensure proper disposal of resources

For the problems mentioned above, have a look at Properly executing database operations[^]

Now what comes to the Total field, I wouldn't necessarily use such column at all. If total is calculated based on Qty * Rate, I would create a computed column in the table instead. Currently if either quantity or rate changes you always need to update the total field correspondingly. Also you need to make sure that total cannot be edited; otherwise quantity and rate wouldn't make sense.

Having a computed column solves those problems, you always update the source values and the computation value is always correct based on the values fo mother columns and is read-only. For more information, have a look at Specify Computed Columns in a Table[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900