I assume you know what the primary key value is for the employee being
modified? Here is an example... I don't know what data provider you are
using, so I will paste in code that can be used against any dynamically
loaded data provider. You can see this example fully implemented in the
context of the application by generating code from your database using a
free code generator I created that is on SourceForge...
http://sourceforge.net/projects/dbobjecter
and generate using "Template04".
the first thing you need to do is get a reference to the correct DataRow
object...
=============================================================
public DataRow GetByID(String title_id)
{
if (_dt==null)
{ //if you don't have the data cached in a DataSet or DataTable; Query the
DB
IDbCommand cmd = _context.DbContext.DbTemplate.Command();
cmd.Connection = _context.DbContext.Connection;
cmd.CommandText = "SELECT title_id, title, type, pub_id, price, advance,
royalty, "
+ "ytd_sales, notes, pubdate FROM titles WHERE title_id = @title_id";
IDbDataParameter prm =
_context.DbContext.DbTemplate.DataParameter("@title_id",
DbType.String);
prm.Value = title_id;
cmd.Parameters.Add(prm);
IDbDataAdapter da = _context.DbContext.DbTemplate.DataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds);
DataTable dt = ds.Tables[0];
if (dt.Rows.Count > 0 )
{
return dt.Rows[0];
}
else
{
return null;
}
}
else
{ //if you have the data cached in a DataTable use a DataView
DataView dv = new DataView();
dv.Table = _dt; //your cached DataTable containing your records.
dv.RowFilter = "title_id='" + title_id.ToString() + "'";
dv.RowStateFilter = DataViewRowState.OriginalRows;
if (dv.Count == 0)
{
return null;
}
DataRowView drv = dv[0];
return drv.Row;
}
}
=============================================================
You don't have a problem in your update as you suspect. You simply call
Update on your DataAdapter and pass in your DataSet. If you are only
modifying a current DataRow no extra code is needed. However if you are
adding a record, you must add the new DataRow to the DataTable...
===================================
System.Data.DataRow dr = Table.NewRow();
dr["title_id"] = title_id;
dr["title"] = title;
... set other row values in same manner ...
Table.Rows.Add(dr);
===================================
When you initially query the database to fill your DataSet, save that to a
variable in the form or some context object, not in the method doing the
query. That way when you need to do an update later, just use that variable
in the Update command of the DataAdapter.
It looks like you need help in the overall application design. I recommend
you take a look at other sample projects to see how they were done. Again,
take a look at the sample I provide in the open source project "Db
Object-er" on SourceForge. Run that generator against your database to see
one way you could structure your application. Use the latest template
available on the downloads page. Right now that would be #04, but I am
working on #05 now. Read the release latest news section. If you have any
trouble in generating code for your Db, post in the supplied forums.
http://sourceforge.net/projects/dbobjecter
Maybe others can supply links to other samples of good Db application
design?
Michael Lang, MCSD
MikeY said:
Hi everyone,
Using C#, Windows forms. I am trying to learn how to modify existing data
with in MSDE table/fields. In general, I have textbox filled with data from
the table "Employee". I have a update button that I want to use if I
want
to
change that persons name or any other field if needed. However this is where
I am getting lost.
If anyone could help me out with my code, I would appreciate it. Re-work it
or provide a sample for me. My code is as follows:
private void btnEmpModify_Click(object sender, System.EventArgs e)
{
DataTable thisTable = dsModifyRecord.Tables["Employee"];
//THIS IS WHERE I'M GETTING LOST
DataRow thisRow = thisTable???????????????
thisRow["EmplNumber"] = txtBoxEmplNumber.Text;
thisRow["EmpFName"] = txtBoxEmpFName.Text;
thisRow["EmpLName"] = txtBoxLNName.Text;
thisRow["DateModified"] = DateTime.Now.ToString();
thisRow["EmployeeNotes"] = txtBoxEmpNotes.Text;
//& GETTING LOST HERE
dsModifyRecord.Tables["EMPLOYEE"]??????????????
sqlDataAdapter1.Update(dsModifyRecord, "Employee");
}
Thank you all in advance.
MikeY