Click here to Skip to main content
15,884,020 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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 not working. if there is any changes please edit.

thanks.


**here is the code sniped**

C#
public void ExportAll()//Function declares methods to export//
 {
   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 {}
    }


**here is the data export method,**

C#
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 { }
     }
Posted
Updated 2-Jun-15 0:28am
v2
Comments
Maciej Los 2-Jun-15 6:56am    
Why string data are declared and initialized as Object: Object a = "GENERAL WARD (FEMALE)";?
[no name] 2-Jun-15 7:24am    
If "not working" means that your switch isn't working as you think it should, it's because your switch expression is always 9 and you never change it.
Richard MacCutchan 2-Jun-15 8:37am    
Why do you need the switch? You can easily make the name by extracting the specific parts of the ward name each time. And all that duplicated code is a recipe for disaster if anything changes in the future.
gggustafson 2-Jun-15 14:57pm    
Remove obj - it's never used; switchexpression is never changed - you execute case 9 repeated; i, icount, and switchexpression are causing confusion - reduce the number of variables; if you must define a - j, define them outside the loop; don't switch on switchexpression, you are forcing yourself into the ugly if(o==?) - rethink the switch totally.

Aren't you missing .ToArray() in your Join statement?

1 solution

Don't you want to do something more like this?
And one more advice don't '+' the string for sql queries. Use parameters ;).


C#
for (int icount = 0; icount <= 400; icount++)
{
string wardName = saiNathHospitalDataSet.Tables["PatientTable"].Rows[i]["WARD NAME"].ToString();

    switch (wardName)
    {
        case "GENERAL WARD (FEMALE)" :
            DataExport("select * from PatientTable where [WARD NAME] =" + o + "", "GENF.csv");
        break;

        case "GENERAL WARD (MALE)" :
               DataExport("select * from PatientTable where [WARD NAME] =" + o + "", "GENM.csv");
        break;

        etc.......
    }
    catch {}
}
 
Share this answer
 
Comments
Member 11543226 3-Jun-15 1:42am    
Not working sir, wardname gets one value and checked in cases but but if in case matches the values it does not creates file. switch case doesn't work
xszaboj 4-Jun-15 3:52am    
switch is not working or method: public void DataExport(string SelectQuery, string fileName) is not working then? can you post the code you actually have?

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