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
Loading
Charlie HoltPosted Nov 21, 2010, 6:47 AM
The IDE does a great job of setting up data binding, but this breaks down when you're using multiple instances of the same form to display different records rather than using the built-in controls to cycle through multiple records on a single form (ie: DataGridView).
I need a bit of help with setting up a custom DataAdapter to help instance multiple forms, not with getting DataGridView to work on a single form. I thank you again for bumping this thread and attempting to help, but it's obvious you cannot lend aid to this situation.
-Charlie
Sam HobbsPosted Nov 21, 2010, 5:52 AM
Charlie HoltPosted Nov 21, 2010, 5:11 AM
I am using a reader in lieu of an adapter. My question was: how do I replace the reader with an adapter so that the textboxes will be editable?
-Charlie
Sam HobbsPosted Nov 21, 2010, 4:01 AM
I don't see the use of a DataAdapter anywhere in your source code. You use a SqlCeDataReader but that does not update the table.
For each data adapter there is a DataTable. The DataTable can be filled using the Fill method of the data adapter. A DataTable has a Rows collection. I think you can pass a DataRow object to each of the DisplayForms; the row that is shown by the form. Then the form can get the data from the row and put the data back in the row when there are changes.
Or you can read my article Database Table Update in a DataGridView without Writing Code and everything will be done for you.