string P_Str_cmdtxt = String.Empty;
string P_Str_selectcondition = this.toolStripComboBox1.Items[this.toolStripComboBox1.SelectedIndex].ToString();
这句出现异常 InvalidArgument=“-1”的值对于“index”无效。
参数名: index
这应该是
toolStripComboBox1控件沒有項目被選擇,所以this.toolStripComboBox1.SelectedIndex是-1
用-1做數組索引,出錯
那么当我改成
if (toolStripComboBox1.SelectedIndex!=-1)
string P_Str_selectcondition = toolStripComboBox1.Items[toolStripComboBox1.SelectedIndex].ToString();
这样用就会提示说"嵌入的语句不能是声明或标记语句" 这个问题怎么解决~
string P_Str_selectcondition = this.toolStripComboBox1.Items[this.toolStripComboBox1.SelectedIndex].ToString();
这句出现异常 InvalidArgument=“-1”的值对于“index”无效。
参数名: index
这应该是
toolStripComboBox1控件沒有項目被選擇,所以this.toolStripComboBox1.SelectedIndex是-1
用-1做數組索引,出錯
那么当我改成
if (toolStripComboBox1.SelectedIndex!=-1)
string P_Str_selectcondition = toolStripComboBox1.Items[toolStripComboBox1.SelectedIndex].ToString();
这样用就会提示说"嵌入的语句不能是声明或标记语句" 这个问题怎么解决~
string P_Str_selectcondition;
if (...)
P_Str_selectcondition = ...
还有,lz要考虑,如果if条件不成立的时候,P_Str_selectcondition应该是什么值
{
string P_Str_cmdtxt = String.Empty; string P_Str_selectcondition = this.toolStripComboBox1.Items[this.toolStripComboBox1.SelectedIndex].ToString();
switch (P_Str_selectcondition)
{
case"编号":
P_Str_cmdtxt="SELECT * FROM 个人简历";
P_Str_cmdtxt += " WHERE 编号 LIKE '%" + this.toolStripTextBox1.Text + "%'";
this.dataGridView1 .DataSource = G_SqlClass.GetDs(P_Str_cmdtxt).Tables[0];
break ;
case "姓名":
P_Str_cmdtxt = "SELECT * FROM 个人简历";
P_Str_cmdtxt += " WHERE 姓名 LIKE '%" + this.toolStripTextBox1.Text + "%'";
this.dataGridView1 .DataSource = G_SqlClass.GetDs(P_Str_cmdtxt).Tables[0];
break ;
case"职称":
P_Str_cmdtxt = "SELECT * FROM 个人简历";
P_Str_cmdtxt += " WHERE 职称 LIKE '%" + this.toolStripTextBox1.Text + "%'";
this.dataGridView1.DataSource = G_SqlClass.GetDs(P_Str_cmdtxt).Tables[0];
break;
case "职务":
P_Str_cmdtxt = "SELECT * FROM 个人简历";
P_Str_cmdtxt += " WHERE 职务 LIKE '%" + this.toolStripTextBox1.Text + "%'";
this.dataGridView1.DataSource = G_SqlClass.GetDs(P_Str_cmdtxt).Tables[0];
break;
default:
break;
}
我的原来的代码是这样的
我改了之后
private void toolFind_Click(object sender, EventArgs e)
{
string P_Str_cmdtxt = String.Empty; string P_Str_selectcondition;
if (toolStripComboBox1.SelectedIndex != -1)
P_Str_selectcondition = toolStripComboBox1.Items[toolStripComboBox1.SelectedIndex].ToString();
switch (P_Str_selectcondition)
{
case"编号":
P_Str_cmdtxt="SELECT * FROM 个人简历";
P_Str_cmdtxt += " WHERE 编号 LIKE '%" + this.toolStripTextBox1.Text + "%'";
this.dataGridView1 .DataSource = G_SqlClass.GetDs(P_Str_cmdtxt).Tables[0];
break ;
case "姓名":
P_Str_cmdtxt = "SELECT * FROM 个人简历";
P_Str_cmdtxt += " WHERE 姓名 LIKE '%" + this.toolStripTextBox1.Text + "%'";
this.dataGridView1 .DataSource = G_SqlClass.GetDs(P_Str_cmdtxt).Tables[0];
break ;
case"职称":
P_Str_cmdtxt = "SELECT * FROM 个人简历";
P_Str_cmdtxt += " WHERE 职称 LIKE '%" + this.toolStripTextBox1.Text + "%'";
this.dataGridView1.DataSource = G_SqlClass.GetDs(P_Str_cmdtxt).Tables[0];
break;
case "职务":
P_Str_cmdtxt = "SELECT * FROM 个人简历";
P_Str_cmdtxt += " WHERE 职务 LIKE '%" + this.toolStripTextBox1.Text + "%'";
this.dataGridView1.DataSource = G_SqlClass.GetDs(P_Str_cmdtxt).Tables[0];
break;
default:
break;
}
else
MessageBox .Show ("请选择查询条件");
会产生错误 1 使用了未赋值的局部变量“P_Str_selectcondition”
if (...)
P_Str_selectcondition = ...
为什么不可以在if里面定义string????