2014-01-22 33 views
0

这是我的代码:问题与Windows更新功能形成

private void button3_Click(object sender, EventArgs e) 
    { 
     int i; 
     i = dataGridView1.SelectedCells[0].RowIndex; 
     textBox1.Text = dataGridView1.Rows[i].Cells[0].Value.ToString(); 
     textBox2.Text = dataGridView1.Rows[i].Cells[1].Value.ToString(); 
     textBox3.Text = dataGridView1.Rows[i].Cells[2].Value.ToString(); 
    } 

    private void button2_Click(object sender, EventArgs e) 
    { 
     SqlConnection con = new SqlConnection(@"Data Source=(LocalDB)\v11.0;AttachDbFilename=C:\Users\Dido\documents\visual studio 2012\Projects\CourseProjectCars\CourseProjectCars\DataCars.mdf;Integrated Security=True;Connect Timeout=30"); 
     con.Open(); 
     SqlCommand cmd = new SqlCommand("Update SuperCars set [email protected],[email protected],[email protected] Where([email protected])", con); 
     cmd.Parameters.AddWithValue("@Car", textBox1.Text); 
     cmd.Parameters.AddWithValue("@mph", textBox2.Text); 
     cmd.Parameters.AddWithValue("@price", textBox3.Text); 
     cmd.ExecuteNonQuery(); 
     MessageBox.Show("updated......"); 
     con.Close(); 
     Bind(); 
     Clear(); 
    } 
} 

当我点击BUTTON3中日DataGridView1我选择的行中的信息被插入文本框,这样我就可以改变,以将Button4更新它们。我的问题是,当我尝试更新textBox1中的某些东西时,更新不适用于textBox1,也不适用于textBox2和3.但是,当我更新textBox 2或3中的信息时,它可以正常工作。

+0

只是为了澄清..如果你只是试图改变'textbox2'和'textbox3'中的'Text'它可以正常工作,但是当你尝试'textbox1'以及'Text'没有更新?我会建议在'textBox1'行设置一个'breakpoint',看看发生了什么。 – KSdev

+0

我认为错误在这一行,但我不知道如何修复它。 SqlCommand cmd = new SqlCommand(“Update SuperCars set Car = @ Car,mph = @ mph,price = @ price Where (Car = @ Car)“,con); – Cathy

+0

在那里设置“断点”,看看它的值是多少。否则,您可以使用'Console.WriteLine'并查看其中的值。 – KSdev

回答

0

问题是,'汽车'列是您的钥匙,但也是可编辑的。所以,当你改变Textbox1中的文本时,你也改变你的密钥。

因此,当您将TextBox1中的文本从“Car1”更改为“Car2”时,SQL命令将查找要更新的行“Car2”。如果此行不存在,update命令将不执行任何操作。如果该行已经存在,则更新错误的行。

你必须存储在一个成员变量选择的密钥,并用它在你的更新命令:

private string carKey; // member variable to store the key 

private void button3_Click(object sender, EventArgs e) 
{ 
    int i; 
    i = dataGridView1.SelectedCells[0].RowIndex; 
    textBox1.Text = dataGridView1.Rows[i].Cells[0].Value.ToString(); 
    textBox2.Text = dataGridView1.Rows[i].Cells[1].Value.ToString(); 
    textBox3.Text = dataGridView1.Rows[i].Cells[2].Value.ToString(); 

    carKey = textBox1.Text; // store the key of the selected car 
} 

private void button2_Click(object sender, EventArgs e) 
{ 
    SqlConnection con = new SqlConnection(@"Data Source=(LocalDB)\v11.0;AttachDbFilename=C:\Users\Dido\documents\visual studio 2012\Projects\CourseProjectCars\CourseProjectCars\DataCars.mdf;Integrated Security=True;Connect Timeout=30"); 
    con.Open(); 
    SqlCommand cmd = new SqlCommand("Update SuperCars set [email protected],[email protected],[email protected] Where([email protected])", con); 
    cmd.Parameters.AddWithValue("@Car", textBox1.Text); 
    cmd.Parameters.AddWithValue("@mph", textBox2.Text); 
    cmd.Parameters.AddWithValue("@price", textBox3.Text); 
    cmd.Parameters.AddWithValue("@CarKey", carKey); // use the stored car key 
    cmd.ExecuteNonQuery(); 
    MessageBox.Show("updated......"); 
    con.Close(); 
    Bind(); 
    Clear(); 
} 

如果您已经为您的汽车进入一个独特的密钥可以存储并使用,而不是汽车名称以避免问题。

+0

谢谢!关键是问题! – Cathy