0

this is my code for an update button which is supposed to update the database based on entry values of textboxes.I made the textboxes to be filled when user selects a row on datagridview.I want the user to simply click the row then change the textbox texts then click update.But i can't get this to work.All comments are helpful.Shoot.

private void btnUpdate_Click(object sender, EventArgs e)
    {
        try
        {
            if (txtOgrenciNo.Text.Length != 0 && txtAd.Text.Length != 0 && txtSoyad.Text.Length != 0 && txtEmail.Text.Length != 0 && txtTelefon.Text.Length != 0)
            {
                string query ="UPDATE ogrenci(studentId,name,lname,email,phone) SET (studentId=@studentId,name@name,lname=@lname,email=@email,phone=@phone) WHERE studentId=@studentId";
                string query1 = "UPDATE loginusers(username,upassword) SET (username=@email,upassword=@phone) WHERE username=@email";
                using (connection = new SqlConnection(connectionString))
                using (SqlCommand command = new SqlCommand(query, connection))
                using (SqlCommand cmd = new SqlCommand(query1, connection))
                {
                    connection.Open();
                    cmd.Parameters.AddWithValue("@emailVal", txtEmail.Text);
                    cmd.Parameters.AddWithValue("@phone", txtPhone.Text);
                    command.Parameters.AddWithValue("@studentId", txtStudentId.Text);
                    command.Parameters.AddWithValue("@name", txtName.Text);
                    command.Parameters.AddWithValue("@lname", txtLname.Text);
                    command.Parameters.AddWithValue("@email", txtEmail.Text);
                    command.Parameters.AddWithValue("@phone", txtPhone.Text);
                    cmd.ExecuteNonQuery();
                    command.ExecuteNonQuery();
                    populateGrid();
                }
            }
            else
            {
                MessageBox.Show("Student Information can not be blank","Alert",MessageBoxButtons.OK,MessageBoxIcon.Information);
            }
        }
        catch (Exception)
        {
            MessageBox.Show("Please enter different student info", "Alert", MessageBoxButtons.OK, MessageBoxIcon.Information);
        }
    }
5
  • because you must execute queries one by one Commented Feb 3, 2017 at 8:03
  • i use a similar code for add button which has INSERT instead of UPDATE in queries and it works perfectly. Commented Feb 3, 2017 at 8:04
  • and I think when you insert there is only one query? Am I right? Commented Feb 3, 2017 at 8:05
  • Seems that you don't provide parameters under this block: using (SqlCommand command = new SqlCommand(query, connection)). SQL queries requires parameter for each execution even the parameters are same, and should be evaluated one-by-one. Commented Feb 3, 2017 at 8:05
  • @IkramTurgunbaev there ıs two queries one for students and one for loginusers. Commented Feb 3, 2017 at 8:14

1 Answer 1

2

Change your update queries, remove (parameters) after table name

Change this lines

string query ="UPDATE ogrenci(studentId,name,lname,email,phone) SET (studentId=@studentId,name@name,lname=@lname,email=@email,phone=@phone) WHERE studentId=@studentId";
string query1 = "UPDATE loginusers(username,upassword) SET (username=@email,upassword=@phone) WHERE username=@email";

To this

string query ="UPDATE ogrenci SET  studentId=@studentId,name@name,lname=@lname,email=@email,phone=@phone WHERE studentId=@studentId";
string query1 = "UPDATE loginusers SET username=@email,upassword=@phone WHERE username=@email";
Sign up to request clarification or add additional context in comments.

2 Comments

one more question i can update name lname email but when i try to update phone number it conflicts with the foreign key it says do you know a way around this ?
Your phone column references primary key column on another table, so your new updated value should exist on another table, if not it raises error you show. For more info look this - Foreign Keys Oluşturma

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.