I have a query in SQL Server 2008.
SELECT
CONVERT(char(80), i.InvDate,3) AS InvDate,
i.InvoiceNo,
i.TaxAmount + i.SubTotal AS Amount,
'' AS Payment,
pd.GivenName
FROM
dbo.Invoice i
INNER JOIN dbo.PatientDetails pd ON (pd.MedicalRecordID = i.MedicalRecordID)
WHERE (InvDate >= CONVERT(datetime, '16/10/2012', 105)) AND
(InvDate <= CONVERT(datetime, '16/10/2012', 105))
Output:
When i tried to put that into C#, it says “Conversion failed when converting date and/or time from character string.”
Info: 16/10/2012 are sample dates which i tried to insert into the query to make sure it runs. It’s a old program, thats why its converting from a string. I’m pretty sure its something gotta do with the datetime format. Had a look at http://www.sqlusa.com/bestpractices/datetimeconversion/. But i cant put a finger through it.
SqlCommand objCmd = new SqlCommand("SELECT CONVERT(char(80), i.InvDate,3)
AS InvDate,
i.InvoiceNo, i.TaxAmount + i.SubTotal AS Amount,'' AS Payment,pd.GivenName
FROM
dbo.Invoice i INNER JOIN dbo.PatientDetails pd
ON
(pd.MedicalRecordID = i.MedicalRecordID)
WHERE
(InvDate >= CONVERT(datetime, 'dd/MM/yyyy', 105))
AND
(InvDate <= CONVERT(datetime, 'dd/MM/yyyy', 105))", objConn);
SqlDataReader objReader;
objReader = objCmd.ExecuteReader();
System.IO.FileStream fs = new System.IO.FileStream("C:\\CMSExportedData\\Sales-" +
DateTime.Now.ToString("dd-MM-yyyy") + ".txt", System.IO.FileMode.Create);
System.IO.StreamWriter sw = new System.IO.StreamWriter(fs,
System.Text.Encoding.Default);

CONVERT(datetime, 'dd/MM/yyyy', 105)is attempting to convert the literal textdd/MM/yyyyinto adatetime– that’s never going to work.You’ll have to fill in the commented parts in the last two lines above. At a guess, from your previous question, they might be
dtpFrom.ValueanddtpTo.Value.