Example #1
0
        private void btn_AddAuthor_Click(object sender, EventArgs e)
        {
            this.Hide();
            AddAuthor form2 = new AddAuthor();

            form2.ShowDialog();
            this.Show();
        }
Example #2
0
        private void btnFindAuthor_Click(object sender, EventArgs e)
        {
            int    count;
            string firstName = txtFirstName.Text;
            string lastName  = txtLastName.Text;

            using (conn = new SqlConnection(connectionString))
                using (SqlCommand comd = new SqlCommand(
                           "select count(a.AuthorID) AS aID from LibraryDB.dbo.Author a " +
                           "where a.AuthorFirstName = @firstName and a.AuthorLastName = @lastName", conn))
                    using (SqlDataAdapter adapter = new SqlDataAdapter(comd))
                    {
                        comd.Parameters.AddWithValue("@firstName", firstName);
                        comd.Parameters.AddWithValue("@lastName", lastName);

                        DataTable AuthorTable = new DataTable();
                        adapter.Fill(AuthorTable);
                        DataRow dr = AuthorTable.Rows[0];
                        count = int.Parse(dr["aID"].ToString());
                        if (count == 0)
                        {
                            //open add author form
                            AddAuthor form3 = new AddAuthor();
                            form3.SetFirstName = firstName;
                            form3.SetLastName  = lastName;
                            form3.ShowDialog();

                            txt_AuthorID.Text = form3.ReturnNewID.ToString();
                        }
                        else if (count == 1)
                        {
                            using (SqlCommand cmd = new SqlCommand(
                                       "select AuthorID from LibraryDB.dbo.Author " +
                                       "where AuthorFirstName = @firstName and AuthorLastName = @lastName", conn))
                                using (SqlDataAdapter adapter2 = new SqlDataAdapter(cmd))
                                {
                                    cmd.Parameters.AddWithValue("@firstName", firstName);
                                    cmd.Parameters.AddWithValue("@lastName", lastName);
                                    DataTable AuthorIDtable = new DataTable();
                                    adapter2.Fill(AuthorIDtable);
                                    DataRow dr2 = AuthorIDtable.Rows[0];
                                    txt_AuthorID.Text = dr2["AuthorID"].ToString();
                                }
                        }
                        else
                        {
                            //load form to select correct author
                            AuthorSelect form_Author = new AuthorSelect();
                            form_Author.FirstName = txtFirstName.Text;
                            form_Author.LastName  = txtLastName.Text;
                            form_Author.ShowDialog();

                            selectedID        = form_Author.SelectedID;
                            txt_AuthorID.Text = selectedID.ToString();
                        }
                    }
        }