Laurentiu Nastase
Laurentiu Nastase

Reputation: 13

Have some problems with Update in c# and SQL Server

I started an app using C# with a SQL Server database. The program works fine with Add and Delete but on Update doesn't work. Here is my code if anyone can help me, i can't find what's wrong. The program doesn't give me any errors, it just shows up the Message Box with "Error!"

Here is the update method

    public bool Update(ClientiClass c)
    {
        bool isSuccess = false;
        SqlConnection conn = new SqlConnection(mycon);

        try
        {
            string sql = "UPDATE Clienti SET Nume=@Nume, Tara=@Tara, Judet=@Judet, Telefon=@Telefon, Mail=@Mail, CNP=@CNP  WHERE CodClient=@CodClient";

            SqlCommand cmd = new SqlCommand(sql, conn);
            cmd.Parameters.AddWithValue("@Nume", c.NumePrenume);
            cmd.Parameters.AddWithValue("@Tara", c.Tara);
            cmd.Parameters.AddWithValue("@Judet", c.Judet);
            cmd.Parameters.AddWithValue("@Telefon", c.Telefon);
            cmd.Parameters.AddWithValue("@Mail", c.EMail);
            cmd.Parameters.AddWithValue("@CNP", c.CNP);

            conn.Open();
            int rows = cmd.ExecuteNonQuery();

            if(rows > 0)
            {
                isSuccess = true;
            }
            else
            {
                isSuccess = false;
            }
        }
        catch (Exception ex)
        {
        }
        finally
        {
            conn.Close();
        }

        return isSuccess;
    }

And here is the code for the button

private void button2_Click(object sender, EventArgs e)
{
        c.CodClient = int.Parse(textBox1.Text);
        c.NumePrenume = textBox2.Text;
        c.Tara = comboBoxTara.Text;
        c.Judet = comboBoxJudet.Text;
        c.Telefon = textBox9.Text;
        c.CNP = textBox3.Text;
        c.EMail = textBox10.Text;

        bool success = c.Update(c);

        if(success == true)
        {
            MessageBox.Show("Success!");
            DataTable dt = c.Select();
            dataGridView1.DataSource = dt;
            Clear();
        }
        else
        {
            MessageBox.Show("Error!");
        }
    }

    private void dataGridView1_RowHeaderMouseClick(object sender, DataGridViewCellMouseEventArgs e)
    {
        int rowIndex = e.RowIndex;
        textBox1.Text = dataGridView1.Rows[rowIndex].Cells[0].Value.ToString();
        textBox2.Text = dataGridView1.Rows[rowIndex].Cells[1].Value.ToString();
        textBox9.Text = dataGridView1.Rows[rowIndex].Cells[4].Value.ToString();
        textBox3.Text = dataGridView1.Rows[rowIndex].Cells[6].Value.ToString();
        textBox10.Text = dataGridView1.Rows[rowIndex].Cells[5].Value.ToString();
        comboBoxTara.Text = dataGridView1.Rows[rowIndex].Cells[2].Value.ToString();
        comboBoxJudet.Text = dataGridView1.Rows[rowIndex].Cells[3].Value.ToString();
    }

Upvotes: 0

Views: 46

Answers (1)

Isma
Isma

Reputation: 15200

You didn't set the CodClient parameter, try the following:

public bool Update(ClientiClass c)
{
    bool isSuccess = false;
    SqlConnection conn = new SqlConnection(mycon);
    try
    {
        string sql = "UPDATE Clienti SET Nume=@Nume, Tara=@Tara, Judet=@Judet, Telefon=@Telefon, Mail=@Mail, CNP=@CNP  WHERE CodClient=@CodClient";
        SqlCommand cmd = new SqlCommand(sql, conn);
        cmd.Parameters.AddWithValue("@Nume", c.NumePrenume);
        cmd.Parameters.AddWithValue("@Tara", c.Tara);
        cmd.Parameters.AddWithValue("@Judet", c.Judet);
        cmd.Parameters.AddWithValue("@Telefon", c.Telefon);
        cmd.Parameters.AddWithValue("@Mail", c.EMail);
        cmd.Parameters.AddWithValue("@CNP", c.CNP);
        cmd.Parameters.AddWithValue("@CodClient", c.CodClient);

        conn.Open();
        int rows = cmd.ExecuteNonQuery();
        if(rows>0)
        {
            isSuccess = true;

        }
        else
        {
            isSuccess = false;
        }
    }
    catch (Exception ex)
    {

    }
    finally
    {
        conn.Close();
    }
    return isSuccess;
}

Upvotes: 2

Related Questions