hi there

Below is my code for a webmethod which checks if data is existent in a table and then amends if bool is true or adds new if bool is false

For some reason my amend works fine but it seems to jump the else condition altogether. Only passes the if when gets to else comes back to the if again

I tested it by swapping the condition around and it does the same thing....skips the else
would really appreciate if someone can look at the code and tell me where I am going wrong...

WebMethod(Description= "Add or Amend Availability & Rates")]

    public bool Availability(string username, string password, DateTime dattm,int ResortID, string RoomType, int Qty, double curprice)
    {

        bool boolReturnValue = false;

        GetAuthCredentials(username, password);
        ResortID = UserAccounts.strTypeID;
        // RoomType = ResortRooms.strRoomType;
        //int intPriceType = 0;
        Available.curPrice = curprice;
        Available.dtm = dattm;
        //Available.intPriceType = intPriceType;
        Available.intQty = Qty;
        Available.intResortID = UserAccounts.strTypeID;
        Available.strRoomType = RoomType;

        string connStr = ConfigurationManager.ConnectionStrings["bedbankstandssConnectionString"].ConnectionString;

        SqlConnection mySQLconnection = new SqlConnection(connStr);

        String strSQL = "select * from available where ResortID="+UserAccounts.strTypeID;

        SqlCommand dbCommand = new SqlCommand(strSQL,mySQLconnection);

        SqlDataReader dr;
        mySQLconnection.Open();
        dr = dbCommand.ExecuteReader();
        while (dr.Read())
        {


            if (dattm== Convert.ToDateTime(dr["dtm"].ToString()) & ResortID == Convert.ToInt16(dr["intResortID"].ToString()) & (RoomType == dr["strRoomType"].ToString()))
            {

             boolReturnValue = true;

            }

            if (boolReturnValue == true)
            { 
                 AmendAvailability(username, password, dattm, RoomType, Qty, curprice);
            }

            else 
            {

                AddAvailability(username, password, ResortID, dattm, RoomType, Qty, curprice);

            }




        }
        dr.Close();

        return boolReturnValue;


    }

thanks

Recommended Answers

All 6 Replies

You need to reset your bool to false on each loop inside the Read(){}. If you don't, the first "true" will cause all subsequent tests to be "true".

Also, please use the [ CODE ] tag to show your code as it gives code a proper layout.

Have amended the code but it still only goes as far as the if does not go into the else

[WebMethod(Description= "Add or Amend Availability & Rates")]
        public bool Availability(string username, string password, DateTime dtm,int ResortID, string RoomType, int Qty, double curprice)
        {

            bool boolReturnValue = false;

            GetAuthCredentials(username, password);
            ResortID = UserAccounts.strTypeID;
            // RoomType = ResortRooms.strRoomType;
            //int intPriceType = 0;
            Available.curPrice = curprice;
            Available.dtm = dtm;
            //Available.intPriceType = intPriceType;
            Available.intQty = Qty;
            Available.intResortID = UserAccounts.strTypeID;
            Available.strRoomType = RoomType;

            string connStr = ConfigurationManager.ConnectionStrings["sConnectionString"].ConnectionString;

            SqlConnection mySQLconnection = new SqlConnection(connStr);
            
            String strSQL = "select * from Available where intResortID="+UserAccounts.strTypeID;

            SqlCommand dbCommand = new SqlCommand(strSQL,mySQLconnection);

            SqlDataReader dr;
            mySQLconnection.Open();
            dr = dbCommand.ExecuteReader();
            while (dr.Read())
            {
                
               
                if (dtm == Convert.ToDateTime(dr["dtm"].ToString()) & ResortID == Convert.ToInt16(dr["intResortID"].ToString()) & (RoomType == dr["strRoomType"].ToString()))
                {

                 boolReturnValue = false;

                }

                if (boolReturnValue == false)
                {
                   AddAvailability(username, password, ResortID, dtm, RoomType, Qty, curprice);
                }

                else 
                {

                    AmendAvailability(username, password, dtm, RoomType, Qty, curprice);
                     
                }


               

            }
            dr.Close();

            return boolReturnValue;
           
        
        }

As I said on the other forum, once you set the boolean value to false IT NEVER CHANGES. All records after that one will be treated as if they passed the if statement check. You need to rethink your logic.

No actually you said once the boolean is set to true, which is why I set it to false this time round.

I hva copied out your reply on the other forum below:

Once boolReturnValue is set to true you never set it back to false. So the first time it is set to true, every record after that is treated as if it were true, also.

If this isn't what you want, set it to false after the if/else where you do the Amend/Adds.

Your code seems rather odd to me in that you can have multiple records but only one return value even though some records may be Amends and some Adds.

Yes, and you didn't do what I said. Set it in line 51 to true since you've now reversed your logic. See, Line 51 is after the if/else where you do the amend/adds. You also need to change it to true in line 5.

thanks...made more sense this time

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.