Click here to Skip to main content
15,883,768 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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 :


C#
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);
            }

        }


        }
    }
Posted
Comments
Richard C Bishop 14-Apr-14 13:34pm    
I don't see anywhere where you are performing any sort of query.
Debug your code and see where exactly is the problem.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900