9
Answers

Binding data on instance?

Mary Anne

Mary Anne

13y
13.3k
1
I have a small 'rolodex' program that opens a form for each person in my database at runtime (each person is a row in the db). I have that part working (thanks to some help from the lovely people here!), but I have a new problem. I am no longer able to edit the information in the textboxes on the forms that have been populated by the database... After doing some research, I realize that I'm failing to bind the data in the instanced forms to its row in the database. After a week of reading and trying different examples all over the net, I'm still stuck.

My question is: How do I instance new forms (childforms) and bind each form to its proper row in the database so that I can edit the contents of the textboxes and have those changes saved in the database?

I'm using VisualStudio 2010 and .Net 4 on a Win7 64 machine.

My database is called PeopleDatabase.sdf
The table is called PeopleTable
Form1 is the 'environment' for the program - all Peoplebox appear inside of it at runtime.
Peoplebox is the form that shows the data queried from the database. It has 3 textboxes on it that need to be bound to the same row in the database. A different Peoplebox appears for each row in the database.

Form1:
[code]
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlServerCe;


namespace MaryAnne
{
    public partial class Form1 : Form
    {

        public Form1()
        {
            InitializeComponent();
        }
               
        public void Form1_Load(object sender, EventArgs e)
        {
           
            //  DB connection
            string fileName = "PeopleDatabase.sdf";
            string connectionString = string.Format("DataSource=\"{0}\";", fileName);

            //  SQL Command
            string sql = "select * from PeopleTable";

            SqlCeConnection cn = null;
            try
            {
                cn = new SqlCeConnection(connectionString);

                SqlCeCommand cmd = new SqlCeCommand(sql, cn);

                //  Checking to make sure no concurrent connections exist
                if (cn.State == ConnectionState.Open) 
                    cn.Close();

                //  Opening the connection
                cn.Open();

                SqlCeDataReader scdr = cmd.ExecuteReader();

                while (scdr.Read())
                {

                   
                    //  Opening a Peoplebox for each row in the DB
                    Peoplebox childForm = new Peoplebox();
                    childForm.MdiParent = this;
                    childForm.IdMessage = (int)scdr["id"];
                    int newIdMessage = childForm.IdMessage;
                    childForm.TitleMessage = (string)scdr["title"];
                    string newTitleMessage = childForm.TitleMessage;
                    childForm.ContentMessage = (string)scdr["content"];
                    string newContentMessage = childForm.ContentMessage;
                    childForm.Show();



                }
                scdr.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            finally
            {
                if (cn != null)
                    cn.Close();
            }
                       
        }

    }
}
[/code]


Peoplebox:
[code]
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace MaryAnne
{
    public partial class Peoplebox : Form
    {
       
        public Peoplebox()
        {
            InitializeComponent();
        }
               
        //  Defining all the variables coming from the DB
        // ID
        private int _IdMessage;

        public int IdMessage
        {
            get { return _IdMessage; }
            set { _IdMessage = value; }
        }
       
        //  TITLE
        private string _TitleMessage;

        public string TitleMessage
        {
            get { return _TitleMessage; }
            set { _TitleMessage = value; }
        }

        //  CONTENT
        private string _ContentMessage;

        public string ContentMessage
        {
            get { return _ContentMessage; }
            set { _ContentMessage = value; }
        }
       


        //  DB connection
        public void PeopleTableBindingNavigatorSaveItem_Click(object sender, EventArgs e)
        {
            this.Validate();
            this.PeopleTableBindingSource.EndEdit();
            this.tableAdapterManager.UpdateAll(this.PeopleDatabaseDataSet);

        }

        public void Peoplebox_Load(object sender, EventArgs e)
        {
            // TODO: This line of code loads data into the 'PeopleDatabaseDataSet.PeopleTable' table. You can move, or remove it, as needed.
            this.PeopleTableTableAdapter.Fill(this.PeopleDatabaseDataSet.PeopleTable);

        }
                       
        public void PeopleboxSibling_Click(object sender, EventArgs e)
        {

            Peoplebox childForm = new Peoplebox(); // Declare the child form as a new one.
            childForm.MdiParent = this.MdiParent; // Set the main form as a parent form.
            childForm.Show();// Show the child form.

        }

        public void idTextBox_TextChanged(object sender, EventArgs e)
        {
            idTextBox.Text = IdMessage.ToString();
        }

        public void titleTextBox_TextChanged(object sender, EventArgs e)
        {
            titleTextBox.Text = TitleMessage;
        }

        public void contentTextBox_TextChanged(object sender, EventArgs e)
        {
            contentTextBox.Text = ContentMessage;
        }

    }
}
[/code]

Thank you so much for reading this far! I'm looking forward to the day when I'll know enough to help others out...
-MaryAnne
Answers (9)