Это мой код для кнопки обновления, которая должна обновлять базу данных на основе значений ввода текстовых полей. Я сделал текстовые поля заполняемыми, когда пользователь выбирает строку на datagridview. Я хочу, чтобы пользователь просто щелкните по строке, затем измените текст текстовых полей, затем нажмите «Обновить». Но я не могу заставить это работать. Все комментарии полезны.Обновление базы данных из нескольких текстовых полей
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 ([email protected],[email protected],[email protected],[email protected],[email protected]) WHERE [email protected]";
string query1 = "UPDATE loginusers(username,upassword) SET ([email protected],[email protected]) WHERE [email protected]";
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);
}
}
, потому что вы должны выполнять запросы один за другим –
я использую аналогичный код для кнопки добавления с INSERT вместо UPDATE в запросах, и он отлично работает. –
, и я думаю, что при вставке есть только один запрос? Я прав? –