更新GridView的我有一个GridView“GridViewMcm”与dropdownlists而在接下来的两列第一和第二列和文本框进行编辑。PairId是表的主键。问题而在ASP.NET
以下是对于Onrowupdating事件的代码
SqlConnection mySQLconnection = new SqlConnection(con);
mySQLconnection.Open();
DropDownList drdList1, drdlist2;
drdList1 = (DropDownList)(GridViewMcm.Rows[e.RowIndex].Cells[0].FindControl("ddeditfrom"));
drdlist2 = (DropDownList)(GridViewMcm.Rows[e.RowIndex].Cells[1].FindControl("ddeditto"));
TextBox txtrate, txtdesc;
txtrate = (TextBox)(GridViewMcm.Rows[e.RowIndex].Cells[2].FindControl("txteditrate"));
txtdesc = (TextBox)(GridViewMcm.Rows[e.RowIndex].Cells[3].FindControl("txteditdesc"));
SqlCommand strupdate = new SqlCommand("Update mcmtable set Measure1FromSymbol = @Measure1FromSymbol, Measure2ToSymbol = @Measure2ToSymbol, ConversionRate = @ConversionRate, ConversionDesc = @ConversionDesc Where [email protected]", mySQLconnection);
strupdate.Parameters.Add("@Measure1FromSymbol", SqlDbType.Text).Value = drdList1.SelectedValue;
strupdate.Parameters.Add("@Measure2ToSymbol", SqlDbType.Text).Value = drdlist2.SelectedValue;
strupdate.Parameters.Add("@ConversionRate", SqlDbType.Decimal).Value = decimal.Parse(txtrate.Text);
strupdate.Parameters.Add("@ConversionDesc", SqlDbType.Text).Value = txtdesc.Text;
strupdate.Parameters.Add("@PairId", SqlDbType.Int).Value = Convert.ToInt32(GridViewMcm.DataKeys[e.RowIndex].Values[0].ToString());
strupdate.ExecuteNonQuery();
GridViewMcm.EditIndex = -1;
这里是在执行上面的代码正在错误:
“UNIQUE KEY约束 违反“U_mcmtable”。无法在对象'mcmtable'中插入重复的 密钥。该 语句已终止。”
我发现这个问题是该下拉列表值‘drdlist1.SelectedValue’和‘drdlist2.SelectedValue’无法获取在下拉列表中的SelectedValue,但取一些默认值。由于它们提取的默认值已经作为特定行中的组合存在于表中,所以它会抛出错误。上面的代码部分是否有任何错误?
建议一:“ne1能帮我解决吗?”不是英语。如果你想被认真对待,不要使用那种语言。 – 2011-03-08 01:11:51
好的。感谢您的通知。这类帮助网站的新用户。 – xyzwwq 2011-03-08 06:33:07