0

When I debug this program, I add data in through the form, and check the data through next and previous buttons. The form shows the data, but when I open the database through server explorer and check for the data, there is no data present in the database. I have done this multiple times.

Note: I have tried changing the COPY TO OUTPUT DIRECTORY property, but it doesn't help. I have also already added some data in the table.

 public partial class Form1 : Form
    {
        DatabaseConnection objConnect;
        string conString;

        DataSet ds;
        DataRow dRow;

        int MaxRows;
        int inc = 0;

        public Form1()
        {
            InitializeComponent();
        }

        private void label1_Click(object sender, EventArgs e)
        {

        }

        private void Form1_Load(object sender, EventArgs e)
        {
            try
            {
                objConnect = new DatabaseConnection();
                conString = Properties.Settings.Default.Employees1ConnectionString;

                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,"Error");
            }   
        }

        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();
        }

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

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

        private void btnaddnew_Click(object sender, EventArgs e)
        {
            txtFirstname.Clear();
            txtSurname.Clear();
            txtJobtitle.Clear();
            txtDepartment.Clear();
            btnaddnew.Enabled = false;
            btnsave.Enabled = true;
            btnCancel.Enabled = true;
        }

        private void btnsave_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 has been updated");
            }
            catch (Exception err)
            {
                MessageBox.Show(err.Message);
            }

            btnaddnew.Enabled = true;
            btnsave.Enabled = false;
            btnCancel.Enabled = false;

        }

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


    }` class DatabaseConnection
    {
        private string sql_string;
        private string strCon;
        SqlDataAdapter da_1;

        public string Sql 
        {
            set { sql_string = value; }
        }

        public string Connection_string
        {
            set { strCon = value; }
        }

        public DataSet Getconnection
        {
            get { return MyDataSet(); }
        }

        public DataSet MyDataSet()
        {
            SqlConnection con = new SqlConnection(strCon);

            con.Open();

            da_1 = new SqlDataAdapter(sql_string, con);

            DataSet dat_set = new DataSet();
            da_1.Fill(dat_set, "Table_Data_1");
            con.Close();

            return dat_set;
        }

        public void Updatedatabase(DataSet ds)
        {
            SqlCommandBuilder cb = new SqlCommandBuilder(da_1);
            cb.DataAdapter.Update(ds.Tables[0]);
        }


    }
Xynariz
  • 1,232
  • 1
  • 11
  • 28
  • Are you sure that the connection used by Server Explorer is the same connection used by your program when running? – Steve Dec 20 '13 at 09:31
  • @Steve, what do you mean? – FeliceM Dec 20 '13 at 09:33
  • I made a database in some other windows form and added it through ADD Resources option. Is that ok? – Ashutosh Bhardwaj Dec 20 '13 at 09:49
  • 1
    @FeliceM it is not uncommon to create a connection in Server Explorer that points to the database file stored in the project folder. Then in the connection string used by the program is present the AttachDbFileName with a path to a different database file. In this way the Inserts/updates/deletes all succed but when looking the database using Server Explorer, nothing appears – Steve Dec 20 '13 at 09:52
  • It shows the "Database updated" message successfully but doesnt add data – Ashutosh Bhardwaj Dec 20 '13 at 10:17
  • What happens when you perform a debug trace? You don't use try/catch around SqlCommandBuilder cb = new SqlCommandBuilder(da_1); cb.DataAdapter.Update(ds.Tables[0]); and it would be interesting to look at the code generated by the command builder. Also, the SQL Select is a variable and one can't tell just by looking at the code. I suggest you debut the above 2 lines in full. Also, make sure you close the connection before you check the rows in the db. – NoChance Dec 20 '13 at 22:09

1 Answers1

0

This might help you. You need to configure insert/update/delete with select Command.

Community
  • 1
  • 1