- 爱易网页
 
                        - 
                            C#教程
 
                        - 小弟我也出现"Input string was not in a correct format"求赐教 
 
                         
                    
                    
                    日期:2014-05-18  浏览次数:21234 次 
                    
                        
                         我也出现"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 + "'";
第一列必须是数字