Thursday, April 30, 2015

else if statement allows false

Hello

I have the following code where i,m trying to see if a CustId exist or not and if it does return data from the database to the textboxes but if not it returns a message in the lblMessage.

But what is happing in my else if statement is allows false so it throws the lblMessage "can not find Customer Id" or an Exception is i change the code to

else if (CustID ==0){
//then code
}

 protected void Button1_Click(object sender, EventArgs e)
        {
            
            conn = new SqlConnection(ConfigurationManager.ConnectionStrings["dbConnection1"].ConnectionString);
            int CustID = 0;
            SqlCommand command = new SqlCommand();

            command.Connection = conn;
            command.CommandType = CommandType.StoredProcedure;
            command.CommandText = "GetCustmer";
            command.Connection.Open();

            SqlParameter param = new SqlParameter();
            param.ParameterName = "@ID";
            param.SqlDbType = SqlDbType.Int;
            param.Direction = ParameterDirection.Input;
            param.Value = txtCustID.Text;
            command.Parameters.Add(param);
            int ID = table.Rows.Count;
            if (string.IsNullOrWhiteSpace(txtCustID.Text))
            {
                //throw new Exception("Id not provided.");
                lblMessage.Text = "You did not enter a Customer Number";

            }
            else if(CustID !=0)
            {
                adapter.SelectCommand = command;
                adapter.Fill(table);

                //txtCustID.Text = table.Rows[0].Field<string>("CustID");
                txtFirstname.Text = table.Rows[0].Field<string>("Firstname");
                txtFirstname.DataBind();
                txtSurname.Text = table.Rows[0].Field<string>("Surname");
                txtSurname.DataBind();
            }
            else {
                lblMessage.Text = "Could not find customer number";
            }
            
        }

Thanks for your help

No comments:

Post a Comment