PDA

View Full Version : سوال: مشکل در کوئری update در datagridview



پیام حیاتی
پنج شنبه 14 اسفند 1393, 15:56 عصر
سلام
این کدهای من هست جهت ویرایش اطلاعات (با دابل کلیک بر روی سلول نام در datagrid نام رو تغییر میدم) :


SqlConnection con = new SqlConnection("Data Source=.; Initial Catalog=Project; Integrated Security=TRUE ");


for (int i = 0; i < dataGridView3.RowCount; i++)
{
con.Open();
SqlCommand sc = new SqlCommand();
sc.Connection = con;
sc.CommandText = "update Personels set p_name='" + dataGridView3.Rows[i].Cells[1] + "' where p_id=" + dataGridView3.Rows[i].Cells[0];
sc.ExecuteNonQuery();


}


con.Close();


فرم :
129136
با تغییر نام و کلیک بر روی دکمه update این خطا رو میگیره :
An unhandled exception of type 'System.Data.SqlClient.SqlException' occurred in System.Data.dll


Additional information: Incorrect syntax near '{'.
129137

Padrone
پنج شنبه 14 اسفند 1393, 16:04 عصر
value یادت رفته

dataGridView3.Rows[i].Cells[1].Value

پیام حیاتی
پنج شنبه 14 اسفند 1393, 16:11 عصر
تغییر دادم به این شکل :

SqlConnection con = new SqlConnection("Data Source=.; Initial Catalog=Project; Integrated Security=TRUE ");


for (int i = 0; i < dataGridView3.RowCount; i++)
{
con.Open();
SqlCommand sc = new SqlCommand();
sc.Connection = con;
sc.CommandText = "update Personels set p_name='" + dataGridView3.Rows[i].Cells[1].Value + "' where p_id=" + dataGridView3.Rows[i].Cells[0].Value;
sc.ExecuteNonQuery();
con.Close();


}

خطای جدید :
129138

Mofid.m
پنج شنبه 14 اسفند 1393, 16:29 عصر
دستور Sql رو به این شکل تغییر بده


sc.CommandText = "update Personels set p_name='" + dataGridView3.Rows[i].Cells[1].Value + "' where p_id='" + dataGridView3.Rows[i].Cells[0].Value+"'";

پیام حیاتی
پنج شنبه 14 اسفند 1393, 16:56 عصر
به این شکل تغغیر دادم درست شد :

SqlConnection con = new SqlConnection("Data Source=.; Initial Catalog=Project; Integrated Security=TRUE ");


for (int i = 0; i < dataGridView3.RowCount-1; i++)
{
con.Open();
SqlCommand sc = new SqlCommand();
sc.Connection = con;
sc.CommandText = "update Personels set p_name='" + dataGridView3.Rows[i].Cells[1].Value + "' , p_family='" + dataGridView3.Rows[i].Cells[2].Value + "' where p_id=" + dataGridView3.Rows[i].Cells[0].Value;
sc.ExecuteNonQuery();
con.Close();


}
ممنون عزیزان.
-1 در حلقه رکورد خالی رو در نظر نمیگیره ، اگر دقت کنید 3 ردیف وجود داره.

Padrone
پنج شنبه 14 اسفند 1393, 17:48 عصر
ردیف اخرت خالی هستش واسه همینه اررور میده
خاصیت AllowUserToAddRow رو false کن