public void parseResourceDatabaseData(ResourceMenu form, bool dateFilled, string resourceType, string resourceName, string resourceRole, string availableDate = "", string unavailableDate = "", string unavailableDateEnd = "")
        {
            rdata = new ResourceData(resourceType, resourceName, resourceRole);
            form.resources.Add(rdata);
            // Create database connection to parse data
            using (SqlConnection openConnection = new SqlConnection(@"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=C:\Users\Tobias\source\repos\FUN12 Project\Killerapp-FUN12\ProjectManager\ProjectManagerData.mdf;Integrated Security=True"))
            {
                string saveQuery = "INSERT into Resources (Resource,Name,Role,AvailableDate,UnavailableDate,UnavailableDateEnd) VALUES (@Resource,@Name,@Role,@AvailableDate,@UnavailableDate,@UnavailableDateEnd)";

                using (SqlCommand cmd = new SqlCommand(saveQuery, openConnection))
                {
                    cmd.Connection = openConnection;
                    cmd.Parameters.Add("@Resource", SqlDbType.Text).Value = resourceType;
                    cmd.Parameters.Add("@Name", SqlDbType.Text).Value     = resourceName;
                    cmd.Parameters.Add("@Role", SqlDbType.Text).Value     = resourceRole;

                    // If date has been filled, then parse it
                    if (dateFilled)
                    {
                        cmd.Parameters.Add("@AvailableDate", SqlDbType.Date).Value      = availableDate;
                        cmd.Parameters.Add("@UnavailableDate", SqlDbType.Date).Value    = unavailableDate;
                        cmd.Parameters.Add("@UnavailableDateEnd", SqlDbType.Date).Value = unavailableDateEnd;
                    }
                    else
                    {
                        cmd.Parameters.Add("@AvailableDate", SqlDbType.Date).Value      = DBNull.Value;
                        cmd.Parameters.Add("@UnavailableDate", SqlDbType.Date).Value    = DBNull.Value;
                        cmd.Parameters.Add("@UnavailableDateEnd", SqlDbType.Date).Value = DBNull.Value;
                    }

                    // Error handle the connection
                    try
                    {
                        openConnection.Open();
                        int recordsAdded = cmd.ExecuteNonQuery();
                        MessageBox.Show(recordsAdded + " rows have been touched!");
                    }
                    catch (SqlException ex)
                    {
                        MessageBox.Show("Something went wrong with an SQL action....", "SQL-related exception");
                        MessageBox.Show(ex.ToString());
                    }
                    // Catch any other exception other then sql
                    catch (Exception ex)
                    {
                        MessageBox.Show("Something went wrong....", "General exception");
                        MessageBox.Show(ex.ToString());
                    }
                    finally
                    {
                        openConnection.Close();
                    }
                }
            }
            // Close the window succesfully
            form.clearFormState();
            form.Hide();
        }
Beispiel #2
0
 public Main()
 {
     InitializeComponent();
     createResource        = new ResourceMenu();
     createTask            = new TaskMenu();
     formUtilities         = new FormUtilities();
     createdEventsOverview = new List <Event>();
     calendarFunctions     = new GoogleCalendarSync();
 }