下面的代码是查询选课情况的,用了if 语句后它出错说是: 使用了未赋值的局部变量“str”
郁闷了,不知道怎么回事.
private void Form7_Load(object sender, System.EventArgs e)
{
this.radioButton2.Checked=true ;
this.textBox1.Enabled=true;
this.textBox2.Enabled =false;
this.textBox3.Enabled =false;
}
private void button1_Click(object sender, System.EventArgs e)
{
try
{
if (radioButton2.Checked )
{
SqlDataAdapter adapt;
DataTable dt=new DataTable ();
string strsql="data source= ; initial catalog=注册用户;user id=sa;password=1234;";
string str;
str="select 帐号 as 学号,姓名 from 注册信息 where (所选择的课程 like '"+textBox1.Text+"%')";
adapt=new SqlDataAdapter (str,strsql);
adapt.Fill(dt);
DataView dv=new DataView (dt);
Form6 form6=new Form6 ();
form6.dataGrid1 .DataSource =dv;
form6.label1 .Text ="选择"+textBox1.Text +"的人数为:";
form6.label1 .Left =form6.textBox1 .Left -form6.label1 .Width ;
form6.textBox1 .Text =dt.Rows .Count .ToString ();
form6.Show ();
}
if (radioButton1.Checked )
{
SqlDataAdapter adapt;
DataTable dt=new DataTable ();
string strsql="data source= ; initial catalog=注册用户;user id=sa;password=1234;";
string str;
if (textBox2.Text =="")
str="select 帐号,姓名,所选择的课程 from 注册信息 where 帐号='"+textBox3.Text+"'";
if (textBox3.Text =="")
str="select 帐号,姓名,所选择的课程 from 注册信息 where 姓名='"+textBox2.Text+"'";
if (textBox3.Text !=null && textBox2.Text !=null)
str="select 帐号,姓名,所选择的课程 from 注册信息 where 帐号='"+textBox2.Text+"'and 姓名='"+textBox3.Text+"'";
if(textBox2.Text =="" && textBox3.Text =="")
{
MessageBox.Show ("请输入学号或姓名","提示");
return;
}
adapt=new SqlDataAdapter (str,strsql);
adapt.Fill(dt);
DataView dv=new DataView (dt);
Form6 form6=new Form6 ();
form6.dataGrid1 .DataSource =dv;
form6.label1.Text =dt.Rows [0]["姓名"].ToString ();
form6.label1 .Left =form6.dataGrid1 .Left ;
form6.label1 .Top =form6.dataGrid1 .Top +form6.label1 .Height +10;
form6.Show ();
}
}
catch(Exception ex)
{
MessageBox.Show (ex.Message );
}
}
private void radioButton2_Click(object sender, System.EventArgs e)
{
textBox1.Enabled =true;
textBox2.Enabled =false;
textBox3.Enabled =false;
}
private void radioButton1_Click(object sender, System.EventArgs e)
{
textBox1.Enabled =false;
textBox2.Enabled =true;
textBox3.Enabled =true;
}
}