Saving DataTable to SQLite Database by Adapter.Update - c#

I wrote SQLite wrapper class
like this
using System;
using System.Data;
using System.Data.SQLite;
namespace SuPOS.Sources
{
public class SQLITE
{
private SQLiteConnection con;
private SQLiteCommand cmd;
private SQLiteDataAdapter adapter;
public SQLITE(string databasename)
{
con = new SQLiteConnection(string.Format("Data Source={0};Compress=True;", databasename));
}
public int Execute(string sql_statement)
{
con.Open();
cmd = con.CreateCommand();
cmd.CommandText = sql_statement;
int row_updated;
try
{
row_updated = cmd.ExecuteNonQuery();
}
catch
{
con.Close();
return 0;
}
con.Close();
return row_updated;
}
public DataTable GetDataTable(string tablename)
{
DataTable DT = new DataTable();
con.Open();
cmd = con.CreateCommand();
cmd.CommandText = string.Format("SELECT * FROM {0}", tablename);
adapter = new SQLiteDataAdapter(cmd);
adapter.AcceptChangesDuringFill = false;
adapter.Fill(DT);
con.Close();
DT.TableName = tablename;
return DT;
}
public void SaveDataTable(DataTable DT)
{
try
{
Execute(string.Format("DELETE FROM {0}", DT.TableName));
con.Open();
cmd = con.CreateCommand();
cmd.CommandText = string.Format("SELECT * FROM {0}", DT.TableName);
adapter = new SQLiteDataAdapter(cmd);
SQLiteCommandBuilder builder = new SQLiteCommandBuilder(adapter);
adapter.Update(DT);
con.Close();
}
catch (Exception Ex)
{
System.Windows.MessageBox.Show(Ex.Message);
}
}
}
}
I retrive DataTable from Database by "GetDataTable"
and binding to control.itemsource in WPF such as DataGrid
I can add new row on DataGrid and save to Database properly
I can change any column and save to database properly
but the problem is,
when I insert new row to DataGrid and call "SaveDataTable" as usual
I can save properly.. then I change some column on that row
now when I use "SaveDataTable" it said like this picture
after this message appear,
when I call "SaveDataTable" it will always said like this picture

I know this is an old thread, but the extra code you wrote for your solution only works because it cancels out a bug in your code, where you set:
adapter.AcceptChangesDuringFill = false ; /* default is 'true' */
If you remove the above line, you no longer need to have the row accept the changes and your method becomes much cleaner:
public DataTable GetDataTable(string tablename)
{
DataTable DT = new DataTable();
con.Open();
cmd = con.CreateCommand();
cmd.CommandText = string.Format("SELECT * FROM {0}", tablename);
adapter = new SQLiteDataAdapter(cmd);
adapter.Fill(DT);
con.Close();
DT.TableName = tablename;
return DT;
}

finally, I found the solution.
the problem came from rowstate of each row after I call Fill(DataTable), every DataRow in DataTable after Fill have "Added" in RowState.
so I can't call "Update(DataTable)" because it will Insert every rows in DataTable to Database.
because of that, in my wrapper class I have to Delete all rows in Database before "Update(DataTable).
so, I have to change RowState in every DataRow after method Fill(DataTable) by "AcceptChanges" method.
public DataTable GetDataTable(string tablename)
{
DataTable DT = new DataTable();
con.Open();
cmd = con.CreateCommand();
cmd.CommandText = string.Format("SELECT * FROM {0}", tablename);
adapter = new SQLiteDataAdapter(cmd);
adapter.AcceptChangesDuringFill = false;
adapter.Fill(DT);
con.Close();
DT.TableName = tablename;
foreach (DataRow row in DT.Rows)
{
row.AcceptChanges();
}
return DT;
}
public void SaveDataTable(DataTable DT)
{
try
{
con.Open();
cmd = con.CreateCommand();
cmd.CommandText = string.Format("SELECT * FROM {0}", DT.TableName);
adapter = new SQLiteDataAdapter(cmd);
SQLiteCommandBuilder builder = new SQLiteCommandBuilder(adapter);
adapter.Update(DT);
con.Close();
}
catch (Exception Ex)
{
System.Windows.MessageBox.Show(Ex.Message);
}
}
Now I don't have to Delete all row before Update, and the problem gone.

Related

How to search data in gridview using a TextBox value with an Oracle Database

I am trying to show Oracle Data in a DataGridView in my Windows Form Application but it just returns a grey blank view. My code for this currently is:
string insertquery = "select * from Candidate where CandidateName like '"+ txtBoxSearchData.Text +"%'";
OracleConnection con = new OracleConnection(oradb);
con.Open();
OracleCommand cmd = new OracleCommand();
cmd.Connection = con;
cmd.CommandText = insertquery;
try
{
OracleDataReader reader = cmd.ExecuteReader();
OracleDataAdapter orada = new OracleDataAdapter(cmd);
DataTable dataTable = new DataTable();
orada.Fill(dataTable);
dataTable.Load(reader);
BindingSource bSource = new BindingSource();
bSource.DataSource = dataTable;
dataGridViewSearch.DataSource = bSource;
orada.Update(dataTable);
}
catch(ArgumentException ex)
{
MessageBox.Show("Error: " + ex.Message);
}
catch(OracleException ex1)
{
MessageBox.Show("Error: " + ex1.Message);
}
finally
{
cmd.Dispose();
con.Dispose();
}
}
I am positive I do not require all those functions in my Try statement but I have come across many different methods to do this - I just included all of those into my code to experiment. The connection string is correct too as I have succeeded in adding data into the tables through queries in another part of my application.
Doing something like the following should be enough. There should be no need for an OracleDataAdapter or BindingSource. Just can't test it here, as I do not have an Oracle database around.
public void fillDataGrid()
{
try
{
using(OracleConnection connection = new OracleConnection("connectstring"))
using(OracleCommand cmd = new OracleCommand("select * from my super table", connection ))
{
connection .Open();
using(OracleDataReader oracleDataReader = cmd.ExecuteReader())
{
DataTable dataTable = new DataTable();
dataTable.Load(oracleDataReader );
myDataGrid.DataSource = dataTable;
}
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
}

Updating table with SqliteDataAdapter

I have a bit of an issue when it comes to updating view in datagridview. My function successfully deletes selected row from database but DataAdapter does not fill datatable dt. Function with the same structure works perfectly when adding rows.
private string ConString;
private SQLiteCommand cmd;
DataTable dt = new DataTable();
SQLiteConnection sql_con;
SQLiteDataAdapter DB;
// Function to delete rows
public void DeleteRow(string value)
{
string sqlStatement = "DELETE FROM Trades WHERE ID = " + value;
try
{
sql_con = new SQLiteConnection(ConString);
sql_con.Open();
cmd = sql_con.CreateCommand();
cmd.CommandText = sqlStatement;
cmd.ExecuteNonQuery();
DB = new SQLiteDataAdapter();
dt.Clear();
DB.Fill(dt);
}
finally
{
sql_con.Close();
}
}

Winforms comboboxes and parameterized stored procedure

In my tblSates Table, I have a column named Monat with these three values
[01.2016, 02.2016 and 03.2016] and I want to fetch these values in a combobox.
I am getting the values but just two of them instead of all three.
Here is my code:
private void FillCombobox2()
{
string S = ConfigurationManager
// TSQL-Statement
SqlCommand cmd = new SqlCommand();
cmd.Connection = con;
cmd.CommandText = ("SELECT DISTINCT Monat from tblSales");
SqlDataReader myReader;
try
{
con.Open();
myReader = cmd.ExecuteReader();
if (myReader.Read())
{
DataTable dt = new DataTable();
dt.Load(myReader);
combobox1.DisplayMember = "Monat";
combobox1.ValueMember = "Monat";
combobox1.DataSource = dt;
combobox1.SelectedIndex = -1;
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
finally
{
con.Close();
}
}
Will appreciate any help or an alternative solution.
I removed reader.Read and then call which advanced the position (and skipped one of my three records). Alternatively I could have used if (myReader.HasRows).
private void FillCombobox2()
{
string S = ConfigurationManager
// TSQL-Statement
SqlCommand cmd = new SqlCommand();
cmd.Connection = con;
cmd.CommandText = ("SELECT DISTINCT Monat from tblSales");
//SqlDataReader myReader;
try
{
con.Open();
SqlDataAdapter ad = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
ad.Fill(dt)
combobox1.DisplayMember = "Monat";
combobox1.ValueMember = "Monat";
combobox1.DataSource = dt;
combobox1.SelectedIndex = -1;
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
finally
{
con.Close();
}
}

data not show on ms access

i'm writing a code on c# (winform). the program is about cachier and the database is
ms access.
when i am entering the data to the database it seems like the data was enterd but when i'm opening the ms access the table is empty. althogh, if i right click on the 'preview data set' in the visual studio, i can see the data.
here is my code so far regard to the database:
private void buttonCloseCart_Click(object sender, EventArgs e)
{
for (int i = 0; i < baught_items.Count; i++)
{
connect.ConnectionString = #"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\אורון\documents\visual studio 2012\Projects\CachierPro\CachierPro\CachierProDB.accdb";
string temp_item = baught_items[i].ToString();
int temp_item_quantity = baught_items_quantity[i];
double temp_item_price = baught_items_price[i];
double temp_total_item_price = total_items_price[i];
connect.Open();
OleDbCommand cmd = new OleDbCommand("INSERT INTO Receipts (ItemName, Quantity, PricePerOne, Total) VALUES (#temp_item, #temp_item_quantity, #temp_item_price, #temp_total_item_price)", connect);
if (connect.State == ConnectionState.Open)
{
cmd.Parameters.Add ("#temp_item", OleDbType.Char, 20).Value = temp_item;
cmd.Parameters.Add("#temp_item_quantity", OleDbType.Integer, 20).Value = temp_item_quantity;
cmd.Parameters.Add("#temp_item_price", OleDbType.Double, 20).Value = temp_item_price;
cmd.Parameters.Add("#cart_sum", OleDbType.Double,20).Value = temp_total_item_price;
try
{
cmd.ExecuteNonQuery();
OleDbDataAdapter da = new OleDbDataAdapter();
da.SelectCommand = cmd;
DataTable dt = new DataTable();
da.Fill(dt);
MessageBox.Show("Data Added To DataBase");
textBoxCurrentCartSumTXT.Clear();
textBoxPricePerOneTXT.Clear();
textBoxQuantityTXT.Clear();
textBoxSumForCurrentItemTXT.Clear();
connect.Close();
}
catch (Exception expe)
{
MessageBox.Show(expe.Source);
connect.Close();
}
}
else
{
MessageBox.Show("Connection Failed");
}
}
}
To get any rows back from your database storage through a OleDbDataAdapter you need to set its SelectCommand with a command that contains a SELECT statement
da.SelectCommand = new OleDbCommand("SELECT * FROM Receipts", connect);
DataTable dt = new DataTable();
da.Fill(dt);
Actually you are using the same command used to INSERT data as it was the SelectCommand. Obviously it doesn't return records. You should have a duplicate record in your table.
I would change something to your code. If you have more than one record to add to your table (you have a loop there) then there is no sense in extracting data from your db at every loop. I would call the Fill of the table outside the loop. Also a bit performance gain could be obtained defining the OleDbCommand and its parameters just one time before entering the loop. Inside the loop just update the values of the parameters and call ExecuteNonQuery
private void buttonCloseCart_Click(object sender, EventArgs e)
{
connect.ConnectionString = #"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\אורון\documents\visual studio 2012\Projects\CachierPro\CachierPro\CachierProDB.accdb";
connect.Open();
OleDbCommand cmd = new OleDbCommand(#"INSERT INTO Receipts
(ItemName, Quantity, PricePerOne, Total)
VALUES (#temp_item, #temp_item_quantity,
#temp_item_price, #temp_total_item_price)", connect);
cmd.Parameters.Add ("#temp_item", OleDbType.Char, 20);
cmd.Parameters.Add("#temp_item_quantity", OleDbType.Integer, 20);
cmd.Parameters.Add("#temp_item_price", OleDbType.Double, 20);
cmd.Parameters.Add("#cart_sum", OleDbType.Double,20);
for (int i = 0; i < baught_items.Count; i++)
{
string temp_item = baught_items[i].ToString();
int temp_item_quantity = baught_items_quantity[i];
double temp_item_price = baught_items_price[i];
double temp_total_item_price = total_items_price[i];
if (connect.State == ConnectionState.Open)
{
cmd.Parameters["#temp_item"].Value = temp_item;
cmd.Parameters["#temp_item_quantity"].Value = temp_item_quantity;
cmd.Parameters["#temp_item_price"].Value = temp_item_price;
cmd.Parameters["#cart_sum"].Value = temp_total_item_price;
try
{
int addedCount = cmd.ExecuteNonQuery();
if(addedCount == 0)
{
... problems here, record not added for some reasons
}
}
catch (Exception expe)
{
MessageBox.Show(expe.Source);
connect.Close();
}
}
else
{
MessageBox.Show("Connection Failed");
}
}
OleDbDataAdapter da = new OleDbDataAdapter();
da.SelectCommand = new OleDbCommand("SELECT * FROM Receipts", connect);
DataTable dt = new DataTable();
da.Fill(dt);
textBoxCurrentCartSumTXT.Clear();
textBoxPricePerOneTXT.Clear();
textBoxQuantityTXT.Clear();
textBoxSumForCurrentItemTXT.Clear();
connect.Close();
}

How to assing values to 2 dimensional array with data from database in asp.net using c#?

Suppose that Sql database is StudentInfo and Table name is Registration
ID----------Name---------------Email---------------------------PhoneNo
1 Munasunghe amilamunasinghe#yahoo.com 0717069425
2 Liyanarachchi hareshliya6#gmail.com 0756706352
protected void Page_Load(object sender, EventArgs e)
{
string query = "select ID, Name, Email, PhoneNo from Registration";
SqlCommand cmd1 = new SqlCommand(query);
DataTable dt1 = GetData(cmd1);
int rowcount = dt1.Rows.Count;
/* I want to assing dt1 datatable data to 2 dimensional array*/
}
The function GetData is used to get data from the Database.
private DataTable GetData(SqlCommand cmd)
{
DataTable dt = new DataTable();
String strConnString = System.Configuration.ConfigurationManager.ConnectionStrings["conString"].ConnectionString;
SqlConnection con = new SqlConnection(strConnString);
SqlDataAdapter sda = new SqlDataAdapter();
cmd.CommandType = CommandType.Text;
cmd.Connection = con;
try
{
con.Open();
sda.SelectCommand = cmd;
sda.Fill(dt);
return dt;
}
catch
{
return null;
}
finally
{
con.Close();
sda.Dispose();
con.Dispose();
}
}
Please give an example code to help me.
object[,] Target = new object[dt1.Rows.Count, dt1.Columns.Count];
int RowCount = 0;
foreach(DataRow dr in dt1.Rows)
foreach(DataColumn dc in dt1.Columns)
{
Target[RowCount, dc.Ordinal] = dr[dc];
RowCount++;
}

Categories