Problems with updating data to access through gridview in C#
-
Mittwoch, 15. August 2012 14:17
Hello everyone,
I got some difficulties with getting my code to work.
1) I get the data from a mdb file in my datagridview.
2) now I want to change records or add records and update it to my mdb file.
this is the code I wrote:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
OleDbConnection con = new OleDbConnection();
DataSet ds = new DataSet();
DataTable dt = new DataTable();
OleDbDataAdapter da = new OleDbDataAdapter();
private void Form1_Load(object sender, EventArgs e)
{
con.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\\Users\\Latop-Sven\\Desktop\\dbExamen.mdb";
con.Open();
ds.Tables.Add(dt);
da = new OleDbDataAdapter("Select * from tblStudent",con);
da.Fill(dt);
dataGridView1.DataSource = dt.DefaultView;
con.Close();
}
private void btnsave_Click(object sender, EventArgs e)
{
con.Open();
da.Update(ds, "tblStudent");
con.Close();
}
}the error that I get at the moment is: Update unable to find TableMapping['tblStudent'] or DataTable 'tblStudent'.
Can someone help me with this?
thanks
Alle Antworten
-
Mittwoch, 15. August 2012 18:10
I would suspect that when you call the Fill method you will need to specify the name of the DataTable. It doesn't look like you do that in your code.Paul ~~~~ Microsoft MVP (Visual Basic)
- Als Antwort markiert Bob Wu-MTMicrosoft Contingent Staff, Moderator Freitag, 17. August 2012 05:29
-
Donnerstag, 16. August 2012 06:53
Paul is right. As you do not mention table name at the time initialization, it will consider it as “Table1”. It is good practices to always give a meaning full name to a Data table.
You can do in many ways i.e
DataTable dt = new DataTable("tblStudent");OR
dt.TableName="tblStudent";
OR
da.Fill(dt,"tblStudent");
Lingaraj Mishra
- Als Antwort markiert Bob Wu-MTMicrosoft Contingent Staff, Moderator Freitag, 17. August 2012 05:29

