首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > C# >

小弟我也出现"Input string was not in a correct format"求赐教

2012-04-18 
我也出现Input string was not in a correct format,求赐教代码如下:private void deptSave_Click(objec

我也出现"Input string was not in a correct format",求赐教
代码如下:
  private void deptSave_Click(object sender, EventArgs e)
  {
  string deptName = this.txtDeptName.Text.Trim();
  if (deptName!="")
  {
  string selectedString = "SELECT * FROM dept WHERE deptName='" + deptName + "'";
  //string selectString = "select * from dept where deptName='" + deptName + "'";
  //string insertString="insert into dept values ('"+txtDeptName.Text+"','"+txtDeptManager.Text+"','"+txtDeptTel.Text+"')"
  if (sqlOperator.SelectQuery(selectedString)>0)
  {
  MessageBox.Show("已经存在的部门,无法重复添加");
  txtDeptName.Text = "";
  txtDeptManager.Text = "";
  txtDeptTel.Text = "";
  txtDeptName.Focus();
  }
  else
  {
  this.Validate();
  this.deptBindingSource.EndEdit();
  this.deptTableAdapter.Update(this.assetArtDataSet.dept);
  MessageBox.Show("部门[" + txtDeptName.Text + "]添加成功");
  //保存当前记录后,可再次添加
  this.deptAdd.Enabled = true;
  this.dgvDept.Enabled = true;
  this.txtDeptName.Text = "";
  this.txtDeptManager.Text = "";
  this.txtDeptTel.Text = "";
  this.txtDeptName.Enabled = false;
  this.txtDeptManager.Enabled = false;
  this.txtDeptTel.Enabled = false;  
  }
  }
  else
  {
  MessageBox.Show("\"部门名称\"不能为空.");
  }


SelectQuery代码如下:
  public static int SelectQuery(string selectString)
  {
  int returnValue;
  Command.Connection = assetConn();
  try
  {
  if (Connection.State == ConnectionState.Closed)
  {
  Connection.Open();
  }
  Command.CommandText = selectString;
  Command.CommandType = CommandType.Text;
  DataAdapter.SelectCommand = Command;
  returnValue = Convert.ToInt32(Command.ExecuteScalar());
  return returnValue;
  }
  catch (Exception)
  {
   
  return -1;
  }
  finally
  {
  Connection.Close();
  }
  }

以上代码在另外一个winForm中可用.在另外一个却不可用,希望高手指导下.

[解决办法]
Object x = Command.ExecuteScalar(); //ExecuteScalar是第一行第一列的内容,你seelct的是数字类型的吗?
if(x==null) return -1;
returnValue = 0;
int.TryParse(x.ToString(),out returnValue))
return returnValue;
[解决办法]
string selectedString = "SELECT * FROM dept WHERE deptName='" + deptName + "'";
第一列必须是数字
你可以写成
string selectedString = "SELECT xxID FROM dept WHERE deptName='" + deptName + "'";


[解决办法]

探讨

Object x = Command.ExecuteScalar(); //ExecuteScalar是第一行第一列的内容,你seelct的是数字类型的吗?
if(x==null) return -1;
returnValue = 0;
int.TryParse(x.ToString(),out returnValue))
return returnValue;

[解决办法]
返回的方法都是一样的
也是
Command.ExecuteScalar()

这个返回值是Object的ToString()即可
[解决办法]
catch语句里写
return "";
因为你返回的是字符串类型的,根据需要需要进行返回

热点排行