Exemplo n.º 1
0
        private void button1_Click(object sender, EventArgs e)
        {
            AddOrUpdateForm mainForm = new AddOrUpdateForm();

            if (mainForm.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                List <Member> listOfMembers = new List <Member>();

                SqlConnection sqlConn = new SqlConnection(@"Server=.\SQLEXPRESS;Database=CDS;Integrated Security=true");
                const string  SQL_LOAD_ALL_MEMBERS = @"SELECT CardNumber, ScreenName, StartDate, EndDate
                                                  FROM memberstable
                                                  ORDER BY FirstName, LastName";

                using (SqlCommand cmd = new SqlCommand(SQL_LOAD_ALL_MEMBERS, sqlConn))
                {
                    sqlConn.Open();

                    using (SqlDataReader reader = cmd.ExecuteReader())
                    {
                        if (reader.HasRows)
                        {
                            while (reader.Read())
                            {
                                Member member = new Member();
                                member.Broj_kartice = Int32.Parse((reader["CardNumber"].ToString()));
                                member.lan          = reader["ScreenName"].ToString();
                                member.Datum_uplate = DateTime.Parse(reader["StartDate"].ToString()).ToString("dddd, dd MMMM yyyy");
                                member.Datum_isteka = DateTime.Parse(reader["EndDate"].ToString()).ToString("dddd, dd MMMM yyyy");

                                listOfMembers.Add(member);
                            }
                        }
                    }
                }

                dataGridView1.DataSource = listOfMembers;
            }
        }
Exemplo n.º 2
0
        private void button4_Click(object sender, EventArgs e)
        {
            if (dataGridView1.SelectedCells.Count > 0)
            {
                int             selectedrowindex = dataGridView1.SelectedCells[0].RowIndex;
                DataGridViewRow selectedRow      = dataGridView1.Rows[selectedrowindex];

                var cardNumber = selectedRow.Cells[0].Value;

                SqlConnection sqlConn = new SqlConnection(@"Server=.\SQLEXPRESS;Database=CDS;Integrated Security=true");

                const string SQL_LOAD_MEMBER = @"SELECT FirstName, LastName, CardNumber, ScreenName, StartDate, EndDate
                                                  FROM memberstable
                                                  WHERE CardNumber = @CardNumber";

                using (SqlCommand cmd = new SqlCommand(SQL_LOAD_MEMBER, sqlConn))
                {
                    RealMember member = new RealMember();

                    sqlConn.Open();

                    cmd.Parameters.Add("@CardNumber", SqlDbType.Int).Value = cardNumber;

                    using (SqlDataReader reader = cmd.ExecuteReader())
                    {
                        if (reader.Read())
                        {
                            member            = new RealMember();
                            member.CardNumber = Int32.Parse((reader["CardNumber"].ToString()));
                            member.FirstName  = reader["FirstName"].ToString();
                            member.LastName   = reader["LastName"].ToString();
                            member.ScreenName = reader["ScreenName"].ToString();
                            member.StartDate  = DateTime.Parse(reader["StartDate"].ToString());
                            member.EndDate    = DateTime.Parse(reader["EndDate"].ToString());
                        }
                    }

                    AddOrUpdateForm form = new AddOrUpdateForm(member);
                    if (form.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                    {
                        List <Member> listOfMembers = new List <Member>();

                        const string SQL_LOAD_ALL_MEMBERS = @"SELECT CardNumber, ScreenName, StartDate, EndDate
                                                  FROM memberstable
                                                  ORDER BY FirstName, LastName";

                        cmd.CommandText = SQL_LOAD_ALL_MEMBERS;

                        using (SqlDataReader reader = cmd.ExecuteReader())
                        {
                            if (reader.HasRows)
                            {
                                while (reader.Read())
                                {
                                    Member updatedMember = new Member();
                                    updatedMember.Broj_kartice = Int32.Parse((reader["CardNumber"].ToString()));
                                    updatedMember.lan          = reader["ScreenName"].ToString();
                                    updatedMember.Datum_uplate = DateTime.Parse(reader["StartDate"].ToString()).ToString("dddd, dd MMMM yyyy");
                                    updatedMember.Datum_isteka = DateTime.Parse(reader["EndDate"].ToString()).ToString("dddd, dd MMMM yyyy");

                                    listOfMembers.Add(updatedMember);
                                }
                            }
                        }

                        dataGridView1.DataSource = listOfMembers;
                    }
                }
            }
        }