2015-03-03 82 views
0
  • 我有4列一個DataGridView:UserID, FirstName, LastName, EmailUPDATE語句改變錯誤記錄

  • 當我雙擊某一行,另一種形式與4個文本框裏面那些行的值,每個文本框有彈起單元格值

  • 當我單擊提交按鈕時,該行將更新爲文本框的值。

  • 我與....where UserID = @UserID一起工作。但是,如果列單元格 我正在更改IS UserId,錯誤的行或無行將被修改。

問題

我如何可以編輯任何列值的行,它改變了右排沒有增量列?

代碼

public partial class EditUser : Form 
{ 
    public DataGridViewRow dgvRow; 
    public EditUser() 
    { 
     InitializeComponent(); 
    } 

    private void EditUser_Load(object sender, EventArgs e) 
    { 
     lbl5.Text = dgvRow.Cells[0].Value.ToString(); 
     txtUserID.Text = dgvRow.Cells[0].Value.ToString(); 
     txtFName.Text = dgvRow.Cells[1].Value.ToString(); 
     txtLName.Text = dgvRow.Cells[2].Value.ToString(); 
     txtEmail.Text = dgvRow.Cells[3].Value.ToString(); 
     label1.Visible = false; 
     txtUserID.Visible = false; 
    } 

    private void linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e) 
    { 
     this.Visible = false; 
    } 

    private void button1_Click(object sender, EventArgs e) 
    { 
     SqlConnection sc = new SqlConnection(); 
     SqlCommand com = new SqlCommand(); 
     sc.ConnectionString = (""); 
     sc.Open(); 
     com.Connection = sc; 
     com.CommandText = ("update JoshUserTable SET UserID = @UserID, [email protected],[email protected],[email protected] where UserID = @UserID"); 
     com.Parameters.AddWithValue("@UserID", txtUserID.Text); 
     com.Parameters.AddWithValue("@FirstName", txtFName.Text); 
     com.Parameters.AddWithValue("@LastName", txtLName.Text); 
     com.Parameters.AddWithValue("@Email", txtEmail.Text); 
     com.ExecuteNonQuery(); 
     sc.Close(); 
    } 

    private void linkLabel2_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e) 
    { 
     label1.Visible = true; 
     txtUserID.Visible = true; 
     SqlConnection sc = new SqlConnection(); 
     SqlCommand con = new SqlCommand(); 
     sc.ConnectionString = (""); 
     sc.Open(); 
     con.Connection = sc; 
     con.CommandText = ("update JoshUserTable SET UserID = @UserID, [email protected],[email protected],[email protected] where FirstName = @FirstName"); 
     con.Parameters.AddWithValue("@UserID", txtUserID.Text); 
     con.Parameters.AddWithValue("@FirstName", txtFName.Text); 
     con.Parameters.AddWithValue("@LastName", txtLName.Text); 
     con.Parameters.AddWithValue("@Email", txtEmail.Text); 
     con.ExecuteNonQuery(); 
     sc.Close(); 
    } 
} 

回答

0

我認爲你是在一個更大的問題,在所有運行。如果某個記錄被另一個用戶同時更改,該怎麼辦?您將運行併發問題。這就是爲什麼自動生成的CommandBuilder代碼總是檢查舊值。

對於您當前的代碼,我添加了對原始用戶ID的檢查。

private void button1_Click(object sender, EventArgs e) 
{ 
    SqlConnection sc = new SqlConnection(); 
    SqlCommand com = new SqlCommand(); 
    sc.ConnectionString = ("My Connection String"); 
    sc.Open(); 
    com.Connection = sc; 

    /// check for original value in whereClause 
    com.CommandText = ("update JoshUserTable SET UserID = @NEW_UserID, 
         [email protected],[email protected],[email protected] 
         where UserID = @ORIGINAL_UserID"); 


    com.Parameters.AddWithValue("@NEW_UserID", txtUserID.Text); 
    com.Parameters.AddWithValue("@ORIGINAL_UserID", INSERT_ORIGINAL_USERID); 


    com.Parameters.AddWithValue("@FirstName", txtFName.Text); 
    com.Parameters.AddWithValue("@LastName", txtLName.Text); 
    com.Parameters.AddWithValue("@Email", txtEmail.Text); 
    com.ExecuteNonQuery(); 
    sc.Close(); 
} 

併發問題仍然未解決撐....

+0

感謝您的輸入,我去一個稍微不同的路線。隨着userIDtext框,我做了一個顯示用戶ID的標籤。當editform彈出時,UserIDtextbox是不可見的。在UserID標籤旁邊,我有一個鏈接標籤「更改」,如果點擊該標籤,則會顯示UserID文本框,以更改名稱。如果鏈接未被選中,則無意更改名稱。在我的linkcheck事件中,我更改了where子句以匹配firstname和lastname,因爲即使用戶名或電子郵件可能,他們的名字也不會改變。然而,排並沒有改變......見上面 – 2015-03-03 15:07:35

+0

哇,我花了一秒才弄清楚什麼是沒有意義的。我正在讓我的linklabel做比它應有的更多的方式。 – 2015-03-03 16:19:33

+0

確定了你 - 無論如何,我真的認爲你應該**檢查原始值並設置(a)新值(s)**。當調用'executeNonQuery()'時,你將得到更新記錄的數量。如果這個記錄*不相等1 *出錯了! – 2015-03-03 19:44:39