如何获取文本框,编辑它们并将它们保存回数据库?

时间:2013-02-06 11:20:10

标签: c# winforms

我在Form1中有一个gridview和Textbox。当Form1加载时,它将从数据库将数据加载到Gridview中。我有一个GridView的selectionChanged事件,从那里数据进入文本框现在我的问题是我想在Textboxes中编辑它们并将它们保存到数据库,但当我点击保存按钮时它在数据库和gridview中创建一个新记录。如何解决这个问题?

以下是我的SaveButton代码:

private void btnSave_Click_1(object sender, EventArgs e)
{
    DataRow dr = dt.NewRow();
    da = new SqlDataAdapter("select * from Measurement", con);
    SqlCommandBuilder cb = new SqlCommandBuilder(da);
    dr["CellNumber"] = txtCellNo.Text.Trim();
    dr["FirstName"] = txtFirstName.Text;
    dr["LastName"] = txtLastName.Text;
    dr["Shirt"] = txtShirt.Text;
    dr["Pant"] = txtPant.Text;
    dr["DueDate"] = txtDueDate.Text;
    dr["Date"] = txtDate.Text;
    if (dr["CellNumber"] == "")
    {
        MessageBox.Show("Please enter Cell Number");
    }
    else if (dr["CellNumber"] != "")
    {
        dr = dt.Select(" ID = " + txtID.Text)[0]; //updated here
    }

    try
    {
        da.Update(ds, "Measurement");
    }
    catch (DBConcurrencyException ex)
    {
        MessageBox.Show(ex.Message);
    }
}

Gridview代码:

private void dgv_SelectionChanged_1(object sender, EventArgs e)
{
    if (dgv.SelectedRows.Count > 0)
    {

        foreach (DataGridViewRow row in dgv.SelectedRows)
        {
            //Send the first cell value into textbox'
            txtLastName.Text = row.Cells["LastName"].Value.ToString();
            txtFirstName.Text = row.Cells["FirstName"].Value.ToString();
            txtCellNo.Text = row.Cells["CellNumber"].Value.ToString();
            txtDate.Text = row.Cells["Date"].Value.ToString();
            txtDueDate.Text = row.Cells["DueDate"].Value.ToString();
            txtPant.Text = row.Cells["Pant"].Value.ToString();
            txtShirt.Text = row.Cells["Shirt"].Value.ToString();
            txtID.Text = row.Cells["ID"].Value.ToString();
        }
    }
}

4 个答案:

答案 0 :(得分:0)

您在查询中遗漏了WHERE条件

da = new SqlDataAdapter("select * from Measurement WHERE ID = " + txtID.Text, con);

答案 1 :(得分:0)

您应该注意的是保存到数据库中的方式。

现在发生的事情是你在数据库中插入一个新的实体/行而不是更新你已加载的实体/行。

您缺少主键字段。身份证。因此,添加一个不存在且需要更新的实体将始终是一个新实体。

答案 2 :(得分:0)

da.Update(ds, "Measurement");正在插入新记录,因为您尝试更新的行的RowState属性设置为Added,因为您要将单元格值分配给新行。您需要更新现有行的值。检查DbDataAdapter.Update Documentation

  

当应用程序调用Update方法时,DbDataAdapter   检查RowState属性,并执行所需的INSERT,   根据每行迭代地迭代或删除语句   DataSet中配置的索引的顺序

试试这个:

private void btnSave_Click_1(object sender, EventArgs e)
{
    da = new SqlDataAdapter();
    da.SelectCommand = new SqlCommand("select * from Measurement where ID = @ID",con);
    da.SelectCommand.Parameters.AddWithValue("@ID",int.Parse(txtID.Text));
    SqlCommandBuilder cb = new SqlCommandBuilder(da);
    da.Fill(ds, "Measurement");        

    if (String.IsNullOrEmpty(txtCellNo.Text.Trim()))
    {
        MessageBox.Show("Please enter Cell Number");
    }
    else
    {
        try
        {
           dr = ds.Tables["Measurement"].Rows[0];

           dr["CellNumber"] = txtCellNo.Text.Trim();
           dr["FirstName"] = txtFirstName.Text;
           dr["LastName"] = txtLastName.Text;
           dr["Shirt"] = txtShirt.Text;
           dr["Pant"] = txtPant.Text;
           dr["DueDate"] = txtDueDate.Text;
           dr["Date"] = txtDate.Text;

           cb.GetUpdateCommand();
           da.Update(ds, "Measurement");
        }
        catch (Exception ex)
        {
           MessageBox.Show(ex.Message);
        }
    }


  }

答案 3 :(得分:0)

da = new SqlDataAdapter("select * from Measurement", con);
SqlCommandBuilder cb = new SqlCommandBuilder(da);
dr = dt.Select(" ID = " + txtID.Text)[0];

if (txtCellNo.Text == "")
{
    MessageBox.Show("Please enter Cell Number");
}
else if (dr["CellNumber"] != "")
{
    dr["CellNumber"] = txtCellNo.Text.Trim();
    dr["FirstName"] = txtFirstName.Text;
    dr["LastName"] = txtLastName.Text;
    dr["Shirt"] = txtShirt.Text;
    dr["Pant"] = txtPant.Text;
    dr["DueDate"] = txtDueDate.Text;
    dr["Date"] = txtDate.Text;
}

try
{
    da.Update(ds, "Measurement");
}
catch (DBConcurrencyException ex)
{
    MessageBox.Show(ex.Message);
}