0

I'm new to methods and classes and knowing when and where to use them, i'm currently going through some tutorials at the moment.

I have created a method below getInfo()

 public partial class MainWindow : Window
    {

            DataTable dtNotes = new DataTable();
            DataTable dtTemplateNotes = new DataTable();
            DataTable dtReplaceVariables = new DataTable();
            public void getInfo()
            {
                MessageBox.Show("Hello");
                //Setup connection to server
                SqlConnectionStringBuilder builder = new SqlConnectionStringBuilder();
                builder.DataSource = "12345678";
                builder.InitialCatalog = "DiscoverThePlanet";
                builder.UserID = "User";
                builder.Password = "Pass";

                string connectionString = builder.ConnectionString;

                using (SqlConnection conn = new SqlConnection(connectionString))
                {
                    conn.Open();

                    SqlCommand cmdNotes = new SqlCommand("SELECT NoteID, NoteName, Note FROM Notes", conn);
                    SqlCommand cmdTemplateNotes = new SqlCommand("SELECT TemplateNoteID, TemplateNoteName, TemplateNote FROM TemplateNotes", conn);
                    SqlCommand cmdReplaceVariables = new SqlCommand("SELECT ReplaceVariableID, ReplaceVariableName, ReplaceVariableNote FROM ReplaceVariables", conn);

                    SqlDataReader readerNotes = cmdNotes.ExecuteReader();

                    dtNotes.Columns.Add("NoteID", typeof(string));
                    dtNotes.Columns.Add("NoteName", typeof(string));
                    dtNotes.Columns.Add("Note", typeof(string));
                    dtNotes.Load(readerNotes);

                    SqlDataReader readerTemplateNotes = cmdTemplateNotes.ExecuteReader();

                    dtTemplateNotes.Columns.Add("TemplateNoteID", typeof(string));
                    dtTemplateNotes.Columns.Add("TemplateNoteName", typeof(string));
                    dtTemplateNotes.Columns.Add("TemplateNote", typeof(string));
                    dtTemplateNotes.Load(readerTemplateNotes);

                    SqlDataReader readerReplaceVariables = cmdReplaceVariables.ExecuteReader();

                    dtReplaceVariables.Columns.Add("ReplaceVariableID", typeof(string));
                    dtReplaceVariables.Columns.Add("ReplaceVariableName", typeof(string));
                    dtReplaceVariables.Columns.Add("ReplaceVariableNote", typeof(string));
                    dtReplaceVariables.Load(readerReplaceVariables);

                    // Temporary loop to see if the DataTable (dt) has any data?!?
                    //foreach (DataRow thisRow in dt.Rows)
                    //{
                    //    MessageBox.Show(thisRow["NoteName"].ToString());
                    //}


                    // Define the columns BEFORE setting the item source
                    noteNamesList.SelectedValuePath = "NoteID";
                    noteNamesList.DisplayMemberPath = "NoteName";

                    templateNoteNamesList.SelectedValuePath = "TemplateNoteID";
                    templateNoteNamesList.DisplayMemberPath = "TemplateNoteName";

                    replaceVariableNoteList.SelectedValuePath = "ReplaceVariableID";
                    replaceVariableNoteList.DisplayMemberPath = "ReplaceVariableName";


                    // Set the ItemSource to my fully loaded data table!
                    noteNamesList.ItemsSource = dtNotes.DefaultView;
                    templateNoteNamesList.ItemsSource = dtTemplateNotes.DefaultView;
                    replaceVariableNoteList.ItemsSource = dtReplaceVariables.DefaultView;

                    //DEBUG START
                    //MessageBox.Show("Hello");
                    //DEBUG END

                    conn.Close();

                }
            }

As you can see its inside the MainWindow class.

Firstly how do i run this method so it populates my combo boxes and then once i have updated a table by typing in a text box and submitting the result back to the database refresh the combo boxes by using the same method to update the data tables

I did have this not in a method so as soon as the window opens it populates the combo boxes, but someone said to move the code into a method so that i could call that method to refresh my combo boxes

I would like to just click a button and it runs that method

This is what i have for submitting an edited note

private void editNote_Click(object sender, RoutedEventArgs e)
        {

            if (string.IsNullOrEmpty(textResult.Text))
                {
                    submitNote.IsEnabled = false;
                    MessageBox.Show("No Text to save");
                    submitNote.IsEnabled = true;
                }

                else if (noteSaveToSelection.SelectedItem == null)
                {
                    MessageBox.Show("Please select the table the note is from!");
                }

                else
                {
                    if(noteSaveToSelection.Text == "Notes")
                    {
                        noteName.Text = noteNamesList.SelectedValue.ToString();
                    }
                    else if(noteSaveToSelection.Text == "Template Notes")
                    {
                        noteName.Text = templateNoteNamesList.SelectedItem.ToString();
                    }
                    else if(noteSaveToSelection.Text == "Replace Variables")
                    {
                        noteName.Text = replaceVariableNoteList.SelectedItem.ToString();
                    }
                    MessageBoxResult editConfirm = MessageBox.Show("Are you sure you want to submit the edited note '" + noteName.Text + "' to the database?", "Wait!", MessageBoxButton.YesNo);

                    if (editConfirm == MessageBoxResult.Yes)
                    {
                        SqlConnectionStringBuilder builder = new SqlConnectionStringBuilder();
                        builder.DataSource = "12345678";
                        builder.InitialCatalog = "DiscoverThePlanet";
                        builder.UserID = "user";
                        builder.Password = "pass";
                        string connectionString = builder.ConnectionString;

                        if (noteSaveToSelection.Text == "Notes")
                        {
                            using (SqlConnection conn = new SqlConnection(connectionString))
                            {
                                int id = Convert.ToInt32(noteNamesList.SelectedValue);
                                SqlCommand cmd = new SqlCommand("UPDATE Notes SET Note = @Note WHERE NoteID = @NoteID");
                                cmd.CommandType = CommandType.Text;
                                cmd.Connection = conn;
                                cmd.Parameters.AddWithValue("@NoteID", id); //FIND THE NOTE ID OF NOTE CURRENTLY SELECTED IN DROPDOWN
                                cmd.Parameters.AddWithValue("@NoteName", noteName.Text);
                                cmd.Parameters.AddWithValue("@Note", textResult.Text);
                                conn.Open();
                                cmd.ExecuteNonQuery();
                                MessageBox.Show("Your note has been saved to Notes!");
                                conn.Close();
                                noteName.Text = String.Empty;
                            }
                        }
Luke Litherland
  • 217
  • 4
  • 16
  • This code is not the right way to do things in WPF. You should learn about MVVM. [Here are some possible starting points](http://stackoverflow.com/a/2034333/424129). The essence of it is that you put your collections and properties on a viewmodel, which has no UI. Then you write some UI -- a view -- which uses "bindings" to associate various controls with properties of the viewmodel. – 15ee8f99-57ff-4f92-890c-b56153 Sep 19 '16 at 16:50
  • 1
    @Luke, I don't see a real question here – ASh Sep 19 '16 at 17:29

0 Answers0