且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

记录不会添加到我的表中

更新时间:2023-11-30 23:42:16

When I am trying to run this code written in :
http://www.homeandlearn.co.uk/csharp/csharp_s12p10.html
I find that records are added to the screen display are not added to the table .
Here is the code :


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace _46_creating_databse_01_sql_express_02
{
    public partial class Form1 : Form
    {
        public Form1() { InitializeComponent(); }
        
            
            DatabaseConnection objConnect;
            string conString;
            DataSet ds;
            DataRow dRow;
            int MaxRows;
            int inc = 0;
        
        private void Form1_Load_1(object sender, EventArgs e)
        {
            try
            {
                objConnect = new DatabaseConnection();
                conString = Properties.Settings.Default.employeesConnectionString;
                
                objConnect.connection_string = conString;
                objConnect.Sql = Properties.Settings.Default.sql;
                
                ds = objConnect.GetConnection;
                MaxRows = ds.Tables[0].Rows.Count;
                
                NavigateRecords();

            }
            catch (Exception err)
            {
                MessageBox.Show(err.Message);
            }

        }
        private void NavigateRecords()
        {
            dRow = ds.Tables[0].Rows[inc];
            txtFirstName.Text = dRow.ItemArray.GetValue(1).ToString();
            txtSurname.Text = dRow.ItemArray.GetValue(2).ToString();
            txtJobTitle.Text = dRow.ItemArray.GetValue(3).ToString();
            txtDepartment.Text = dRow.ItemArray.GetValue(4).ToString();
            labelUpdate();
        }

        private void labelUpdate()
        {

            label6.Text = "Record " + (inc + 1) + " of " + MaxRows;

        }

        private void button2_Click(object sender, EventArgs e)
        {
            if (inc != MaxRows - 1)
            {
                inc++;
                NavigateRecords();
            }
            else
            {
                MessageBox.Show("No More Rows");
            }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            if (inc > 0)
            {
                inc--;
                NavigateRecords();
            }
            else
            {
                MessageBox.Show("first Record");
            }
        }

        private void button4_Click(object sender, EventArgs e)
        {
            if (inc != 0)
            {
                inc = 0;
                NavigateRecords();
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (inc != MaxRows - 1)
            {
                inc = MaxRows - 1;
                NavigateRecords();
            }
        }

        private void button6_Click(object sender, EventArgs e)
        {
            txtFirstName.Clear();
            txtSurname.Clear();
            txtJobTitle.Clear();
            txtDepartment.Clear();

            btnAddNew.Enabled = false;
            btnSave.Enabled = true;
            btnCancel.Enabled = true;
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            NavigateRecords();
            btnCancel.Enabled = false;
            btnSave.Enabled = false;
            btnAddNew.Enabled = true;
        }

        private void button7_Click(object sender, EventArgs e)
        {
            DataRow row = ds.Tables[0].NewRow();
            row[1] = txtFirstName.Text;
            row[2] = txtSurname.Text;
            row[3] = txtJobTitle.Text;
            row[4] = txtDepartment.Text;

            ds.Tables[0].Rows.Add(row);
            try
            {
                objConnect.UpdateDatabase(ds);
                MaxRows = MaxRows + 1;
                inc = MaxRows - 1;
                MessageBox.Show("database updated");
            }
            catch (Exception err)
            {
                MessageBox.Show(err.Message);
            }
            btnCancel.Enabled = false;
            btnSave.Enabled = false;
            btnAddNew.Enabled = true;



        }

        private void button9_Click(object sender, EventArgs e)
        {
            DataRow row = ds.Tables[0].Rows[inc];
            
            row[1] = txtFirstName.Text;
            row[2] = txtSurname.Text;
            row[3] = txtJobTitle.Text;
            row[4] = txtDepartment.Text;
            try
            {
                objConnect.UpdateDatabase(ds);
                MessageBox.Show("Record Updated");
            }
            catch (Exception err)
            {
                MessageBox.Show(err.Message);
            }

        }


        }
    }