Click here to Skip to main content
15,912,578 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi,

I'm getting Exception on this only Client Machine, While build and Execute in local machine didn't get any Exception. Please go through let me know...


<pre>DateTime LastRunAt = new DateTime(2000,08,15);
            //
            string query = "SELECT golden_id FROM `golden_details` WHERE `golden_name` = '" + goldenName + "'";
            MySQLDataBaseHelper.Result gId = db.ExecuteNonQuery(query);
            if (gId.ResultSet.Rows.Count > 0)
            {
                query = "SELECT `datetime` FROM `" + gId.ResultSet.Rows[0]["golden_id"] + "_golden_audit_trail_details` WHERE `id` = (SELECT Max(id) FROM `" + gId.ResultSet.Rows[0]["golden_id"] + "_golden_audit_trail_details`)";
                MySQLDataBaseHelper.Result gUDt = db.ExecuteNonQuery(query);
                IFormatProvider provider = new CultureInfo("en-US", true);
                if (gUDt.ResultSet.Rows.Count > 0)
                    //LastRunAt =Convert.ToDateTime(gUDt.ResultSet.Rows[0]["datetime"].ToString());
                    LastRunAt = DateTime.Parse(gUDt.ResultSet.Rows[0]["datetime"].ToString(),provider,DateTimeStyles.AdjustToUniversal);
            }

            //

            if (r.Success && r.NumberOfRecords > 0)
            {
                string lastRunDatePath = Path.Combine(Application.StartupPath, "LastRunAt");
                List<string> jobIDs = r.ResultSet.AsEnumerable().Select(p => "" + p.Field<int>("job_id")).ToList();
                foreach (string jID in jobIDs)
                {
                    MySQLDataBaseHelper.Result rID = db.ExecuteNonQuery("SELECT `last_run_at` FROM `job_details` WHERE `job_id` = "+jID);
                    IFormatProvider provider1 = new CultureInfo("en-US", true);
                    //DateTime time = Convert.ToDateTime(rID.ResultSet.Rows[0]["last_run_at"].ToString());
                    DateTime time = DateTime.Parse(rID.ResultSet.Rows[0]["last_run_at"].ToString(), provider1, DateTimeStyles.NoCurrentDateDefault);
                    if (LastRunAt.CompareTo(time) < 0)
                        LastRunAt = time;
                }
                Directory.CreateDirectory(lastRunDatePath);
            }
            return LastRunAt;


What I have tried:

I'm getting Exception on this only Client Machine, While build and Execute in local machine didn't get any Exception. Please go through let me know...
Posted
Updated 18-Dec-17 4:07am
Comments
F-ES Sitecore 18-Dec-17 8:29am    
You don't think the line the error occurs on is of any relevance to solving the problem?

You'll be making assumptions about date formats that are based on your settings, but different people have different settings. If you ask the code to convert "1/1/2000" to a date it will work on your machine where that is your date format setting, but if I am in Japan and my setting is 2000-1-1 then it won't work as the computer doesn't know what "1/1/2000" means.
chellapandi160 18-Dec-17 8:41am    
String was not recognized as a valid datetime. at System.DateTime.Parse(String s, DateTimeFormatInfo dtfi,DateTimeStyles styles)

This Exeception i'm getting all other PC. Except my PC..
F-ES Sitecore 18-Dec-17 10:18am    
If "datetime" and "last_run_at" are date fields in the database then don't use ToString, use the generic method instead

https://stackoverflow.com/questions/1106204/retrieving-a-datetime-value-from-a-datarow-c

If these values are stored as strings then you'll need to know the format they are stored in and use DateTime.TryParseExact rather than Parse.
Richard MacCutchan 18-Dec-17 8:44am    
Why are you converting a DateTime value to a string, just so you can parse it back to a DateTime?

1 solution

The only real solution to this is to store your datetime values as DATETIME, DATE, or DATETIME2 in your Database - that way you Parse it using the settings for the user that entered the date when it is entered, and it never needs to be converted again. Storing it as a string and parsing it later always leads to trouble - and by then you no longer have any user context to help you decide what format the date is in.

So right now you have a database where the information is stored badly and you have no idea if 01/02/03 is supposed to be 1st Feb 2003, 2nd Jan 2003, or the 3rd of Feb 2001.
Change your database, and re-enter all teh date info.

And do yourself a favour: Never concatenate strings to build a SQL command. It leaves you wide open to accidental or deliberate SQL Injection attack which can destroy your entire database. Use Parametrized queries instead.

When you concatenate strings, you cause problems because SQL receives commands like:
SQL
SELECT * FROM MyTable WHERE StreetAddress = 'Baker's Wood'
The quote the user added terminates the string as far as SQL is concerned and you get problems. But it could be worse. If I come along and type this instead: "x';DROP TABLE MyTable;--" Then SQL receives a very different command:
SQL
SELECT * FROM MyTable WHERE StreetAddress = 'x';DROP TABLE MyTable;--'
Which SQL sees as three separate commands:
SQL
SELECT * FROM MyTable WHERE StreetAddress = 'x';
A perfectly valid SELECT
SQL
DROP TABLE MyTable;
A perfectly valid "delete the table" command
SQL
--'
And everything else is a comment.
So it does: selects any matching rows, deletes the table from the DB, and ignores anything else.

So ALWAYS use parameterized queries! Or be prepared to restore your DB from backup frequently. You do take backups regularly, don't you?
 
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