Hi, I've got a input sheet where start and end dates are saved to a access database, I do have the access database dates field as Date/Time with a short date format. On my output page the start date's month and day are swapped but only if the actual day is between(1-12) else its fine. I've checked the database it does save the date as yyyy/mm/dd. But I've got the same code for start and end date, but only startDate has this problem.

I don't where this error may occure and I do have so much code, so could someone please give me some advice.

Oh this only happens on the server, not on the localhost.

Recommended Answers

All 4 Replies

Can you post the code you're having a problem with please?

I'm not sure where the problem is coming from but here is some code.

This is where the data is added to the access database in input.aspx.cs, for the dates a javascript date picker is used.

public void addData()
    {
        string name = name_txt.Text;
        string currency = currency_txt.Text;
        string cost = cost_txt.Text;
        string start = testinput.Text;
        string completion = testinput2.Text;
        string locality = locality_dd.SelectedItem.Text;
        string sector = sector_dd.SelectedItem.Text;
        string superstructure = superstructure_dd.SelectedItem.Text;
        string lifts = lifts_dd.SelectedItem.Text;
        string excavation = excavation_dd.SelectedItem.Text;
        int validation = Int32.Parse(validation_txt.Text);
        string holiday_str = holiday_txt.Text;
        int holiday = 0;
        if (holiday_str == "Yes")
        {
            holiday = 1;
        }
        else
        {
            holiday = 0;
        }
        string alpha_beta = locality_dd.SelectedValue.ToString() + sector_dd.SelectedValue.ToString() +
            superstructure_dd.SelectedValue.ToString() + lifts_dd.SelectedValue.ToString() +
            excavation_dd.SelectedValue.ToString();
        int code = Int32.Parse(alpha_beta);

        //string scon =
        //    "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=d:\\www\\solarsearch.co.za\\wwwroot\\calc\\app_data\\calculator.mdb";
        string scon =
            "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=|DataDirectory|calculator.mdb";
        string str =
           "INSERT INTO form_input(identification, symbol, cost, start, completion, locality, sector, superstructure, lifts, excavation, validation, holiday, code)values(name, currency, cost, start, completion, locality, sector, superstructure, lifts, excavation, validation, holiday, code)";
        string id = "Select @@Identity";
        int f_id;
        using (OleDbConnection con = new OleDbConnection(scon))
        {
            using (OleDbCommand cmd = new OleDbCommand(str, con))
            {
                cmd.CommandType = CommandType.Text;
                cmd.Parameters.AddWithValue("identification", name);
                cmd.Parameters.AddWithValue("symbol", currency);
                cmd.Parameters.AddWithValue("cost", cost);
                cmd.Parameters.AddWithValue("start", start);
                cmd.Parameters.AddWithValue("completion", completion);
                cmd.Parameters.AddWithValue("locality", locality);
                cmd.Parameters.AddWithValue("sector", sector);
                cmd.Parameters.AddWithValue("superstructure", superstructure);
                cmd.Parameters.AddWithValue("lifts", lifts);
                cmd.Parameters.AddWithValue("excavation", excavation);
                cmd.Parameters.AddWithValue("validation", validation);
                cmd.Parameters.AddWithValue("holiday", holiday);
                cmd.Parameters.AddWithValue("code", code);
                con.Open();
                cmd.ExecuteNonQuery();
                cmd.CommandText = id;
                f_id = (int)cmd.ExecuteScalar();

                message.Text = "Submission was Successful";
                Response.Redirect("output.aspx?f_id=" + f_id);
            }
        } 
    }

This is where the data is read from the database output.aspx.cs

public DataSet GetTable()
    {
        string f_id = Request.QueryString["f_id"];

        //string strConnString =
        //  "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=d:\\www\\solarsearch.co.za\\wwwroot\\calc\\app_data\\calculator.mdb";
        string strConnString =
            "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=|DataDirectory|calculator.mdb";

        OleDbConnection con = new OleDbConnection(strConnString);
        con.Open();

            // read from form_input table
        string strQuery = "select * from form_input where f_id=" + f_id;
        OleDbCommand cmd = new OleDbCommand(strQuery, con);
        OleDbDataReader reader = cmd.ExecuteReader();
        reader.Read();

        string company = reader["identification"].ToString();
        string start_str = reader["start"].ToString();
        DateTime start = DateTime.Parse(start_str);
        string end_str = reader["completion"].ToString();
        DateTime end = DateTime.Parse(end_str);
        string cost = reader["cost"].ToString();
        double b = double.Parse(cost);
        string validDay_str = reader["validation"].ToString();
        int validDay = Int32.Parse(validDay_str);
        string code = reader["code"].ToString();
        string holiday_str = reader["holiday"].ToString();
        int holiday = Int32.Parse(holiday_str);
        string symbol = reader["symbol"].ToString();
        string locality_str = reader["locality"].ToString();
        string sector_str = reader["sector"].ToString();
        string super_str = reader["superstructure"].ToString();
        string lifts_str = reader["lifts"].ToString();
        string excavation_str = reader["excavation"].ToString();

            // read from alpha_beta table
        string strQuery2 = "select * from alpha_beta where code=" + code;
        OleDbCommand cmd2 = new OleDbCommand(strQuery2, con);
        OleDbDataReader reader2 = cmd2.ExecuteReader();
        reader2.Read();
        string construction_a_str = reader2["construction_a"].ToString();
        string construction_b_str = reader2["construction_b"].ToString();
        string top_a_str = reader2["top_a"].ToString();
        string top_b_str = reader2["top_b"].ToString();
        string bottom_a_str = reader2["bottom_a"].ToString();
        string bottom_b_str = reader2["bottom_b"].ToString();

            // alpha_beta values
        double construction_a = double.Parse(construction_a_str);
        double construction_b = double.Parse(construction_b_str);
        double top_a = double.Parse(top_a_str);
        double top_b = double.Parse(top_b_str);
        double bottom_a = double.Parse(bottom_a_str);
        double bottom_b = double.Parse(bottom_b_str);

            // display labels
        companyName.Text = company;
        todayDate.Text = (String.Format("{0:yyyy/MM/dd}", DateTime.Now));
        currency.Text = symbol;
        constructionCost.Text = cost;
        startDate.Text = (String.Format("{0:dd MMMM yyyy}", start)); //here values are swapped as well as with the valuation dates later on
        endDate.Text = (String.Format("{0:dd MMMM yyyy}", end));
        valuationDay.Text = validDay_str;
        locality.Text = locality_str;
        sector.Text = sector_str;
        super.Text = super_str;
        lifts.Text = lifts_str;
        mass.Text = excavation_str;

        if (holiday == 1)
            apply.Text = "Yes";
        else
            apply.Text = "No";

            // display
        DataSet ds = new DataSet();
        DataTable dt = ds.Tables.Add("Table");
        dt.Columns.Add(new DataColumn("Valuation Date", typeof(string)));
        dt.Columns.Add(new DataColumn("Construction Cash Flow", typeof(string)));
        dt.Columns.Add(new DataColumn("Upper Probability Limit", typeof(string)));
        dt.Columns.Add(new DataColumn("Lower Probability Limit", typeof(string)));

            // fill rows
        foreach (DateTime valuationDate in GetDates(start, end, validDay))
        {
            int numberOfDays = 0;
            if (valuationDate.Month == 12 || valuationDate.Month == 01)
            {
                numberOfDays = 14;
            }
            current += numberOfDays;

            DataRow row = dt.NewRow();
            row["Valuation Date"] = valuationDate.ToString("yyyy/MM/dd");
            row["Construction Cash Flow"] = Calculation(current, holiday, validDay, b, valuationDate, start, end, construction_a, construction_B);
            row["Upper Probability Limit"] = Calculation(current, holiday, validDay, b, valuationDate, start, end, top_a, top_B);
            row["Lower Probability Limit"] = Calculation(current, holiday, validDay, b, valuationDate, start, end, bottom_a, bottom_B);
            dt.Rows.Add(row);
        }

        reader2.Close();
        reader.Close();
        con.Close();
        return ds;
    }

Ok, I think your problem is that the Parse method is attempting to convert the DateTime from US style (mm/dd/yyy) to your current culture (dd/mm/yyyy) If you want the datetime parsed correctly, you must use the DateTime.Parse(String, IFormatProvidor) overload and parse it using the American style. That way, your code will store it correctly.

Here is a link to it on the MSDN

Thanks I will check it out

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.