updating access database in C#2005
-
Hi everyone Im using c# 2005 and I want to update my access database, it can insert fine and has no errors but just does not update Here's the code below
string strConnection = "Provider=Microsoft.Jet.OLEDB.4.0;" + "Data Source=datatry.mdb"; OleDbCommand command = new OleDbCommand(); string bhala = "Update Table1 Set CompanyName ='"+txtCompany.Text+"' where CompanyName = '" + txtCompany.Text + "'"; OleDbConnection datatryConn = new OleDbConnection(strConnection); datatryConn.Open(); OleDbDataAdapter da = new OleDbDataAdapter(bhala, datatryConn); //DataSet ds = new DataSet(); DataRow r = datatryds.Tables["Table1"].NewRow(); //r["CompanyName"] = txtCompany.Text; datatryds.Tables["Table1"].Rows.Find(r); r.BeginEdit(); r["CompanyName"] = txtCompany.Text; r.EndEdit(); if (datatryds.HasChanges()) { da.Update(datatryds, "Table1"); datatryds.AcceptChanges(); } OleDbCommandBuilder objCommandBuilder = new OleDbCommandBuilder(da); //da.Update(datatryds, "Table1"); //this.table1TableAdapter.Update(this.datatryds.Table1); datatryConn.Close(); MessageBox.Show("Updated successfully!");
Thank you any help is appreciated csanda -
Hi everyone Im using c# 2005 and I want to update my access database, it can insert fine and has no errors but just does not update Here's the code below
string strConnection = "Provider=Microsoft.Jet.OLEDB.4.0;" + "Data Source=datatry.mdb"; OleDbCommand command = new OleDbCommand(); string bhala = "Update Table1 Set CompanyName ='"+txtCompany.Text+"' where CompanyName = '" + txtCompany.Text + "'"; OleDbConnection datatryConn = new OleDbConnection(strConnection); datatryConn.Open(); OleDbDataAdapter da = new OleDbDataAdapter(bhala, datatryConn); //DataSet ds = new DataSet(); DataRow r = datatryds.Tables["Table1"].NewRow(); //r["CompanyName"] = txtCompany.Text; datatryds.Tables["Table1"].Rows.Find(r); r.BeginEdit(); r["CompanyName"] = txtCompany.Text; r.EndEdit(); if (datatryds.HasChanges()) { da.Update(datatryds, "Table1"); datatryds.AcceptChanges(); } OleDbCommandBuilder objCommandBuilder = new OleDbCommandBuilder(da); //da.Update(datatryds, "Table1"); //this.table1TableAdapter.Update(this.datatryds.Table1); datatryConn.Close(); MessageBox.Show("Updated successfully!");
Thank you any help is appreciated csanda -
Hi everyone Im using c# 2005 and I want to update my access database, it can insert fine and has no errors but just does not update Here's the code below
string strConnection = "Provider=Microsoft.Jet.OLEDB.4.0;" + "Data Source=datatry.mdb"; OleDbCommand command = new OleDbCommand(); string bhala = "Update Table1 Set CompanyName ='"+txtCompany.Text+"' where CompanyName = '" + txtCompany.Text + "'"; OleDbConnection datatryConn = new OleDbConnection(strConnection); datatryConn.Open(); OleDbDataAdapter da = new OleDbDataAdapter(bhala, datatryConn); //DataSet ds = new DataSet(); DataRow r = datatryds.Tables["Table1"].NewRow(); //r["CompanyName"] = txtCompany.Text; datatryds.Tables["Table1"].Rows.Find(r); r.BeginEdit(); r["CompanyName"] = txtCompany.Text; r.EndEdit(); if (datatryds.HasChanges()) { da.Update(datatryds, "Table1"); datatryds.AcceptChanges(); } OleDbCommandBuilder objCommandBuilder = new OleDbCommandBuilder(da); //da.Update(datatryds, "Table1"); //this.table1TableAdapter.Update(this.datatryds.Table1); datatryConn.Close(); MessageBox.Show("Updated successfully!");
Thank you any help is appreciated csandaAs someone else said, this can't work, you need the old name in order to find the right record to update, unless you have a unique Id ( definately a better idea ) to identify the one record you want to change. Your code is also insecure, you need to read up on SQL injection attacks.
Christian Graus - Microsoft MVP - C++ "I am working on a project that will convert a FORTRAN code to corresponding C++ code.I am not aware of FORTRAN syntax" ( spotted in the C++/CLI forum )