0

I'm having a bit of trouble understanding DataAdapters and I was hoping for a quick bit of help. At runtime, I'd like Form1 to open a DisplayForm for each row in the database and have each of those DisplayForms be bound to the database and fully editable (there are 3 text fields in each DisplayForm corresponding to the 3 columns in the table). I understand enough to fill the text fields via the DataReader method - but this doesn't let me edit those fields at runtime. I've read a lot of documentation on the DataAdapters, but I just don't have the experience needed to understand it. Would you mind taking pity on me and showing me how it's done? Or explaining the concept/syntax as if you were talking to a very very stupid rock?

namespace DataBindTest
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        public void Form1_Load(object sender, EventArgs e)
        {
            //  DB connection
            string fileName = "MyDatabase.sdf";
            string connectionString = string.Format("DataSource=\"{0}\";", fileName);

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

            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 rd = cmd.ExecuteReader();

                while (rd.Read())
                {
                    //  Opening a DisplayForm for each row in the DB
                    DisplayForm childForm = new DisplayForm();
                    childForm.MdiParent = this;
                    childForm.IdMessage = (int)rd["id"];
                    int newIdMessage = childForm.IdMessage;
                    childForm.TitleMessage = (string)rd["title"];
                    string newTitleMessage = childForm.TitleMessage;
                    childForm.ContentMessage = (string)rd["content"];
                    string newContentMessage = childForm.ContentMessage;
                    childForm.Show();
                }
                rd.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            finally
            {
                if (cn != null)
                    cn.Close();
            }
        }
    }
}

Thanks for taking pity on a very new and very thick programmer.
-Charlie

2
Contributors
1
Reply
2
Views
6 Years
Discussion Span
Last Post by Mitja Bonca
This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.