Selecting SQL DateTime with C# DateTime variable
<p>I'm trying to run a query that selects all from a table that contains a DateTime. At the DateTime, it uses a DateTime variable (argument passed through the parameter), however it gives me this exception: </p>

<p>"<strong>FormatException was unhandled</strong>
An unhandled exception of type 'System.FormatException' occurred in mscorlib.dll
Additional information: The string was not recognized as a valid datetime"</p>

<p><strong>Solutions I've tried, but didn't work:</strong></p>

<ul>
<li>Convert the DateTime to string</li>
<li>Convert the DateTime to string with correct format and then back to
DateTime again</li>
<li>Use the DateTime.Parse method</li>
<li>Use the DateTime.ParseExact method with correct format and using both
"CultureInfo.InvariantCulture" and "CreateSpecificCulture("en-GB")</li>
<li>Change the format to the exact same shown when running the query in
Database</li>
</ul>

<p>As said, none of the above have worked..</p>

<ul>
<li>The format I try to use: "MM/dd/yyyy 00:00:00"</li>
<li>Format shown when running query in database: "MM/dd/yyyy
hh:mm:ss:fff" (the time needs to be set to 00:00:00)</li>
</ul>

<p><strong>Here is the code with the connection stuff excluded:</strong></p>

<pre><code>cmd = new SqlCommand("SELECT * FROM Arithmetics "
                    + "INNER JOIN ArithmeticsAndTypeIDs "
                    + "ON Arithmetics.ArithmeticID = ArithmeticsAndTypeIDs.ArithmeticsID "
                    + "INNER JOIN ArithmeticTypes "
                    + "ON ArithmeticsAndTypeIDs.TypeID = ArithmeticTypes.TypeID "
                    + "WHERE UserID = @value1 "
                    + "AND TimeStamp &gt;= '@value2' "
                    + "AND TimeStamp &lt;= '@value3'", con);
                cmd.Parameters.AddWithValue("@value1", GetUserID(userLoggedIn));
                cmd.Parameters.AddWithValue("@value2", DateTime.ParseExact(time.ToString(), formatString, CultureInfo.CreateSpecificCulture("en-GB").DateTimeFormat));
                time = time.AddDays(1);
                cmd.Parameters.AddWithValue("@value3", DateTime.ParseExact(time.ToString(), formatString, CultureInfo.CreateSpecificCulture("en-GB").DateTimeFormat));
</code></pre>

<p>The problem first occurs at the line: "+ "AND TimeStamp >= '@value2' ""</p>

<p><strong>* And in case it makes a difference, here is the quote I use in the database which obv. works (with the variables replaced by placeholder values)</strong></p>

<pre><code>    SELECT * FROM Arithmetics
    INNER JOIN ArithmeticsAndTypeIDs 
    ON Arithmetics.ArithmeticID = ArithmeticsAndTypeIDs.ArithmeticsID
    INNER JOIN ArithmeticTypes 
    ON ArithmeticsAndTypeIDs.TypeID = ArithmeticTypes.TypeID
    WHERE UserID = 0
    AND
    ArithmeticTypes.TypeID = 2
    AND TimeStamp &gt;= '09-21-2016 00:00:00'
    AND TimeStamp &lt;= '09-22-2016 00:00:00'
</code></pre>

<p><strong>It should be noted that "@value2" contains the value ("09-21-2016 00:00:00") (assuming that's the date that is passed as a parameter), so I know that the formatting is correct.</strong></p>

<p><strong>Any help towards fixing this issue would be greatly appreciated :)</strong></p>