Click here to Skip to main content
15,896,207 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)
        {
            
            SqlConnection ce = new SqlConnection(@"Data Source=(LocalDB)\v11.0;AttachDbFilename=c:\users\egzon\documents\visual studio 2013\Projects\Arka2\Arka2\DataArka.mdf;Integrated Security=True");
           
            try
            {
                if (txtID.Text != "" & txtPershkrimi.Text != "" & txtNjesia.Text != "" & txtSasia.Text != "" && txtCmimi.Text != "" && listBoxID.SelectedIndex != -1)
                {

                    ce.Open();
                    exeSql.CommandText = "UPDATE Produktet SET ID=" + txtID.Text + " Pershkrimi= '" + txtPershkrimi.Text + "'Njesia='" + txtNjesia.Text + "'Sasia=" + txtSasia.Text + "Cmimi=" + txtCmimi.Text + "";
                    exeSql.ExecuteNonQuery();
                    loadlist();
                    MessageBox.Show("Rreshti u Editua!!", "Mesazhi", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    statusLBL.Text = "konektimi pati sukses";
                    ce.Close();
                    txtID.Text = "";
                    txtPershkrimi.Text = "";
                    txtNjesia.Text = "";
                    txtSasia.Text = "";
                    txtCmimi.Text = "";
                    grdProduktet.Update();
                    grdProduktet.Refresh();
                    
                    
                   
                }
                try
                {
                    statusLBL.Text = "editimi pati sukses!!";
                }
                catch (Exception)
                {

                    statusLBL.Text = "editimi deshtoi!!";
                    return;
                }
            }
            catch (Exception)
            {
                statusLBL.Text = "konektimi deshtoi";
                return;
            }
            finally
            {
                ce.Close();
            }

           
        }
Posted
Updated 15-Jan-15 6:09am
v2

Firstly you should be using parameterised queries - I'll leave you to look that up.

Secondly, you must separate the fields your are using in your sql statement with commas - as your query currently stands it should be
UPDATE Produktet SET ID=" + txtID.Text + ", Pershkrimi= '" + txtPershkrimi.Text + "',Njesia='" + txtNjesia.Text + ",'Sasia=" + txtSasia.Text + ",Cmimi=" + txtCmimi.Text + "";


Edit - here's a link to learn about parameterized queries http://www.dotnetperls.com/sqlparameter[^]
 
Share this answer
 
v2
Commas, my friend. You need commas:
SQL
UPDATE <table_name> SET <field>=<new value>,<field>=<new value>,<field... WHERE ...


But please, don't do it like that! Do not 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.
 
Share this answer
 
v2
You are missing commas in your query.
E.g. exeSql.CommandText = "UPDATE Produktet SET ID=" + txtID.Text + ", Pershkrimi= '" + txtPershkrimi.Text + "', Njesia='" + txtNjesia.Text + "', Sasia=" + txtSasia.Text + "Cmimi=" +
 
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