Whats missing using OleDbDataAdapter.Update to update Access.mdb file? - c#

With the following code below I have managed to open an Access.mpd database and read rows from the table Saved.
However when I try to change data or add new rows I works fine as long as the program is running but nothing seem to be saved to the access.mdb file.
Update: OleDbCommand apparently cannot simply be modified inside the DataAdapter.
Update: AcceptChanges was by me mistakenly used. If used it tells the affected rows to not be updated.
With these updates the code now works. Still I'm looking for understanding of the issue so explanations why will be appreciated. Also If the fixed code is the way to go.
string connection = #"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Documents and Settings\...\Access.mdb;Persist Security Info=True";
OleDbConnection conn = new OleDbConnection(connection);
OleDbDataAdapter da = new OleDbDataAdapter();
OleDbCommand cmd;
cmd = new OleDbCommand();
cmd.CommandText = "Saved";
cmd.CommandType = CommandType.TableDirect;
cmd.Connection = conn;
da.SelectCommand = cmd;
cmd = new OleDbCommand();
cmd.CommandText = "Saved";
cmd.CommandType = CommandType.TableDirect;
cmd.Connection = conn;
da.InsertCommand = cmd;
cmd = new OleDbCommand();
cmd.CommandText = "Saved";
cmd.CommandType = CommandType.TableDirect;
cmd.Connection = conn;
da.UpdateCommand = cmd;
cmd = new OleDbCommand();
cmd.CommandText = "Saved";
cmd.CommandType = CommandType.TableDirect;
cmd.Connection = conn;
da.DeleteCommand = cmd;
OleDbCommandBuilder cb = new OleDbCommandBuilder(da);
da.InsertCommand = cb.GetInsertCommand();
da.DeleteCommand = cb.GetDeleteCommand();
da.UpdateCommand = cb.GetUpdateCommand();
PbDataSet ds = new PbDataSet();
da.Fill(ds, "Saved");
PbDataSet.SavedDataTable table = ds.Tables["Saved"] as PbDataSet.SavedDataTable;
Here I try to change the data, which works. However it is not saved to file. this now works!
PbDataSet.SavedRow sr = table.Rows[0] as PbDataSet.SavedRow;
sr.berAktiv = true; //Changeing data here
sr.AcceptChanges();
da.Update(table as DataTable);
sr = table.NewSavedRow();
sr.rtAktiv = true;
table.AddSavedRow(sr);
table.AcceptChanges();
da.Update(table as DataTable);
No errors are given anywhere.
How can I fix this, so that the data is saved on the file?
How can I verify, in the running program, that it has really been saved, other than reopen the file?

What I can't see is that you're generating the update statements (or let generate them) anywhere...
//Select data
DataSet dataSet = new DataSet();
OleDbDataAdapter dataAdapter = new OleDbDataAdapter(query, db);
dataAdapter.FillSchema(dataSet, SchemaType.Source);
dataAdapter.Fill(dataSet);
//Make changes to the data in the data set...
//Write changes to the mdb
OleDbCommandBuilder cmdBuilder = new OleDbCommandBuilder(dataAdapter);
dataAdapter.Update(dataSet);

Related

How to fill asp listview in fast time if i have thousand of rows

DataTable dt = new DataTable();
SqlConnection conn = new SqlConnection(Global.ConnectionString());
SqlDataAdapter da = new SqlDataAdapter();
SqlCommand cmd = new SqlCommand();
SqlDataReader drReader = null;
cmd = new SqlCommand();
conn.Open();
cmd.Connection = conn;
cmd.CommandType = CommandType.StoredProcedure;
cmd.CommandText = "spIndustryDataSelect";
cmd.Parameters.AddWithValue("#iiProcessType", 1);
drReader = cmd.ExecuteReader();
conn.Close();
da.SelectCommand = new SqlCommand();
da.SelectCommand = cmd;
DataSet ds = new DataSet();
da.Fill(dt);
lstIndustry.DataSource = dt;
lstIndustry.DataBind();
I using this c# code to fill the lstIndustry asp ListView, but it take a minute time because it has a thousand of rows and i need to select all, is there any method to fill the list in fast time?
You can create pagination with Using Take And Skip for improve performance
You can add Index on Table if You have where in sp

UPDATE command not updating data

I have two oledbcommnds used for updating a table in a database.One of them is working (adding data to empty fields) but the other one designed to empty the data, does not work.Any ideas?
First Update commnd that is working (adds values to empty data):
conn.Open();
OleDbDataAdapter adapter1 = new OleDbDataAdapter();
adapter3.UpdateCommand = conn.CreateCommand();
adapter3.UpdateCommand.CommandText = "UPDATE table SET Occup=Yes, Profesor=?";
adapter3.UpdateCommand.Parameters.AddWithValue("p1", "name");
adapter3.UpdateCommand.ExecuteNonQuery();
conn.Close();
The second one, that does not work (replaces values)
conn.Open();
OleDbDataAdapter adapter3 = new OleDbDataAdapter();
adapter3.UpdateCommand = conn.CreateCommand();
adapter3.UpdateCommand.CommandText = "UPDATE table SET Occup=No, Profesor=?";
adapter3.UpdateCommand.Parameters.AddWithValue("p1", "replace_prof_name");
adapter3.UpdateCommand.ExecuteNonQuery();
conn.Close();
When running the second code, I don't get any errors. I have put a counter and it shows the correct number of operations but I see no modfifications.
You are wrongly referring adapter3 instead of adapter1..
Try this
conn.Open();
OleDbDataAdapter adapter1 = new OleDbDataAdapter();
adapter1.UpdateCommand = conn.CreateCommand();
adapter1.UpdateCommand.CommandText = "UPDATE table SET Occup=Yes, Profesor=?";
adapter1.UpdateCommand.Parameters.AddWithValue("p1", "name");
adapter1.UpdateCommand.ExecuteNonQuery();
conn.Close();
conn.Open();
OleDbDataAdapter adapter3 = new OleDbDataAdapter();
adapter3.UpdateCommand = conn.CreateCommand();
adapter3.UpdateCommand.CommandText = "UPDATE table SET Occup=No, Profesor=?";
adapter3.UpdateCommand.Parameters.AddWithValue("p1", "replace_prof_name");
adapter3.UpdateCommand.ExecuteNonQuery();
conn.Close();

Error CS0103 FastReport.net

i am C#.net Windows Form developer .
i am using FastReport.net . but have some error .
this is my code :
DataSet ds = new DataSet();
SqlConnection cnn = new SqlConnection("MyCnnStr");
SqlCommand cmd = new SqlCommand();
cmd.Connection = cnn;
cmd.CommandText = "select * from test";
cmd.CommandType = CommandType.Text;
SqlDataAdapter dap = new SqlDataAdapter(cmd);
dap.Fill(ds, "ds");
report1.RegisterData(ds.Tables[0],"ds");
report1.GetDataSource("ds").Enabled = true;
report1.Load("Untitled.frx");
report1.Show();
but my error :
what is my wrong ?
If name, tel and fax is names of tables, then you can try to write following:
DataSet ds = new DataSet();
SqlConnection cnn = new SqlConnection("MyCnnStr");
SqlCommand cmd = new SqlCommand();
cmd.Connection = cnn;
cmd.CommandText = "select * from test";
cmd.CommandType = CommandType.Text;
SqlDataAdapter dap = new SqlDataAdapter(cmd);
dap.Fill(ds, "ds");
report1.RegisterData(ds.Tables[0],"ds");
report1.GetDataSource("ds").Enabled = true;
report1.GetDataSource("name").Enabled = true; // add this part
report1.GetDataSource("tel").Enabled = true; //
report1.GetDataSource("fax").Enabled = true; //
report1.Load("Untitled.frx");
report1.Show();
Also you can try delete connections in Designer mode.
CS0103 may appear if you casually print symbol from other keyboard layout.
For example: E (English), Е (Russian), Ε (Greek).

Want to show data from two different tables in two different datagridviews in a form in c#

I have two different queries for two different tables I want to show the result in two datagridviews on a form
string query1 = string.Format("select * from Flat where [Flat_No.]='{0}'",flat.Text);
string query2 = string.Format("select * from 1");
SqlCommand cmd = new SqlCommand(query1, con);
SqlCommand cmd1 = new SqlCommand(query2, con1);
dataview frm1 = new dataview(query1,query2); //the form where data is to be displayed
// on form dataview I have two DataGridViews
public dataview(string a,string b)
{
InitializeComponent();
SqlConnection con = new SqlConnection(Class1.getConnectionString);
//connection name
con.Open();
SqlCommand cmd = new SqlCommand(a , con);
cmd.CommandType = CommandType.Text;
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds, "ss");
dataGridView1.DataSource = ds.Tables["ss"];
con.Close();
SqlConnection con1 = new SqlConnection(Class1.getConnectionString);
//connection name for query1
con1.Open();
SqlCommand cmd1 = new SqlCommand(b, con1);
cmd1.CommandType = CommandType.Text;
SqlDataAdapter da1 = new SqlDataAdapter(cmd1);
DataSet ds1 = new DataSet();
da.Fill(ds1, "aa");
dataGridView2.DataSource = ds1.Tables["aa"];
con1.Close();
}
}
but the above code is showing data from query 1 in both the datagridviews. plz help me out how can I solve this problem? If there in another way let me know it also. I have also tried to merge both the queries using "+" sign but it also didn't proved helpful.
Use da1.Fill instead of da.fill. You're using the da DataAdapter for filling both Datasets
da.Fill(ds1, "aa");
da1.Fill(ds1, "aa");

SQL DataTable Update?

I'm not sure, but I think I may have taken a wrong path here. I am trying to update my customer table on my SQL Server. I Connected with a SQLDatareader and then loaded that into my Datatable. I have made all the changes I wanted and now I can't figure out how to get the changes back up. I thought that the "myDataTable.AcceptChanges();" would trigger that to happen but it doesn't.
SqlConnection myConnection = new SqlConnection();
SqlCommand myCommand;
DataTable myDataTable;
SqlDataReader myReader;
myCommand = new SqlCommand();
myCommand.CommandText = " SELECT * FROM customer";
myCommand.CommandType = CommandType.Text;
myCommand.Connection = myConnection;
myCommand.Connection.Open();
myReader = myCommand.ExecuteReader(CommandBehavior.CloseConnection);
myDataTable = new DataTable();
myDataTable.Load(myReader);
// Make Data changes here
myDataTable.AcceptChanges();
MyDataTable.Dispose();
MyCommand.Dispose();
MyConnection.Dispose();
You can use a TableAdapter to commit your changes back to the database. Check out this link for details.
TableAdapter.Update()
In such case you need to use a DataAdapter which has an Update property that takes your Update Query Command.
Even you can use Command Builder and then get the UpdateCommand from CommandBuilder.
Sample Code from MSDN
SqlDataAdapter catDA = new SqlDataAdapter("SELECT CategoryID, CategoryName FROM Categories", nwindConn);
catDA.UpdateCommand = new SqlCommand("UPDATE Categories SET CategoryName = #CategoryName " +
"WHERE CategoryID = #CategoryID" , nwindConn);
catDA.UpdateCommand.Parameters.Add("#CategoryName", SqlDbType.NVarChar, 15, "CategoryName");
SqlParameter workParm = catDA.UpdateCommand.Parameters.Add("#CategoryID", SqlDbType.Int);
workParm.SourceColumn = "CategoryID";
workParm.SourceVersion = DataRowVersion.Original;
DataSet catDS = new DataSet();
catDA.Fill(catDS, "Categories");
DataRow cRow = catDS.Tables["Categories"].Rows[0];
cRow["CategoryName"] = "New Category";
catDA.Update(catDS);
MSDN Link

Categories

Resources