Click here to Skip to main content
15,902,492 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
using (SqlConnection connect = new SqlConnection(db.Connectionstring()))
           {
               using (SqlCommand command = new SqlCommand())
               {
                   string pro = "";
                   int qty;
                   int totqty;

                   int i=0;

                        foreach (DataGridViewRow rows in dataGridView1.Rows)
                        {

                            SqlConnection con1 = new SqlConnection(db.Connectionstring());
                            con1.Open();
                            SqlCommand cmd1 = new SqlCommand("select * from sales where empnames='" + comboBox1.Text + "' ", con1);
                            SqlDataReader dr = cmd1.ExecuteReader();

                            if (dr.HasRows)
                            {
                                //while (dr.Read())
                                //{
                                    SqlConnection con2 = new SqlConnection(db.Connectionstring());
                                    con2.Open();

                                    SqlCommand cmd2 = new SqlCommand("update sales set empnames=@empnames+'" + comboBox1.Text + "',  categories=@categories, weight=@weight,per=@per,wastage=@wastage,customer=@customer,party=@party  where date=@date ", con2);

                                   cmd2.Parameters.AddWithValue("@empnames", Convert.ToString((comboBox1.Text)));
                                    cmd2.Parameters.AddWithValue("@date", Convert.ToDouble(rows.Cells[0].Value));
                                    cmd2.Parameters.AddWithValue("@categories", Convert.ToString(rows.Cells[1].Value));
                                    cmd2.Parameters.AddWithValue("@weight", Convert.ToString((rows.Cells[2].Value)));
                                    cmd2.Parameters.AddWithValue("@per", Convert.ToDouble(rows.Cells[3].Value));
                                    cmd2.Parameters.AddWithValue("@wastage", Convert.ToString((rows.Cells[4].Value)));
                                    cmd2.Parameters.AddWithValue("@customer", Convert.ToDouble(rows.Cells[5].Value));
                                    cmd2.Parameters.AddWithValue("@party", Convert.ToString((rows.Cells[6].Value)));




                                    cmd2.ExecuteNonQuery();
                                    con2.Close();


                                    SqlConnection con4 = new SqlConnection(db.Connectionstring());
                                    con4.Open();
                                    SqlCommand cmd4 = new SqlCommand("update sales set amtcst='" + txt_netamount.Text + "', amtparty='" + txtparty.Text + "', amtfinal='" + txtfinal.Text + "' where empnames='" + comboBox1.SelectedText + "'", con4);
                                    cmd4.ExecuteNonQuery();
                                    con4.Close();

                          // }
                            }


                            else
                            {
                                SqlConnection con3 = new SqlConnection(db.Connectionstring());
                                con3.Open();
                                SqlCommand cmd3 = new SqlCommand("insert into sales(empnames,date,categories,weight,per,wastage,customer,party,amtcst,amtparty,amtfinal)values('" + comboBox1.Text + "', '" + rows.Cells[0].Value + "','" + rows.Cells[1].Value + "','" + rows.Cells[2].Value + "','" + rows.Cells[3].Value + "','" + rows.Cells[4].Value + "','" + rows.Cells[5].Value + "','" + rows.Cells[6].Value + "','" + txt_netamount.Text + "','" + txtparty.Text + "','" + txtfinal.Text + "')", con3);
                                cmd3.ExecuteNonQuery();
                                con3.Close();


                            }
                        }

                   }
               }




this is my insert and updation script for my c# 2010 win forms, i have problem in empnames insert in two times to data base.

Any one give me ideas

What I have tried:

C#
data base field type stored in two times
Posted
Updated 27-Nov-16 4:13am

>> empnames=@empnames+'" + comboBox1.Text
>> cmd2.Parameters.AddWithValue("@empnames", Convert.ToString((comboBox1.Text)));

You're already passing @empnames as parameter (which is good) so you don't need to re add it with query. As is empname will be saved as: "sometext""sometext"

But from the 1st line think of sql injection queries...as you're passing any text to query instead of just parameter.

Also on another point is that there is too much functionality being done by 1 method.
Opening many connections, can re use the same but clear parameters each time
select
update
update
insert

No validation of data on server side...so some queries may fail ex insert a string instead of int, etc
 
Share this answer
 
Comments
Boopalslm 27-Nov-16 9:24am    
what i am do now, sir please alter my update query and send me sir.
P_Z 27-Nov-16 9:29am    
Did you read/understand the solution? First line just shows you how to do it...see the strikethrough text which means remove that
Boopalslm 27-Nov-16 9:33am    
below line is correct

empnames=@empnames+'", categories=@categories, weight=@weight,per=@per,wastage=@wastage,customer=@customer,party=@party where date=@date ", con2);

bur error is start on categories=@categories, how to solve sir
Boopalslm 27-Nov-16 9:33am    
sir just guide me sir i will spend 4 days sir. but till now i cannot get result.
You're mixing parameters and literals and this is causing confusion for you. Use parameters and only parameters in your statements. Every time you start writing something like ..." + ... into an SQL statement you're most likely on the wrong track.

So the update should look something like
C#
SqlCommand cmd2 = new SqlCommand(
@"update sales 
  set empnames=@empnames,
      categories=@categories,      
      weight=@weight,
      per=@per,
      wastage=@wastage,
      customer=@customer,
      party=@party  
where date=@date ", con2);
cmd2.Parameters.AddWithValue("@empnames", comboBox1.Text);
cmd2.Parameters.AddWithValue("@categories", Convert.ToString(rows.Cells[1].Value));
cmd2.Parameters.AddWithValue("@weight", Convert.ToString((rows.Cells[2].Value)));
cmd2.Parameters.AddWithValue("@per", Convert.ToDouble(rows.Cells[3].Value));
cmd2.Parameters.AddWithValue("@wastage", Convert.ToString((rows.Cells[4].Value)));
cmd2.Parameters.AddWithValue("@customer", Convert.ToDouble(rows.Cells[5].Value));
cmd2.Parameters.AddWithValue("@party", Convert.ToString((rows.Cells[6].Value)));
cmd2.Parameters.AddWithValue("@date", Convert.ToDouble(rows.Cells[0].Value));

cmd2.ExecuteNonQuery();

But it doesn't stop there, you should fix all the statements to use parameters.

Also you should
- Open a connection only once inside a single method. Why repeatedly open it?
- Use using blocks to ensure that objects are disposed correctly.
- Use try..catch blocks to properly handle exceptions.

I suggest reading through Properly executing database operations[^]
 
Share this answer
 
Comments
Boopalslm 27-Nov-16 9:51am    
i am alter my code your procedure but cannot add to data base second row in same empnames, how to solve this error
Wendelius 27-Nov-16 10:58am    
Not quite sure what you mean. If you get error message, please post it or some example data for the problem.
Boopalslm 27-Nov-16 11:02am    
no error, i am bind my previous record, and enter second row but cannot stored second row in data base.
Wendelius 27-Nov-16 11:03am    
What is the code you currently use?
Boopalslm 27-Nov-16 11:05am    
above your code
Never build an SQL query with concatenation like this:
C#
SqlCommand cmd1 = new SqlCommand("select * from sales where empnames='" + comboBox1.Text + "' ", con1);

If user input is exotic, the query will fail, if it is malicious,it opens door to SQL injection.
SQL Injection[^]
SQL injection - Wikipedia[^]
 
Share this answer
 
Comments
Boopalslm 27-Nov-16 10:36am    
so how alter my queries give me some ideas
Patrice T 27-Nov-16 10:46am    
read second link
Boopalslm 27-Nov-16 10:52am    
sir if you don't mind please above alter my command because i was already spend 4 days, till now i cannot get result.

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