【发布时间】:2016-09-27 23:51:14
【问题描述】:
我正在尝试将 dataGridViewAFp1 中 Annual_Fees 列的值添加到文本框 txtp1AF.Text 中的新值 AF2 中,该值已转换为整数。然后将添加 ResultAF 的结果转换回字符串 updatedAF,这就是要在数据库中更新的值。我确实将变量 AF11 初始化为 0。查询确实更新了选定的列。我们的想法是获取 AF2 中的任何值,并将其添加到我在 数据库 中的 AF11 中特别输入的列和行中的任何值strong> 因此更新值 updatedAF。这是我到目前为止所做的,但似乎没有奏效。这些值没有加起来。
private void btnEnterAFp1_Click(object sender, EventArgs e)
{
if (string.IsNullOrWhiteSpace(txtp1AF.Text))
{
MessageBox.Show("Please Enter fee","Oops",MessageBoxButtons.OK,MessageBoxIcon.Warning);
}
else
{
if (dataGridViewAFp1.Rows[0].Cells["Annual_Fees"].Value != null)
{
conn.Open();
SqlCommand cmd = new SqlCommand();
cmd.Connection = conn;
int.TryParse(dataGridViewAFp1.Rows[0].Cells["Annual_Fees"].Value.ToString(), out AF11);
AF2 = Convert.ToInt32(txtp1AF.Text);
ResultAF = AF11 + AF2;
String updatedAF = Convert.ToString(ResultAF);
cmd.CommandText = @"Update P1 set Annual_Fees=@af where Sponsorship_Status = 'Sponsored' OR Sponsorship_Status = 'Unsponsored' OR Sponsorship_Status = 'Formerly Sponsored' ";
cmd.Parameters.AddWithValue("@af", updatedAF);
cmd.ExecuteNonQuery();
DataTable dt = new DataTable();
SqlDataAdapter adap = new SqlDataAdapter(cmd);
adap.Fill(dt);
//dataGridViewAFp1.DataSource = dt;
conn.Close();
MessageBox.Show("Record Updated Successfully ");
txtp1AF.Text = " ";
}
【问题讨论】:
-
您是否收到错误消息?
-
我收到
Input string not valid,那是在我更新我的代码之前。没有错误消息,值没有加起来 -
尝试在您的查询中添加括号:[Annual_Fees] 和 [Sponsorship_Status]
-
查询工作正常,逻辑不是 - 想法是,变量 AF2 中的任何值都将添加到该年费列中的任何值
-
你的变量 ResultAF, AF11 ,AF2 被声明为 int ?
标签: c# .net sql-server winforms datagridview