/*--------------------------------------------------------------------------------------------------------------------------------------------------
Program Listing for:  MyData.cs
Project:  xml
Namespace:  cs
----------------------------------------------------------------------------------------------------------------------------------------------------
*/

using System;
using System.Data;
using System.Windows.Forms;

namespace HODecisionEntry
{
         /// <summary>
         /// Summary description for MyData.
         /// </summary>
         public class MyData
         {

                  public MyData()
                  {
                  }

                  // this is a straight forward select wrapper
                  // select method behavior:
                  // if no dataservice, return null
                  // if bad command syntax, return null
                  // otherwise return table
                  public static DataTable Select( string selectString) 
                  {
                           DataService ds1 = new DataService();
                           DataTable mytable = null;

                           if (ds1 == null) 
                           {
                                    MessageBox.Show("Error: error connecting to remote data service, are you online? Retry and notify programmer.");
                                    return null;
                           }

                           DataSet mydata = ds1.GetDataSetSelect( selectString, "daffy" );

                           if (mydata == null) 
                           {
                                    MessageBox.Show("Error: possibly bad select string:\r" + selectString + "\rNo data returned from select");
                           } 
                           else {
                                    mytable = mydata.Tables[0];
                           }
                           return mytable;

                  }

                  // this is a straight forward select wrapper for select which returns a single value
                  // method similar to select method, but returns a string rather than datatable, if successful
                  public static string SelectSingleVal( string selectString) 
                  {
                           DataService ds1 = new DataService();
                           DataTable mytable = null;
                           string returnString = null;

                           if (ds1 == null) 
                           {
                                    MessageBox.Show("Error: error connecting to remote data service, are you online? Retry and notify programmer.");
                                    return null;
                           }

                           DataSet mydata = ds1.GetDataSetSelect( selectString, "daffy" );
                           if (mydata == null) 
                           {
                                    MessageBox.Show("Error: possibly bad select string:\r" + selectString + "\rNo data returned from select");
                           } 
                           else
                           {
                                    mytable = mydata.Tables[0];

                                    // successful select but returned no rows, return an empty string -- for now
                                    if (mytable.Rows.Count == 0) 
                                    {
                                             returnString = "";
                                    } 
                                    // otherwise process the returned row, from the first element only, get the value as string
                                    else 
                                    {
                                             DataRow curRow = mytable.Rows[0];
                                             returnString = curRow[0].ToString();
                                    }

                           }
                           return returnString;

                  }

                  // this is a straight forward update, insert, delete command wrapper
                  // if no dataservice, returns -1
                  // if bad command syntax, returns -1
                  // otherwise returns rows affected (0-x)
                  public static int Update(string updateString) 
                  {

                           DataService ds1 = new DataService();

                           if (ds1 == null) 
                           {
                                    MessageBox.Show("Error: error connecting to remote data service, are you online? Retry and notify programmer.");
                                    return -1;
                           }

                           int rows = ds1.ExecuteCommand(updateString, "scooby");

                           if (rows == -1) 
                           {
                                    MessageBox.Show("Some error occurred, database not updated correctly.");
                                    MessageBox.Show( "Service reported this error:\r" + ds1.GetLastError() );
                                    ds1.ClearLastError();

                           }
                           return rows;

                  }


         }

}