Charlie Holt

Charlie Holt

  • NA
  • 12
  • 49.7k

I need a quick bit of help with DataAdapters...

Nov 21 2010 1:32 AM
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?

[code]
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();
            }
        }
    }
}
[/code]

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

Answers (4)