Navigacija
Lista poslednjih: 16, 32, 64, 128 poruka.

C# i DataAdapter.Update()

[es] :: .NET :: .NET Desktop razvoj :: C# i DataAdapter.Update()

[ Pregleda: 4744 | Odgovora: 6 ] > FB > Twit

Postavi temu Odgovori

Autor

Pretraga teme: Traži
Markiranje Štampanje RSS

ismilovic
Ivan Smilović
Istra

Član broj: 63197
Poruke: 89
*.net.t-com.hr.



Profil

icon C# i DataAdapter.Update()18.10.2005. u 08:56 - pre 225 meseci
Pozdrav!
Imam DataSet ds sa tablom Artikli i DataAdapter da.
I. [code]
DataRow cRow = dataTable1.NewRow();
cRow["ART"] = "12345";
cRow["NAZIV"] = "Test artikal";
dataTable1.Rows.Add(cRow);
da.Update(ds, "Artikli");
[endcode]
kod se izvršava bez greške...
II. [code]
dataTable1.Rows[0]["ART"]="xxx";
da.Update(ds, "Artikli");
[endcode]
kod javlja grešku:
Project Upis.exe encountered unhandled exception class System.Data.DBConcurencyException with message 'Concurency violation: UpdateCommand affected 0 records'

Gdje grešim?
 
Odgovor na temu

Csharp

Član broj: 39818
Poruke: 63
*.adsl.net.t-com.hr.



Profil

icon Re: C# i DataAdapter.Update()18.10.2005. u 10:35 - pre 225 meseci
Možda ti ne postoji taj Rows[0]!

To možeš napraviti ako taj row postoji, ali u tvom slučaju ne znam jer ne znam da li ti je tablica napunjena sa podacima! Ako nije onda moraš dodati ono s NewRow!
 
Odgovor na temu

majstor_01

Član broj: 60008
Poruke: 63
*.air.my-its.net.



Profil

icon Re: C# i DataAdapter.Update()18.10.2005. u 11:46 - pre 225 meseci
Kolona ART ti primarni kljuc, koliko sam video iz primera.
Nije moguce da menjas vrednost primarnog kljuca na taj nacin i programa.
To je najnormalnija reakcija baze.

Ako zelis da menjas vrednost artikla, postavi novu kolonu da ti bude primarni kljuc, pa ces onda moci ovu da menjas.

Pozdrav

 
Odgovor na temu

dusans
Stojanov Dušan
Pančevo

Član broj: 9551
Poruke: 1343
212.200.27.*



+311 Profil

icon Re: C# i DataAdapter.Update()18.10.2005. u 14:03 - pre 225 meseci
Samo stavi u update komandi adaptera da ti koristi originalnu vrednost polja ART kod proređenja u WHERE-u a ne tekuću vrednost.

Tekst update komande bi trebao da bude "UPADTE Artikli SET ART=@ART, NAZIV=@NAZIV WHERE ART=@ORIGINAL_ART "
Dakle za ovakvu update komandu bi trebao da imas tri parametra - @ART, @NAZIV i @ORIGINAL_ART.
Svojstvo SourceVersion parametra @ORIGINAL_ART treba da bude postavljeno na DataRowVersion.Original da bi updejt bio uspešno izvršen i u slučaju da menjaš vrednost primarnog ključa.

Ista logika ti važi i za Delete komandu, u WHERE uvek trebaš da porediš originalnu vrednost.
Ako ti je nejasno ovo napisano, pošalji ceo source pa će mo videti pošto se iz ovog koda ne vidi da li si koristio CommandBuilder, Form Wizard ili si ručno pisao komande za adapter.
 
Odgovor na temu

ismilovic
Ivan Smilović
Istra

Član broj: 63197
Poruke: 89
*.net.t-com.hr.



Profil

icon Re: C# i DataAdapter.Update()19.10.2005. u 09:08 - pre 225 meseci
Zahvaljujem na odgovorima

Csharp
Rows[0] postoji

majstor_01
Art nije primarni knjuč - barem ga nisam definirao

dusans - posebno hvala na obješnjenju
Nisam baš upio primjetit grešku zato prilažem source code.
Još jedna napomena:
Kada dodam
cRow.BeginEdit();
...
cRow.EndEdit();
dataTable1.AcceptChanges();
Greška se ne javlja ali izostaje upis u datoteku







using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using Smilovic;

namespace Update
{
/// <summary>
/// Summary description for WinForm1.
/// </summary>
public class WinForm1 : System.Windows.Forms.Form
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.Container components = null;
private System.Windows.Forms.Button Upis;
public DataRow cRow;
private System.Windows.Forms.DataGrid dataGrid1;
private Borland.Data.Provider.BdpConnection IbConn;
private Borland.Data.Provider.BdpCommand bdpSelectCommand1;
private Borland.Data.Provider.BdpCommand bdpInsertCommand1;
private Borland.Data.Provider.BdpCommand bdpUpdateCommand1;
private Borland.Data.Provider.BdpCommand bdpDeleteCommand1;
private Borland.Data.Provider.BdpDataAdapter da;
private System.Data.DataSet ds;
private System.Data.DataTable dataTable1;
private System.Data.DataColumn dataColumn1;
private System.Data.DataColumn dataColumn2;

public WinForm1()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();

//
// TODO: Add any constructor code after InitializeComponent call
//
}

/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose (bool disposing)
{
if (disposing)
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose(disposing);
}

#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.Upis = new System.Windows.Forms.Button();
this.dataGrid1 = new System.Windows.Forms.DataGrid();
this.IbConn = new Borland.Data.Provider.BdpConnection();
this.da = new Borland.Data.Provider.BdpDataAdapter();
this.ds = new System.Data.DataSet();
this.dataTable1 = new System.Data.DataTable();
this.dataColumn1 = new System.Data.DataColumn();
this.dataColumn2 = new System.Data.DataColumn();
this.bdpDeleteCommand1 = new Borland.Data.Provider.BdpCommand();
this.bdpInsertCommand1 = new Borland.Data.Provider.BdpCommand();
this.bdpSelectCommand1 = new Borland.Data.Provider.BdpCommand();
this.bdpUpdateCommand1 = new Borland.Data.Provider.BdpCommand();
((System.ComponentModel.ISupportInitialize)(this.dataGrid1)).BeginInit();
((System.ComponentModel.ISupportInitialize)(this.da)).BeginInit();
((System.ComponentModel.ISupportInitialize)(this.ds)).BeginInit();
((System.ComponentModel.ISupportInitialize)(this.dataTable1)).BeginInit();
this.SuspendLayout();
//
// Upis
//
this.Upis.Location = new System.Drawing.Point(104, 288);
this.Upis.Name = "Upis";
this.Upis.TabIndex = 3;
this.Upis.Text = "Upis";
this.Upis.Click += new System.EventHandler(this.Upis_Click);
//
// dataGrid1
//
this.dataGrid1.CaptionFont = new System.Drawing.Font("Times New Roman", 10.2F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(238)));
this.dataGrid1.DataMember = "";
this.dataGrid1.DataSource = this.dataTable1;
this.dataGrid1.FlatMode = true;
this.dataGrid1.HeaderForeColor = System.Drawing.SystemColors.ControlText;
this.dataGrid1.Location = new System.Drawing.Point(0, 0);
this.dataGrid1.Name = "dataGrid1";
this.dataGrid1.Size = new System.Drawing.Size(208, 265);
this.dataGrid1.TabIndex = 2;
//
// IbConn
//
this.IbConn.ConnectionOptions = "waitonlocks=False;commitretain=False;sqldialect=3;transaction isol" +
"ation=ReadCommitted;servercharset=;rolename=myrole";
this.IbConn.ConnectionString = "database=D:\\C#\\Data\\MIX.GDB;assembly=Borland.Data.Interbase, Versi" +
"on=2.0.0.0, Culture=neutral, PublicKeyToken=91d62ebb5b0d1b1b;vend" +
"orclient=gds32.dll;provider=Interbase;username=sysdba;password=ma" +
"sterkey";
//
// da
//
this.da.Active = true;
this.da.DataSet = this.ds;
this.da.DataTable = this.dataTable1;
this.da.DeleteCommand = this.bdpDeleteCommand1;
this.da.InsertCommand = this.bdpInsertCommand1;
this.da.SelectCommand = this.bdpSelectCommand1;
this.da.StartRecord = 0;
this.da.TableMappings.AddRange(new System.Data.Common.DataTableMapping[] {
new System.Data.Common.DataTableMapping("Table", "ARTIKLI", new System.Data.Common.DataColumnMapping[] {
new System.Data.Common.DataColumnMapping("ART", "ART"),
new System.Data.Common.DataColumnMapping("NAZIV", "NAZIV")})});
this.da.UpdateCommand = this.bdpUpdateCommand1;
//
// ds
//
this.ds.DataSetName = "NewDataSet";
this.ds.Locale = new System.Globalization.CultureInfo("hr-HR");
this.ds.Tables.AddRange(new System.Data.DataTable[] {
this.dataTable1});
//
// dataTable1
//
this.dataTable1.Columns.AddRange(new System.Data.DataColumn[] {
this.dataColumn1,
this.dataColumn2});
this.dataTable1.TableName = "ARTIKLI";
//
// dataColumn1
//
this.dataColumn1.ColumnName = "ART";
this.dataColumn1.MaxLength = 6;
//
// dataColumn2
//
this.dataColumn2.ColumnName = "NAZIV";
this.dataColumn2.MaxLength = 26;
//
// bdpDeleteCommand1
//
this.bdpDeleteCommand1.CommandOptions = null;
this.bdpDeleteCommand1.CommandText = "DELETE FROM ARTIKLI WHERE ART = ? AND NAZIV = ?";
this.bdpDeleteCommand1.CommandType = System.Data.CommandType.Text;
this.bdpDeleteCommand1.Connection = this.IbConn;
this.bdpDeleteCommand1.ParameterCount = ((short)(2));
this.bdpDeleteCommand1.Parameters.Add(new Borland.Data.Common.BdpParameter("ARTOriginal", Borland.Data.Common.BdpType.String, Borland.Data.Common.BdpType.stFixed, 0, System.Data.ParameterDirection.Input, false, ((byte)(5)), ((byte)(0)), 5, "ART", System.Data.DataRowVersion.Original, null));
this.bdpDeleteCommand1.Parameters.Add(new Borland.Data.Common.BdpParameter("NAZIVOriginal", Borland.Data.Common.BdpType.String, Borland.Data.Common.BdpType.stFixed, 0, System.Data.ParameterDirection.Input, false, ((byte)(25)), ((byte)(0)), 25, "NAZIV", System.Data.DataRowVersion.Original, null));
this.bdpDeleteCommand1.SchemaName = null;
this.bdpDeleteCommand1.Transaction = null;
this.bdpDeleteCommand1.UpdatedRowSource = System.Data.UpdateRowSource.None;
//
// bdpInsertCommand1
//
this.bdpInsertCommand1.CommandOptions = null;
this.bdpInsertCommand1.CommandText = "INSERT INTO ARTIKLI (ART, NAZIV ) VALUES ( ?, ?)";
this.bdpInsertCommand1.CommandType = System.Data.CommandType.Text;
this.bdpInsertCommand1.Connection = this.IbConn;
this.bdpInsertCommand1.ParameterCount = ((short)(2));
this.bdpInsertCommand1.Parameters.Add(new Borland.Data.Common.BdpParameter("ART", Borland.Data.Common.BdpType.String, Borland.Data.Common.BdpType.stFixed, 0, System.Data.ParameterDirection.Input, false, ((byte)(5)), ((byte)(0)), 5, "ART", System.Data.DataRowVersion.Current, null));
this.bdpInsertCommand1.Parameters.Add(new Borland.Data.Common.BdpParameter("NAZIV", Borland.Data.Common.BdpType.String, Borland.Data.Common.BdpType.stFixed, 0, System.Data.ParameterDirection.Input, false, ((byte)(25)), ((byte)(0)), 25, "NAZIV", System.Data.DataRowVersion.Current, null));
this.bdpInsertCommand1.SchemaName = null;
this.bdpInsertCommand1.Transaction = null;
this.bdpInsertCommand1.UpdatedRowSource = System.Data.UpdateRowSource.None;
//
// bdpSelectCommand1
//
this.bdpSelectCommand1.CommandOptions = null;
this.bdpSelectCommand1.CommandText = "SELECT ART, NAZIV FROM ARTIKLI";
this.bdpSelectCommand1.CommandType = System.Data.CommandType.Text;
this.bdpSelectCommand1.Connection = this.IbConn;
this.bdpSelectCommand1.ParameterCount = ((short)(0));
this.bdpSelectCommand1.SchemaName = null;
this.bdpSelectCommand1.Transaction = null;
this.bdpSelectCommand1.UpdatedRowSource = System.Data.UpdateRowSource.None;
//
// bdpUpdateCommand1
//
this.bdpUpdateCommand1.CommandOptions = null;
this.bdpUpdateCommand1.CommandText = "UPDATE ARTIKLI SET ART = ?, NAZIV = ? WHERE ART = ? AND NAZIV = ?";
this.bdpUpdateCommand1.CommandType = System.Data.CommandType.Text;
this.bdpUpdateCommand1.Connection = this.IbConn;
this.bdpUpdateCommand1.ParameterCount = ((short)(4));
this.bdpUpdateCommand1.Parameters.Add(new Borland.Data.Common.BdpParameter("ART", Borland.Data.Common.BdpType.String, Borland.Data.Common.BdpType.stFixed, 0, System.Data.ParameterDirection.Input, false, ((byte)(5)), ((byte)(0)), 5, "ART", System.Data.DataRowVersion.Current, null));
this.bdpUpdateCommand1.Parameters.Add(new Borland.Data.Common.BdpParameter("NAZIV", Borland.Data.Common.BdpType.String, Borland.Data.Common.BdpType.stFixed, 0, System.Data.ParameterDirection.Input, false, ((byte)(25)), ((byte)(0)), 25, "NAZIV", System.Data.DataRowVersion.Current, null));
this.bdpUpdateCommand1.Parameters.Add(new Borland.Data.Common.BdpParameter("ARTOriginal", Borland.Data.Common.BdpType.String, Borland.Data.Common.BdpType.stFixed, 0, System.Data.ParameterDirection.Input, false, ((byte)(5)), ((byte)(0)), 5, "ART", System.Data.DataRowVersion.Original, null));
this.bdpUpdateCommand1.Parameters.Add(new Borland.Data.Common.BdpParameter("NAZIVOriginal", Borland.Data.Common.BdpType.String, Borland.Data.Common.BdpType.stFixed, 0, System.Data.ParameterDirection.Input, false, ((byte)(25)), ((byte)(0)), 25, "NAZIV", System.Data.DataRowVersion.Original, null));
this.bdpUpdateCommand1.SchemaName = null;
this.bdpUpdateCommand1.Transaction = null;
this.bdpUpdateCommand1.UpdatedRowSource = System.Data.UpdateRowSource.None;
//
// WinForm1
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(208, 326);
this.Controls.Add(this.dataGrid1);
this.Controls.Add(this.Upis);
this.Name = "WinForm1";
this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
this.Text = "WinForm1";
this.Activated += new System.EventHandler(this.WinForm1_Activated);
((System.ComponentModel.ISupportInitialize)(this.dataGrid1)).EndInit();
((System.ComponentModel.ISupportInitialize)(this.da)).EndInit();
((System.ComponentModel.ISupportInitialize)(this.ds)).EndInit();
((System.ComponentModel.ISupportInitialize)(this.dataTable1)).EndInit();
this.ResumeLayout(false);
}
#endregion

/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new WinForm1());
}

private void WinForm1_Activated(object sender, System.EventArgs e)
{
da.Active = true;
}


private void Upis_Click(object sender, System.EventArgs e)
{


DataRow cRow = dataTable1.Rows[0];
cRow.BeginEdit();
// Javi.Poruka(""+cRow[0]);
cRow ["ART"]= "xxx";
cRow.EndEdit();
dataTable1.AcceptChanges();
//ds.Tables["Artikli"].Rows[0]["ART"]="xxx";

/* DataRow cRow = dataTable1.NewRow();
cRow["ART"] = "22334";
cRow["NAZIV"] = "Drugi artikal";
dataTable1.Rows.Add(cRow); */

// Close();


try {
da.Update(ds, "Artikli");
}
catch ( Exception ex) {
Javi.Poruka(ex.ToString());
}
}
}
}

 
Odgovor na temu

Csharp

Član broj: 39818
Poruke: 63
*.adsl.net.t-com.hr.



Profil

icon Re: C# i DataAdapter.Update()19.10.2005. u 12:45 - pre 225 meseci
Sada ti ne upisuje u bazu zbog AcceptChanges. To radiš nakon update-a u bazu
 
Odgovor na temu

ismilovic
Ivan Smilović
Istra

Član broj: 63197
Poruke: 89
*.hr
Via: [es] mailing liste



Profil

icon Re: C# i DataAdapter.Update()22.10.2005. u 17:08 - pre 225 meseci
Csharp HVALA! ...to je bilo to
 
Odgovor na temu

[es] :: .NET :: .NET Desktop razvoj :: C# i DataAdapter.Update()

[ Pregleda: 4744 | Odgovora: 6 ] > FB > Twit

Postavi temu Odgovori

Navigacija
Lista poslednjih: 16, 32, 64, 128 poruka.