日期:2014-05-20  浏览次数:20805 次

判断或值报错!使用了未赋值的局部变量
 // 获得歌手姓名
                string name = this.txtName.Text.Trim().ToString();
                // 获得歌手类型
                int number = this.cmbType.SelectedIndex;
                // 获得歌手性别
                String Sex;
                if (this.rdbMale.Checked == true)
                {
                    Sex = "男";
                }
                else if (this.rdbFemale.Checked == true)
                {
                    Sex = "女";
                }
                 else if (this.rdbCombination.Checked == true)
                {
                    Sex = "组合";
                }
                //获得歌手的描述
                String description = this.txtdescription.Text.Trim().ToString();

                //  组合SQL 语句 
                SqlCommand cmd = new SqlCommand();
                cmd.Connection = dbHelper.conn;
                cmd.CommandText =String.Format( " Insert into singer_info ");
                cmd.CommandText +=String.Format(" Values('{0}','{1}','{2}','{3}','{4}')",name,number,Sex,picture,description);


这里Sex 怎么会报错呢!说使用使用使用了未赋值的 局部变量Sex 我全部换成if 也报错 
insert ?判断获值报错!

------解决方案--------------------
  String Sex;
                if (this.rdbMale.Checked == true)
                {
                    Sex = "男";