Click here to Skip to main content
15,887,083 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
C#
private void btnUpdate_Click(object sender, EventArgs e)
        {
            try
            {
                if (txtempid.Text == "")
                {
                    MessageBox.Show("Enter Employee Id To Update");
                }
                else
                {
                    SqlCommand cmdupdate = new SqlCommand("Update EmployeeDetails SET EmpName='" + txtEmpName.Text + "',EmpDesgn='" + txtEmpDegn.Text + "' ,EmpSalary='" + txtSalary.Text + "'  where EmpId=" + txtempid.Text + "", con);
                    con.Open();
                    cmdupdate.CommandType = CommandType.Text;
                    cmdupdate.ExecuteNonQuery();
                    MessageBox.Show("Data  Updated");
                }



            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            finally
            {
                if (con.State == ConnectionState.Open)
                {
                    con.Close();
                }
            }
        }


What I have tried:

this error happen ony when im update my data but inserting,delation and others no error
Posted
Updated 21-Oct-19 5:06am
v2
Comments
jimmson 21-Oct-19 8:22am    
There's no desc keyword in the code.
F-ES Sitecore 21-Oct-19 8:26am    
Maybe the employee name is John O'Desc :)
jimmson 21-Oct-19 8:32am    
:) this gave me good laugh, thanks. You're right though.

Please, please read the following article:

Understanding SQL Injection and Creating SQL Injection Proof ASP.NET Applications[^]

I've corrected your code. Not 100% the best way but at least you won't get sql injection issues.

C#
private void btnUpdate_Click(object sender, EventArgs e) {
            try {
                if (txtempid.Text == "") {
                    MessageBox.Show("Enter Employee Id To Update");
                }
                else {

                    var sql = "Update EmployeeDetails SET " +
                              "EmpName=@EmpName," +
                              "EmpDesgn=@EmpDesgn ," +
                              "EmpSalary=@EmpSalary  " +
                              "where EmpId=@EmpId";

                    using (SqlCommand cmdupdate = new SqlCommand(sql, con)) {
                        cmdupdate.Parameters.AddWithValue("@EmpName", txtEmpName.Text);
                        cmdupdate.Parameters.AddWithValue("@EmpDesgn", txtEmpDegn.Text);
                        cmdupdate.Parameters.AddWithValue("@EmpSalary", txtSalary.Text);
                        cmdupdate.Parameters.AddWithValue("@EmpId", txtempid.Text);
                        cmdupdate.CommandType = CommandType.Text;

                        con.Open();
                        cmdupdate.ExecuteNonQuery();
                        MessageBox.Show("Data  Updated");
                    }
                }
            }
            catch (Exception ex) {
                MessageBox.Show(ex.Message);
            }
            finally {
                if (con.State == ConnectionState.Open) {
                    con.Close();
                }
            }
        }
 
Share this answer
 
C#
SqlCommand cmdupdate = new SqlCommand("Update EmployeeDetails SET EmpName='" + txtEmpName.Text + "',EmpDesgn='" + txtEmpDegn.Text + "' ,EmpSalary='" + txtSalary.Text + "'  where EmpId=" + txtempid.Text + "", con);

Not a solution to your question, but another problem you have.
Never build an SQL query by concatenating strings. Sooner or later, you will do it with user inputs, and this opens door to a vulnerability named "SQL injection", it is dangerous for your database and error prone.
A single quote in a name and your program crash. If a user input a name like "Brian O'Conner" can crash your app, it is an SQL injection vulnerability, and the crash is the least of the problems, a malicious user input and it is promoted to SQL commands with all credentials.
SQL injection - Wikipedia[^]
SQL Injection[^]
SQL Injection Attacks by Example[^]
PHP: SQL Injection - Manual[^]
SQL Injection Prevention Cheat Sheet - OWASP[^]
How can I explain SQL injection without technical jargon? - Information Security Stack Exchange[^]
 
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