1

I am writing a text file with some data recorded inside a SQL database.

I am writing some data and one of them is a data, and it writes with this shape: dd/mm/yyyy hh:mm:ss.

How can I make it just writing dd/mm/yyyy.

Here is the code I am using to write the data in the text file:

if (sqlReader["TaxableGroupID"].Equals(1))
{ 
    tw.WriteLine(sqlReader["PartyFederalTaxID"] + ";" + sqlReader["TransSerial"] + sqlReader["TransDocNumber"] + ";" + sqlReader["CreateDate"] + ";" + sqlReader["TotalAmount"] + "\r\n" + sqlReader["BarCode"] + ";" + "23" + ";" + sqlReader["Quantity"]);
}

I have the same problem with the TotalAmount. I want to write it with a . to separate the decimals and it writes with a ,. I have tested this code but didn’t work. The decimal separator still ,.

if (sqlReader["TaxableGroupID"].Equals(1)) 
{
    tw.WriteLine(sqlReader["PartyFederalTaxID"] + ";" + sqlReader["TransSerial"] + sqlReader["TransDocNumber"] + ";" + sqlReader["CreateDate"] + ";" + sqlReader["TotalAmount".ToString(CultureInfo.InvariantCulture)] + "\r\n" + sqlReader["BarCode"] + ";" + "23" + ";" + sqlReader["Quantity"]); 
}

I tried this to fix the date problem but as the . problem, I didn’t get any error but the date stood the same.

if (sqlReader["TaxableGroupID"].Equals(1))
{
    tw.WriteLine(sqlReader["PartyFederalTaxID"] + ";" + sqlReader["TransSerial"] + sqlReader["TransDocNumber"] + ";" + sqlReader["CreateDate".toString("dd/MM/yyyy")] + ";" + sqlReader["TotalAmount".ToString] + "\r\n" + sqlReader["BarCode"] + ";" + "23" + ";" + sqlReader["Quantity"]);
}
Koopakiller
  • 2,838
  • 3
  • 32
  • 47
  • *Your code* specifies the string's format. If it doesn't, *you* (the user's) locale is used to determine the format. The compiler probably already issues warnings that you are using localizable functions without specifying a culture – Panagiotis Kanavos Aug 30 '17 at 10:53
  • BTW *don't* concatenate strings like that, use the TextWriter methods that accept a format string, eg [WriteLine(string,object[])](https://msdn.microsoft.com/en-us/library/60scc1f1(v=vs.110).aspx), add placeholders with the format specifiers and you want and the desired culture. By concatenating strings in this way, you generate a lot of temporary strings that just waste memory *and* you are unable to specify the format you want – Panagiotis Kanavos Aug 30 '17 at 10:55
  • I already posted a link to the version of WriteLine that accepts a format string. **ALL** TextWriter classes, String.Format and StringBuilder etc have overloads that accept a format string. So do many `ToString()` methods of built-in classes, eg `someDate.ToString("d") – Panagiotis Kanavos Aug 30 '17 at 10:58
  • @PanagiotisKanavos thanks for the suggestion – Josue Figueiredo Aug 30 '17 at 10:58
  • You can use Date property of DateTime object to get only the date. – vortex Aug 30 '17 at 11:04
  • @vortex how can I use that on my code ? – Josue Figueiredo Aug 30 '17 at 11:04
  • If the data is a string you can use for example DateTime.Parse to create DateTime object from given string and then use Date property - DateTime.Parse(sqlReader["CreateDate"]). If it's DateTime already use sqlReader["CreateDate"].Date – vortex Aug 30 '17 at 11:11
  • @vortex I get an error "Cannot convert from "object" to "string" when I try to do what you have suggested – Josue Figueiredo Aug 30 '17 at 11:18
  • @Josue Figueiredo,give some example of decimal values – VIGNESH ARUNACHALAM Aug 30 '17 at 12:21
  • @VIGNESHARUNACHALAM I have a number like 12,67 I want to make it 16.67 – Josue Figueiredo Aug 30 '17 at 12:39
  • @VIGNESHARUNACHALAM I need to replace the `comma (,) ` for a `dot(.) ` – Josue Figueiredo Aug 30 '17 at 13:04

3 Answers3

1

You can use following code:

if (sqlReader["TaxableGroupID"].Equals(1))
 { 
    DateTime CreateDate = DateTime.ParseExact(sqlReader["CreateDate"],  "dd/MM/yyyy", CultureInfo.InvariantCulture);
    tw.WriteLine(sqlReader["PartyFederalTaxID"] + ";" + sqlReader["TransSerial"] + sqlReader["TransDocNumber"] + ";" + CreateDate.ToString("dd/MM/yyyy", CultureInfo.InvariantCulture); + ";" + sqlReader["TotalAmount"] + "\r\n" + sqlReader["BarCode"] + ";" + "23" + ";" + sqlReader["Quantity"]);
 }

Note: sqlReader["CreateDate"] should be a string

1

Try to do this Convert.ToDecimal(sqlReader["TotalAmount"]).ToString(CultureInfo.InvariantCulture)

Pedro Azevedo
  • 228
  • 4
  • 12
0
string s = "12,67";
decimal d = decimal.Parse(s, CultureInfo.GetCultureInfo("tr-TR"));
Console.WriteLine(d);

enter image description here