Esempio n. 1
0
    protected void Page_Load(object sender, EventArgs e)
    {
        // Declare and instantiate the AppointmentRetriever.cs class to get the linq queries inside
        apps = new LinqDataManipulator();

        try
        {
            notCheckedInAppointments = apps.GetNotCheckedInAppointments();
            checkedInAppointments    = apps.GetCheckedInAppointments();

            // Make all appointments in the Database Todays date to test the webapp
            apps.MakeAllAppointmentsTodaysDate();

            // Make the appointments IQueryable object
            // the GridView's DataSource and then bind it.
            AppointmentsGridView.DataSource = notCheckedInAppointments;
            CheckedInGridView.DataSource    = checkedInAppointments;

            AppointmentsGridView.DataBind();
            CheckedInGridView.DataBind();
        }
        catch (Exception)
        {
            throw;
        }
    }
        /**
         * This method will now allow me to vary what the gridview will show depending on some SQL command
         * I put an optional parameter SqlParam parameters so that I can specify what type of parameter might be in the SQL command string
         * I also put an optional indefinite parameters params string[] parameterValues so that I can send in values for parameters
         * The order of those parameter values should match the switch-case statements below
         */
        private void BindAppointments(string sqlCommandString = defaultSqlCommand, SqlParam parameters = SqlParam.NONE, params string[] parameterValues)
        {
            DataTable appointmentTable = new DataTable("Appointments");

            using (SqlConnection sqlConnection = new SqlConnection(GetConnectionString()))
            {
                try
                {
                    //sqlConnection.Open();
                    SqlCommand     sqlCommand      = new SqlCommand(sqlCommandString, sqlConnection);
                    SqlDataAdapter gridViewAdapter = new SqlDataAdapter(sqlCommandString, sqlCommand.Connection);
                    DataSet        dataSet         = new DataSet();
                    if (sqlCommand.Parameters.Count > 0)
                    {
                        switch (parameters)
                        {
                        case SqlParam.ADVISORID:
                            sqlCommand.Parameters.Add(new SqlParameter("@AdvisorID", parameterValues[0]));
                            break;

                        case SqlParam.STUDENTNAME:
                            sqlCommand.Parameters.Add(new SqlParameter("@StudentName", parameterValues[0]));
                            break;
                        }
                    }


                    gridViewAdapter.SelectCommand = sqlCommand;
                    sqlCommand.Connection.Open();
                    gridViewAdapter.Fill(dataSet);


                    if (appointmentTable.Rows.Count > 0)
                    {
                        AppointmentsGridView.DataSource = appointmentTable;
                        AppointmentsGridView.DataBind();

                        DebugTextBox.Text = "Success! " + appointmentTable.Rows.Count.ToString();
                        //Console.WriteLine();
                        //Console.Write("Number of Rows in Table: {0}", appointmentTable.Rows.Count);
                    }
                    else
                    {
                        DebugTextBox.Text = "Failed: " + appointmentTable.Rows.Count.ToString();
                        //Console.WriteLine();
                        //Console.Write("Number of Rows in Table: {0}", appointmentTable.Rows.Count);
                    }
                } catch (SqlException exception)
                {
                    throw new Exception(exception.Message);
                }
                finally
                {
                    sqlConnection.Close();
                }
            }

            LoadAppointmentsToWeek();
        }