Click here to Skip to main content
15,891,033 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
C#
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:

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

When txt_idcnt.Text is empty the condition is skipped
Posted
Updated 4-Dec-18 22:43pm
v2
Comments
MadMyche 4-Dec-18 20:54pm    
Does the procedure execute? What is displayed?
Member 12770648 4-Dec-18 20:59pm    
Yes
MadMyche 4-Dec-18 21:04pm    
So that portions works out. What is supposed to change based on your input? The only thing I see is the "No Record Selected" label which solely depends on the input page.
Member 12770648 4-Dec-18 21:13pm    
If a record is selected from a grid to populate the text boxes on a page and txt_idcnt.Text is empty

intno2 = txt_idcnt.Text;

if (intno2 == "")
{
lblstatus.Text = "No record Selected";
}
Member 12770648 4-Dec-18 21:18pm    
Thereafter the ADD button is selected for submission.

If intno2 is empty the conditional statement should kick in.

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;
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;
}
 
Share this answer
 
Make use of the proper methods available in the String class: String.IsNullOrEmpty Method (System) | Microsoft Docs[^].
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900