Click here to Skip to main content
15,891,136 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
C#
private void label6_Click(object sender, EventArgs e)
        {
            try
            {

                DialogResult dr = MessageBox.Show("Are you sure you want to update?", "Details Updation", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
                if (dr == DialogResult.No)
                {
                    return;
                }
                else
                {


                    int number = int.Parse(comboBox1.Text);
                    OleDbConnection oleDbConnection = new OleDbConnection(@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=D:\Projects\Active Institute User Details\active.mdb;Persist Security Info=False");
                    OleDbCommand update = new OleDbCommand("UPDATE user_details SET first_name,last_name,course_name,DOJ,father_name,reg_fees='" + textBox1.Text + "','" + textBox4.Text + "','" + textBox8.Text + "','" + textBox7.Text + "','" + textBox2.Text + "','" + textBox3.Text + "' where code = " + number + "", oleDbConnection);
                   
                    oleDbConnection.Open();
                    update.ExecuteNonQuery();
                    oleDbConnection.Close();

                    MessageBox.Show("Updated the student Successfully", "Updation", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
            }
            catch (SystemException se)
            {
                MessageBox.Show(se.Message);
            }
            finally
            {
                
            }
Posted
Updated 17-Oct-14 3:24am
v2
Comments
Member 11160992 17-Oct-14 8:12am    
Please assist me.
NaibedyaKar 17-Oct-14 8:30am    
Your update query is wrong.
King Fisher 17-Oct-14 9:28am    
my be your number Pass null values .check out .

As solution 1 mentioned, the syntax is UPDATE table SET field1 = value, field2 = value.

However, you have a much bigger problem, Sql injection. You never want to concatenate a sql string together by taking input from the user.

Instead, create a stored procedure that takes all of those inputs and then add them as parameters in C#.
 
Share this answer
 
v2
Comments
BillWoodruff 17-Oct-14 9:06am    
+5
UPDATE <yourTable>

SET <yourCol1> = <newValue1>,
        <yourCol2> = <newValue2

Where etc....;



Cheers
 
Share this answer
 
The update script that you are generating here is wrong. It should be like below line
SQL
OleDbCommand update = new OleDbCommand("UPDATE user_details SET first_name = '" + textBox1.Text + "',last_name'" + textBox4.Text + "',course_name = '" +
    textBox8.Text + "', DOJ = '" + textBox7.Text + "',father_name = '" + textBox2.Text + "',reg_fees='"+ textBox3.Text + "' where code = " + number + "", oleDbConnection);
 
Share this answer
 
Comments
ZurdoDev 17-Oct-14 9:29am    
This is very bad advice. Although it does fix the issue with syntax is leaves the user wide open for sql injection.
CHill60 17-Oct-14 10:10am    
I agree with RyanDev. Do not do it this way
NaibedyaKar 17-Oct-14 10:23am    
Well, it is just to help him knowing how to fix the issue.
Member 11160992 18-Oct-14 1:43am    
Thanks NaibedyaKar (Mindfire) for this quick solution. @Ryan Since i am not aware of sql injection, i am Ok with this code until i get any problem. I tried this code and it worked well. Thanks again my friends.
NaibedyaKar 18-Oct-14 2:07am    
I am happy that I helped you.. But Ryan advice is also correct. Why don't you try to use parameterized queries. Something like mentioned here. Else we can kill your DB server easily :)

http://stackoverflow.com/questions/17509169/parameterized-queries-vs-sql-injection
Specify the update statement like field_name = value. For ex: your code should be like this

'UPDATE user_details SET first_name=' + textBox1.Text + "',last_name='" + textBox4.Text + "'...." etc
 
Share this answer
 
Comments
ZurdoDev 17-Oct-14 9:29am    
This is very bad advice. Although it does fix the issue with syntax is leaves the user wide open for sql injection.

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