Ok. I'm new in c# and I'm trying to create an online booking service.

I have the file: dbConnClass.cs where I have the db connection/statement as shown:

public void getPrice(int fromDest, int toDest, int totalPass)
        {
            string sqlStatement =   "SELECT * " +
                                    "FROM   destPrices "+
                                    "WHERE  fromID = @fromID " +
                                        "AND toID = @toID " +
                                        "AND maxPassengers = @maxPassengers";

            SqlCommand cmd = new SqlCommand(sqlStatement, cn);

            cmd.Parameters.AddWithValue("@fromID", fromDest);
            cmd.Parameters.AddWithValue("@toID", toDest);
            cmd.Parameters.AddWithValue("@maxPassengers", totalPass);

            try
            {
                SqlDataReader dr = cmd.ExecuteReader();


                if (dr.HasRows)
                {

                    while (dr.Read())
                    {
                        dbData dbGetSet = new dbData();

                        dbGetSet.oneWayPrice = Convert.ToDecimal(dr["price"]);
                        
                        if (dbGetSet.radValue == "radRoundTrip")
                        {
                            double roundTripPrice = (double)dbGetSet.oneWayPrice * 1.85;
                            dbGetSet.roundTripPrice = (decimal)roundTripPrice;
                        }
                    }
                }
            }
            catch (SqlException e)
            {
                string error = e.Errors[0].Message;

                //HttpContext.Current.Response.Write("Error: {0} " + e.Errors[0].Message);
            }
        }

The accessor file:

public class dbData
    {

        private string _radValue;
        private int _ddlLeavingFrom;
        private int _ddlDestination;
        private int _ddlCellRental;
        private DateTime _txtDate;
        private DateTime _txtTime;
        private DateTime _txtReturnTime;
        private int _txtTotalPassengers;
        private decimal _oneWayPrice;
        private decimal _roundTripPrice;

        public string radValue
        {
            get { return _radValue; }
            set { _radValue = value; }
        }

        public int ddlLeavingFrom
        {
            get { return _ddlLeavingFrom; }
            set { _ddlLeavingFrom = value; }
        }

        public int ddlDestination
        {
            get { return _ddlDestination; }
            set { _ddlDestination = value; }
        }

        public int ddlCellRental
        {
            get { return _ddlCellRental; }
            set { _ddlCellRental = value; }
        }

        public DateTime txtDate
        {
            get { return _txtDate; }
            set { _txtDate = value; }
        }

        public DateTime txtTime
        {
            get { return _txtTime; }
            set { _txtTime = value; }
        }

        public DateTime txtReturnTime
        {
            get { return _txtReturnTime; }
            set { _txtReturnTime = value; }
        }

        public int txtTotalPassengers
        {
            get { return _txtTotalPassengers; }
            set { _txtTotalPassengers = value; }
        }

        public decimal oneWayPrice
        {
            get { return _oneWayPrice; }
            set { _oneWayPrice = value; }
        }

        public decimal roundTripPrice
        {
            get { return _roundTripPrice; }
            set { _roundTripPrice = value; }
        }
        
        
    }

and the file: index.aspx.cs with the code as shown:

protected void btnIndexQuoPrice_Click(object sender, EventArgs e)
        {
            #region dbConn

            dbConnCLASS dbConn = new dbConnCLASS();

            string openConnectionString = "Data Source=??????????;" +
                                           "Initial Catalog=?????; User ID=?????; Password=?????;";

            dbConn.openConn(openConnectionString);

            #endregion

            dbData dbGetSetData = new dbData();

            #region radio buttons value

            if (radOneWay.Checked)
            {
                dbGetSetData.radValue = "radOneWay";
            }
            else if (radRoundTrip.Checked)
            {
                dbGetSetData.radValue = "radRoundTrip";
            }
            else if (radMultiPackages.Checked)
            {
                dbGetSetData.radValue = "radMultiPackages";
            }
            else
            {

            }

            #endregion

            #region ddl values

            dbGetSetData.ddlLeavingFrom = Convert.ToInt16(ddlLeaving.SelectedValue);
            dbGetSetData.ddlDestination = Convert.ToInt16(ddlDestination.SelectedValue);
            dbGetSetData.ddlCellRental = Convert.ToInt16(ddlCellRental.SelectedValue);

            #endregion

            #region Text Boxes

            dbGetSetData.txtDate = Convert.ToDateTime(txtDate.Text);
            dbGetSetData.txtTime = Convert.ToDateTime(txtLeavingTime.Text);
            dbGetSetData.txtReturnTime = Convert.ToDateTime(txtReturningTime.Text);
            dbGetSetData.txtTotalPassengers = Convert.ToInt16(txtTotalPass.Text);

            #endregion

            dbConn.getPrice(dbGetSetData.ddlLeavingFrom, dbGetSetData.ddlDestination, dbGetSetData.txtTotalPassengers);

            lblTotalPrice.Text = Convert.ToString(dbGetSetData.oneWayPrice);
        }
         
    }

I can access the db, query the data but when I try to display (last line of code just above ), the value of the oneWayPrice is 0 which makes no sense since in the query it found the correct value and added to _oneWayPrice on dbData.cs.

Thanks for the help.

Recommended Answers

All 5 Replies

Are you positive "price" is coming back in your DataReader?

In your first block of code, you declare a new dbData in line 25. Where in your code do you pass that back to the calling code? You don't. So the dbData you declare in line 15 of the last block of code has no relation to the other dbData at all.

In your first block of code, you declare a new dbData in line 25. Where in your code do you pass that back to the calling code? You don't. So the dbData you declare in line 15 of the last block of code has no relation to the other dbData at all.

So, you're saying that since I declared a new dbData on the last block of code, it is a new instance and doesn't have the values that I passed right?

Sorry for my ignorance but how do I pass the first dbData back?

Thanks

Are you positive "price" is coming back in your DataReader?

I'm positive because I debugged line-per-line and it was coming back from dr.

You pass it back as a return value. Change the method

public void getPrice(int fromDest, int toDest, int totalPass)

to be

public dbData getPrice(int fromDest, int toDest, int totalPass)

. You'll need to change your code so that you actually return it, and you need to accept the return when you call the method.

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.