How to export CSV file with specific name?

How to export csv file with specific name?

  • I want to export csv file from the database by specific names, such as the data of patient contains "Ward name" field as "GENERAL WARD (FEMALE)" then the gererated file name must be "GENF.csv ". I tried with switch-case but it is not working. If there are any changes please edit. Thanks. Here is the code: //Function declares methods to export public void ExportAll() { try { int i = 0; for (int icount = 0; icount <= 400; icount++) { object o = saiNathHospitalDataSet.Tables["PatientTable"].Rows[i]["WARD NAME"]; object obj = saiNathHospitalDataSet.Tables["PatientTable"].Rows[i]["PTNAME"]; int swichexpression = 9; object a = "GENERAL WARD (FEMALE)"; object b = "GENERAL WARD (MALE)"; object c = "RECOVERY"; object d = "SEMI DELUXE 02"; object e = "SEMI DELUXE 05"; object f = "SEMI DELUXE 06"; object g = "ICU"; object h = "SEMI SPECIAL 03"; object j = "SEMI SPECIAL 01"; switch (swichexpression) { case 1: if (o == a) { // o = "GENF"; DataExport("select * from PatientTable where [WARD NAME] =" + o + "", "GENF.csv"); } break; case 2: if (o == b) { // o = "GENM"; DataExport("select * from PatientTable where [WARD NAME] =" + o + "", "GENM.csv"); } break; case 3: if (o == c) { //o = "REC"; DataExport("select * from PatientTable where [WARD NAME] =" + o + "", "REC.csv"); } break; case 4: if (o == d) { // o = "SDELX02"; DataExport("select * from PatientTable where [WARD NAME] =" + o + "", "SDELX02.csv"); } break; case 5: if (o == e) { // o = "SDELX05"; DataExport("select * from PatientTable where [WARD NAME] =" + o + "", "SDELX05.csv"); } break; case 6: if (o == f) { // o = "SDELX06"; DataExport("select * from PatientTable where [WARD NAME] =" + o + "", "SDELX06.csv"); } break; case 7: if (o == g) { //o = "ICU"; DataExport("select * from PatientTable where [WARD NAME] =" + o + "", "ICU.csv"); } break; case 8: if (o == h) { //o = "SSPEC03"; DataExport("select * from PatientTable where [WARD NAME] =" + o + "", "SSPEC03.csv"); } break; case 9: if (o == j) { //string z = "SSPEC01"; DataExport("select * from PatientTable where [WARD NAME] =" + o + "", "SSPEC01.csv"); } break; } //DataExport("select * from PatientTable", "" + s + ".csv"); i++; } } catch { } } Edited::Data Export Method, public void DataExport(string SelectQuery, string fileName) { try { using (var dt = new DataTable()) { using (var da = new SqlDataAdapter(SelectQuery, con)) { da.Fill(dt); var header = String.Join( ",", dt.Columns.Cast<DataColumn>().Select(dc => dc.ColumnName)); var rows = from dr in dt.Rows.Cast<DataRow>() select String.Join( ",", from dc in dt.Columns.Cast<DataColumn>() let t1 = Convert.IsDBNull(dr[dc]) ? "" : dr[dc].ToString() let t2 = t1.Contains(",") ? String.Format("\"{0}\"", t1) : t1 select t2); using (var sw = new StreamWriter(txtreceive.Text + "\\" + fileName)) { sw.WriteLine(header); foreach (var row in rows) { sw.WriteLine(row); } sw.Close(); } } } } catch { } }

  • Answer:

    Your switch case is incorrect, right now you're making a mix of switch case and if statements, but your switch case will always go to case 9 and you don't actually need a switch case on integers. You need to change it to something like this: string o = saiNathHospitalDataSet.Tables["PatientTable"].Rows[i]["WARD NAME"]; const string a = "GENERAL WARD (FEMALE)"; const string b = "GENERAL WARD (MALE)"; const string c = "RECOVERY"; const string d = "SEMI DELUXE 02"; const string e = "SEMI DELUXE 05"; const string f = "SEMI DELUXE 06"; const string g = "ICU"; const string h = "SEMI SPECIAL 03"; const string j = "SEMI SPECIAL 01"; switch (o) { case (a): //Do something break; case (b): //Do something break; default: //Do something break; } Edit: If you want to export a file per patient for each of your wards, you will have to change your foreach loop of your export method to something like this: foreach (var row in rows) { using (var sw = new StreamWriter(txtreceive.Text + "\\" + "Some variable to identify different patients" + filename)) { sw.WriteLine(header); sw.WriteLine(row); } } Assuming each row represents a patient, you need to create a file per row.

Member123 at Stack Overflow Visit the source

Was this solution helpful to you?

Just Added Q & A:

Find solution

For every problem there is a solution! Proved by Solucija.

  • Got an issue and looking for advice?

  • Ask Solucija to search every corner of the Web for help.

  • Get workable solutions and helpful tips in a moment.

Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.