为什么我的程序如果没有正确执行?

编程入门 行业动态 更新时间:2024-10-27 02:30:32
本文介绍了为什么我的程序如果没有正确执行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

您好,我正在测试三种不同的条件; 第一个是dropDown1 == 2的selectedIndex,而TextBox是空的 其次是selectedIndex of DropDown1 == 2并且TextBox不为空 最后一个是dropDown的selectedIndex == 3 当执行它直接进入Last条件并且甚至没有测试第一个。 这是我的代码

Hello, I am testing on three different conditions; First one is the selectedIndex of the dropDown1==2 and the TextBox is empty Second is the selectedIndex of the DropDown1==2 and the TextBox is not empty Last one is the selectedIndex of the dropDown ==3 When executing It goes directly to the Last condition and doesnt even test the first ones. This is my code

protected void btnValider_Click(object sender, EventArgs e) { if (cmbStatut.SelectedIndex == 2) { if (txtNvSt.Text != null) { con.charger("update Materiel set reparation= NULL where serviceTag='" + txtServiceTag.Text + "'", false); Session["ST"] = txtNvSt.Text; Response.Redirect("NouveauMAt.aspx"); } else if (txtNvSt.Text == null) { con.charger("insert into Stocker values('1', '" + txtServiceTag.Text + "')", false); } } else { con.charger("update Materiel set reparation = NULL, idEmplacement = NULL where serviceTag='" + txtServiceTag.Text + "'", false); Response.Redirect("StockHS.aspx"); } }

推荐答案

属性文字永远不会是空的。它可以是空的,只能包含空格,依此类推。如果你需要测试一些输入的数据是否为空,你可以这样做 The property Text is never null. It could be empty, could contain only blank spaces, and so on. If you need to test if some entered data is empty, you can do this if (txtNvSt.Text == string.Empty) //...

or

if (string.IsNullOrEmpty(txtNvSt.Text)) //...

此外,您可能需要考虑类似

Also, you may want to consider something like

if (txtNvSt.Text.Trim() != string.Empty) //...

第一个和最后一个片段基本上基于以下假设 txtNvSt.Text 永远不会为空。在其他可能为null的情况下,您还应该检查null。 要查看代码的哪一部分真的已执行,以防万一甚至丝毫怀疑,总是使用调试器。它还会告诉你 condition 的哪一部分是真还是假。 :-)

-SA

The first and last fragment essentially based on assumption that txtNvSt.Text is never null. In other cases where null is possible you should also check for null. To see what part of your code is really executed, in case of even slightest doubt, always use the debugger. It will also tell you which part of condition is true or false. :-)

—SA

你的字符串可能不是空的,而是空的。 使用 String.IsNullOrEmpty() Your string is probably not null, but empty. Use String.IsNullOrEmpty() protected void btnValider_Click(object sender, EventArgs e) { if (cmbStatut.SelectedIndex == 2) { if (!String.IsNullOrEmpty(txtNvSt.Text)) { con.charger("update Materiel set reparation= NULL where serviceTag='" + txtServiceTag.Text + "'", false); Session["ST"] = txtNvSt.Text; Response.Redirect("NouveauMAt.aspx"); } else { con.charger("insert into Stocker values('1', '" + txtServiceTag.Text + "')", false); } } else if (cmbStatut.SelectedIndex == 3) { con.charger("update Materiel set reparation = NULL, idEmplacement = NULL where serviceTag='" + txtServiceTag.Text + "'", false); Response.Redirect("StockHS.aspx"); } else { // Unsupported case } }

除了提供的其他解决方案通过SA和George,您可以使用switch-case语句而不是看起来更结构化的if-else: In addition to the other solutions provided by SA and George, you may use switch-case statements instead of if-else which looks more structured: protected void btnValider_Click(object sender, EventArgs e) { switch (cmbStatut.SelectedIndex) { case 2: if (!string.IsNullOrEmpty(txtNvSt.Text)) { con.charger("update Materiel set reparation= NULL where serviceTag='" + txtServiceTag.Text + "'", false); Session["ST"] = txtNvSt.Text; Response.Redirect("NouveauMAt.aspx"); } else { con.charger("insert into Stocker values('1', '" + txtServiceTag.Text + "')", false); } break; case 3: con.charger("update Materiel set reparation = NULL, idEmplacement = NULL where serviceTag='" + txtServiceTag.Text + "'", false); Response.Redirect("StockHS.aspx"); break; default: // Write code here to handle any other value } }

更多推荐

为什么我的程序如果没有正确执行?

本文发布于:2023-07-07 09:08:27,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1061428.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:如果没有   正确   程序

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!