日期:2014-05-18  浏览次数:20960 次

c#.net datagridview值传到textbox修改,更新数据库,得根据主键ID来确定是否更新该记录
strSql.Append("update Pocket_book set");
  if (rdoIncome.Checked) 
  {
  strSql.Append(" IO_Project = '" + cboIncome.Text + "'");
  }
  else
  {
  strSql.Append(" IO_Project = '" + cboOutcome.Text + "'");

  }
  if (Convert.ToDouble(txtMoney.Text) == 0)
  {
  strSql.Append(" ,Output_Money = '" + txtMoney.Text + "'");
  }
  else
  {
  strSql.Append(" ,Input_Money = '" + txtMoney.Text + "'");
  }
  strSql.Append(" ,IO_date = '" + dtpkDate.Text + "'");
  strSql.Append(" ,Project_Explain = '" + txtExplain.Text + "'");
  strSql.Append(" where ");
  strSql.Append(" Main_ID = " + strMain_ID);<-------出错 说没有定义“strMain_ID”

但是我在前面定义了:
  public partial class MyAccountUpdate : Form
  {
  public MyAccountUpdate()
  {
   
  string mainID = string.Empty;
  string strExplainName = string.Empty;
  string strInMoneyName = string.Empty;
  string strOutMoneyName = string.Empty;
  string strProjectName = string.Empty;
  string strDateName = string.Empty;
  string strMain_ID = string.Empty;

  InitializeComponent();
  }

  private void MyAccountUpdate_Load(object sender, EventArgs e)
  {
   
  string strInMoneyName = txtMoney.Text;
  string strProjectName = txtExplain.Text;
  string strOutMoneyName = txtMoney.Text;
  string strExplainName = cboIncome.Text;
  string strDateName = dtpkDate.Text;
  string strMain_ID = txtMain_id.Text;



这是怎么回事啊 在线等!!!!
谢谢了!!!!!!!!!!!!!

------解决方案--------------------
请问你这前后两段代码是否都在 MyAccountUpdate_Load 方法中呢,strMain_ID 是局部变量,如果出了MyAccountUpdate_Load 就无法访问了