c#中止程序

来源:百度知道 编辑:UC知道 时间:2024/07/05 04:21:14
判断文本框不为空了怎么也不向下执行了
if (nul5 == "")
MessageBox.Show("不允许空格"); return;
float a = Convert.ToInt32(textBox5.Text) * Convert.ToInt32(textBox6.Text);
SqlConnection conn = new SqlConnection("server=localhost;uid=sa;pwd=;database=n");
SqlCommand com = new SqlCommand("P_jinhuo ", conn);
com.CommandType = CommandType.StoredProcedure;
com.Parameters.AddWithValue("@Usermc", textBox1.Text);
com.Parameters.AddWithValue("@Userpz", textBox2.Text);
com.Parameters.AddWithValue("@Usergg", textBox3.Text);
com.Parameters.AddWithValue("@Usertm", textBox4.Text);
com.Parameters.AddWithValue("@Userdj", textBox5.Text);
com.Parameters.AddWithValue("@Usersl", textBox6.Text);
com.Parameters.AddWithValue("@Userz

1.if (nul5 == "")
MessageBox.Show("不允许空格"); return;
方法中有return 那后面的就不执行了。

你可以加断点看,
return 去掉,,不可能不执行下面的程序。
加断点

如你补充,那你现在的代码没有错。
我认为你的疑问是if下面为什么只执行了一句。。
那是因为没有大括号。所以只执行一句

if (nul5 == "")
MessageBox.Show("不允许空格"); return; //没有大括号,if 的作用范围只有这一句不包括下面的,if 判断后记叙下面的代码
float a = Convert.ToInt32(textBox5.Text) * Convert.ToInt32(textBox6.Text);
SqlConnection conn = new SqlConnection("server=localhost;uid=sa;pwd=;database=n");

这样:

String nul5=txt_attm.Text;

if (nul5 == "")
{
MessageBox.Show("不允许空格");
return;
}

if (nul5 == "")
{
MessageBox.Show("不允许空格"); return;
}

你在 float a = Convert.ToInt32(textBox5.Text) * Convert.ToInt32(textBox6.Text);
前面加上else