Tuesday, 15 January 2013

How to return array from function in c#

This below example shows how to return an array from function in C# :


   public string[] PatientChatList()
   {
       //Method returns patient's list in Array form

       String sessionId;
       String patientName;
       con.Open();
       String queryChatList = "select patientName,sessionId from tmpChatTable";
       SqlCommand comm = new SqlCommand(queryChatList, con);
       SqlDataAdapter da = new SqlDataAdapter(comm);
       DataTable dt = new DataTable();
       da.Fill(dt);
       int no = dt.Rows.Count;
       //MessageBox.Show(no.ToString());
       String[] PatientList = new String[no];
       for (int i = 0; i < dt.Rows.Count; i++)
       {
           sessionId = dt.Rows[i]["sessionId"].ToString();
           patientName = dt.Rows[i]["patientName"].ToString();
           PatientList[i] = sessionId + " " + patientName;
       }
       da.Dispose();
       con.Close();
       return PatientList;
   }

0 comments:

Post a Comment


                                                            
 
Design by Abhinav Ranjan Sinha