程序段在ADD按钮中嵌入了“if”子句。 'if'条款不起作用 [英] The program segment has an 'if' clause embedded in an ADD button. The 'if' clause is not working

查看:68
本文介绍了程序段在ADD按钮中嵌入了“if”子句。 'if'条款不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

protected void Add_Click(object sender, EventArgs e)
        {
            //Add Begin
            try
            {
                // Connection to the database
                string str;
                str = ConfigurationManager.ConnectionStrings["WEBHR"].ConnectionString;
                SqlConnection sqlCon = new SqlConnection(str);
                SqlCommand sqlCmd = new SqlCommand("sp_GenerateLeaveID", sqlCon);
                sqlCmd.CommandType = System.Data.CommandType.StoredProcedure;

                // Create and supply the output parameters
                string intno,intno1,intno2;
          
                intno2 = txt_idcnt.Text;

                if (intno2 == "")
                {
                    lblstatus.Text = "No record Selected";
                }

                // Execute the stored procedure
                sqlCmd.ExecuteNonQuery();
                sqlCon.Close();
            }
            catch (Exception ex)
            {
                lblstatus.Text = ex.Message;
            }
            //Add End
        }





我的尝试:





What I have tried:

intno2 = txt_idcnt.Text;
                if (intno2 == "")
                {
                    lblstatus.Text = "No record Selected";
                }



当txt_idcnt.Text为空时,跳过条件


When txt_idcnt.Text is empty the condition is skipped

推荐答案

使用调试器 - 这是找出正在发生的事情的唯一方法。我们无法在相同条件下运行您的代码,因此我们实在无法告诉您执行此操作。



在行上设置断点
Use the debugger - it's the only way to find out what is going on. We can't run your code under the same conditions, so we really can't tell you "do this".

Put a breakpoint on the line
str = ConfigurationManager.ConnectionStrings["WEBHR"].ConnectionString;

当它到达它时,调试器将停止。每行单步执行,仔细查看每个变量并准确跟踪发生的情况。你应该能够自己解决这个问题,但如果你还不能,至少你会有信息来帮助我们解决问题。目前,你所拥有的只是它不起作用,这对任何人都没有帮助!



BTW:我强烈建议你使用在你的SQl对象周围使用块,以确保它们被关闭并正确处理:

And when it reaches it the debugger will stop. Single step each line, looking closely at each variable and follow exactly what is happening. You should be able to work it out for yourself from that, but if you still can't, at least you will have information to give us to help us work it out. At the moment, all you have is "it don't work" which helps no-one!

BTW: I'd strongly suggest that you use using blocks around your SQl objects, to ensure they are closed and disposed correctly:

try
{
    // Connection to the database
    string str;
    str = ConfigurationManager.ConnectionStrings["WEBHR"].ConnectionString;
    using (SqlConnection sqlCon = new SqlConnection(str))
    {
        using (SqlCommand sqlCmd = new SqlCommand("sp_GenerateLeaveID", sqlCon))
        {
            sqlCmd.CommandType = System.Data.CommandType.StoredProcedure;

            // Create and supply the output parameters
            string intno,intno1,intno2;

            intno2 = txt_idcnt.Text;

            if (intno2 == "")
            {
                lblstatus.Text = "No record Selected";
            }

            // Execute the stored procedure
            sqlCmd.ExecuteNonQuery();
        }
    }
}
catch (Exception ex)
{
    lblstatus.Text = ex.Message;
}


使用String类中可用的正确方法:String.IsNullOrEmpty Method(System)| Microsoft Docs [ ^ ]。
Make use of the proper methods available in the String class: String.IsNullOrEmpty Method (System) | Microsoft Docs[^].


这篇关于程序段在ADD按钮中嵌入了“if”子句。 'if'条款不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆