Click here to Skip to main content
15,884,739 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
i have two form, main form and form update. form update contain is some textboxes and button, and main form containt datagridview from database.. i want to update the database using form update. i tried this code, but it can't be update..

private void button1_Click_1(object sender, EventArgs e)
        {
            try
            {
                Form1 form1 = (Form1)this.Owner;
                MySqlConnection conn = new MySqlConnection("server=localhost;User Id=root;database=sma9");
                dt = new DataTable();
                MySqlDataAdapter adp = new MySqlDataAdapter("UPDATE tbukux SET judulbuku="+txtjudul.Text+",namakategori=" + txtkategori.Text + ",pengarang=" + txtpengarang.Text + ",penerbit" + txtpenerbit.Text + ",tahunterbit=" + txttahun.Text + ",stokbuku=" + txtstok.Text + ",tglpenerimaan=" + tglter.Text + ") WHERE kodebuku=" + txtkode.Text + "", conn);
                adp.Fill(dt);
                MessageBox.Show("Data Berhasil Disimpan");
                DataTable dt1 = new DataTable();
                MySqlDataAdapter adp1 = new MySqlDataAdapter("select * from tbukux", conn);
                adp1.Fill(dt1);
                form1.gridbuku.DataSource = dt1;
            }

            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }             
        }



MySql.Data.mysqlclient.mysqlexception (0x800040005) ": you have en error in your sql syntax blah blah blah


please help me
Posted
Comments
Sandeep Mewara 23-May-12 13:42pm    
You have to update the question with your full error. Basically, what was this 'blah blah blah..."?
Ed Nutting 23-May-12 13:58pm    
What does it say about a coder, I wonder, when they manage to discard the most useful bit of the error message? :P
Sandeep Mewara 23-May-12 14:23pm    
+5! :)

I know how much I controlled myself on not commenting about the coder here when posting comment of 'blah blah blah'.... :)

Ed Nutting 23-May-12 14:30pm    
Thank you and yes that was about the most restrained I could get :)

from your code above I think you should remove ) before WHERE in the UPDATE command text. I hope this will help.
 
Share this answer
 
You seem to have an extra ) and you miss equal sign after penerbit

And another observation: Don't concatenate values directly to your SQL statement since this leaves you open to SQL injections, data type conversion problems and so on. Instead use MySqlParameter[^].

So your statement should look something like:
C#
MySqlDataAdapter adp = new MySqlDataAdapter(@"
UPDATE tbukux 
SET judulbuku     = @judulbuku,
    namakategori  = @namakategori,
    pengarang     = @pengarang,
    penerbit      = @penerbit,
    tahunterbit   = @tahunterbit,
    stokbuku      = @stokbuku,
    tglpenerimaan = @tglpenerimaan
WHERE kodebuku = @kodebuku", conn);
...
 
Share this answer
 
Comments
Ed Nutting 23-May-12 14:31pm    
Good answer and (again) very good point about the direct concatenation, my 5+. Though the OP is probably using ASP.Net so is protected from SQL injection fairly well - the auto-validation of data is very good already :)

Ed
Wendelius 23-May-12 14:37pm    
Thanks :)
Maciej Los 23-May-12 14:36pm    
Good work, my 5!
Wendelius 23-May-12 14:37pm    
Thanks losmac :)
thanks to Mika Wendelius, its really working... :)
 
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