DataAdapter Update method batch updates multiple rows - insert

I have a Problem with DataAdapter Update method :
this is insert code for each row:
private void btnInsert_Click(object sender, EventArgs e)
{
dtTESTDB = dsTESTDB.Tables["dstblTESTDB"];
drTblTESTDB = dtTESTDB.NewRow();
drTblTESTDB.BeginEdit();
drTblTESTDB["ID"] = txtID.Text;
drTblTESTDB["Name"] = txtName.Text;
drTblTESTDB["Family"] = txtFamily.Text;
dtTESTDB.Rows.Add(drTblTESTDB);
dtTESTDB = dsTESTDB.Tables[0].GetChanges(DataRowState.Added);
dtlAdd = dtTESTDB;
drTblTESTDB.EndEdit();
cmd.Parameters.AddWithValue("#ID", int.Parse(txtID.Text));
cmd.Parameters.AddWithValue("#Name", txtName.Text);
cmd.Parameters.AddWithValue("#Family", txtFamily.Text);
cmd.CommandType = CommandType.Text;
cmd.Connection = SQLConnection;
cmd.CommandText = "insert into tblTESTDB
(ID,Name,Family) values (#ID,#Name,#Family)";
cmd.UpdatedRowSource = UpdateRowSource.None;
da.InsertCommand = cmd;
}
now,I want update database after insert multiple rows with :
private void btnSynchronize_Click(object sender, EventArgs e)
{
try
{
da.UpdateBatchSize = 0;// dttblAdd.Rows.Count;
da.Update(dtlAdd);
dsTESTDB.AcceptChanges();
}
catch (SqlException sqlEX)
{
MessageBox.Show(sqlEX.ToString());
}
catch (System.Exception EX)
{
MessageBox.Show(EX.ToString());
}
}
this code work by one row insert without Error ,but if insert two or more record (rows) at a time , DataAdapter insert only last row into DATABASE and throw Exception.
why ?
(sorry - I can't Speak or Writing English very well)

UpdateBatchSize = 0 may be the problem. You need to set it to those many rows as the row count.

Related

Query Data to Combobox using LINQ Query

I am having trouble trying to add/query data from my database using LINQ to Entities to my ASPComboBox. Very noob and doing this for the first time. Does anyone know why this is having issues? My combobox is not being populated at all.
DEVEntities db; //no model namespace in front of db name
protected void Page_Load(object sender, EventArgs e)
{
GridViewOrgSearch.DataBind();
BingoSearchTest ecrm = new BingoSearchTest();
LoadPStatus();
}
void LoadPStatus()
{
db = new DEVEntities();
if (!IsPostBack)
{
var processingQuery = from p in db.BingoLOVs
where p.Category == "ProcessingStatus"
select new { Name = p.Text, ID = p.ID };
ddlProcess.DataSource = processingQuery.ToList();
ddlProcess.ValueField = "ID";
ddlProcess.TextField = "Name";
}
}

null reference exception when i delete item from sync realm database in xamarin

I got a null reference exception after I delete any item from the sync realm database .
the item is deleted from the database but it throws the exception and craches
I don't know why it throws this exception or where is the null object .
but when I delete this line the exception disappears :
listView.ItemsSource = Employees;
PS : this exception appeared when I tried to sync the realm database online.
public MainPage()
{
InitializeComponent();
Initialize();
listView.ItemsSource = Employees;
}
private async Task Initialize()
{
_realm = await OpenRealm();
Employees = _realm.All<Employee>();
Entertainments= _realm.All<Entertainment>();
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(Employees)));
}
void OnDeleteClicked(object sender, EventArgs e)
{
try {
var o = _realm.All<Employee>().FirstOrDefault(c => c.EmpId == 4);
if (o != null)
_realm.Write(() => { _realm.Remove(o); });
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(Employees)));
}
catch (Exception exp)
{
string msg = exp.Message;
}
}
here is a screenshot of the exception
THIS SOLVED THE EXCEPTION ! Thank youu . but the listview is not auto updated now !
According to your code and description, you want to delete item form realm database, and update Listview. I find you use PropertyChanged to want to update Employees, but it doesn't work, because you delete item from realm database, don't change Employees, so it is not fired PropertyChanged event.
List<Employee> Employees = new List<Employee>();
private async Task Initialize()
{
_realm = await OpenRealm();
Employees = _realm.All<Employee>().ToList(); ;
Entertainments = _realm.All<Entertainment>();
}
void OnDeleteClicked(object sender, EventArgs e)
{
try
{
var o = _realm.All<Employee>().FirstOrDefault(c => c.EmpId == 4);
if (o != null)
_realm.Write(() => { _realm.Remove(o); });
Employees = _realm.All<Employee>().ToList();
listView.ItemsSource = Employees;
}
catch (Exception exp)
{
string msg = exp.Message;
}
}
Helpful article for you:
https://dzone.com/articles/xamarinforms-working-with-realm-database

ALTER TABLE doesn't work

I'm trying to change my column datatype in the table item. Now it has just 0(it's int now). What will happen when I run it? I tried to use int num = cmd.ExecuteNonQuery() but didn't work, it came an error. With this code I don't see changes. I found that LONGBLOB is the particular datatype for saving images in the database. This is my code:
public partial class alterOneSec : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
OleDbConnection con = DAL.GetConnection();
con.Open();
if(con.State == ConnectionState.Open)
{
string sql = "ALTER TABLE item ALTER COLUMN picture LONGBLOB NOT NULL";
OleDbCommand cmd = DAL.GetCommand(con, sql);
}
con.Close();
Response.Redirect("homepage.aspx?err=case3");
}
}
The GetCommand method is this:
public static OleDbCommand GetCommand(OleDbConnection con, string sql)
{
OleDbCommand cmd = new OleDbCommand();
cmd.Connection = con;
cmd.CommandType = CommandType.Text;
cmd.CommandText = sql;
return cmd;
}

insert into access table syntax error where

i want to insert some data in access table but it gives me syntax error for my insert into staatements....
so i share my code to you and hope you can help me....
private OleDbConnection conn;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
conn = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;
`Data Source=D:\\Database3.mdb;Persist Security Info=False");
}`
`private void button1_Click(object sender, EventArgs e)`
{
conn.Open();
string query = "INSERT INTO Table([Kaargah],[Manager])" +
"VALUES('"+textBox1.Text+"','"+textBox2.Text+"')";
OleDbCommand command = new OleDbCommand();
command.CommandText = query;
command.Connection = conn;
command.ExecuteNonQuery();
conn.Close();
MessageBox.Show("داده‌ها با موفقیت ثبت گردید!");
}
Try to change your table name to anything other than 'Table'. Like this... Some name such as 'Table' are probably reserved. Avoid using such names.
string query = "INSERT INTO Table1([Kaargah],[Manager]) VALUES ('" + textBox1.Text + "','" + textBox2.Text + "')";
well you can write below query it is fine
string query = "INSERT INTO Table1([Kaargah],[Manager]) VALUES('textBox1.Text','textBox2.Text')";

How to show progress bar with progress calculation in winform while filling dataset?

Here is my code i want to load a progress bar while dataset filling and data importing in a datagridview.Plz Help me
private void btnLoad_Click(object sender, EventArgs e)
{
string str="select Cert_ID,Card_Number,Name,DOB,Mobile,Gender,Relation ";
str = str + " from dbo.beneficiary";
SqlConnection conn = null;
SqlDataAdapter sqlda = null;
DataSet res = new DataSet();
DataTable dt = new DataTable();
try
{
_connectionString = "xyz.........";
conn = new SqlConnection(_connectionString);
conn.Open();
SqlCommand command = new SqlCommand(str, conn);
sqlda = new SqlDataAdapter(command);
sqlda.Fill(res);
dataGridView1.DataSource=res.Tables[0];
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
finally
{
conn.Close();
}
}

Resources