Hello!
At school we created a project and it works fine.
I wanted to create my own project, it's practically identical to the school project, so i copy paste the code, and modify it to my needs.
Everything works fine, i can add data in C-sharp DataSet, but when i press "Shrani" button (this means Save - to add data in Access database) it shows me an error "Syntax error in UPDATE statement. I have another button "Spremeni" (which means Edit) and again it works fine (it successfully store data in DataSet) but when i press "Shrani" it shows me the same error.
I don't understand why it wouldn't work, because it's basically the same as the school project (the only difference is name of the table, and column names; and i changed them in a code).
If anybody can help me, i would really appreciate it.
Thanks
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.OleDb;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace WindowsFormsApplication3
{
public partial class Delnice : Form
{
private OleDbConnection PortCN;
private OleDbCommand PortCM;
private OleDbDataAdapter PortDA;
private DataSet PortDS;
private DataTable PortDT;
private DataRow PortDR;
private OleDbCommandBuilder PortCB;
private string selectDelnice;
int[] A;
public Delnice()
{
InitializeComponent();
PortCN = new OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;" +
"Data Source=C:\\Users\\Matej\\Desktop\\Visual - AMBase\\Baza.accdb;");
PortCM = new OleDbCommand();
PortCM.Connection = PortCN;
PortDA = new OleDbDataAdapter();
PortDS = new DataSet();
PortDT = new DataTable();
selectDelnice = "SELECT * FROM Delnice ORDER BY ID;";
SelectDelniceRows();
}
private void SelectDelniceRows()
{
PortCM.CommandText = selectDelnice;
PortDA.SelectCommand = PortCM;
try
{
PortCN.Open();
int DelniceSt = PortDA.Fill(PortDS, "Delnice");
PortCN.Close();
PortDT = PortDS.Tables["Delnice"];
PortDT.PrimaryKey =
new DataColumn[]
{
PortDT.Columns["ID"]
};
comboBoxPortiFill();
}
catch (System.Exception e)
{
MessageBox.Show(e.Message);
}
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void comboBoxPortiFill()
{
int stVrstic = PortDT.Rows.Count;
A = new int[stVrstic];
int i = 0;
foreach (DataRow vrstica in PortDT.Rows)
{
comboBoxPorti.Items.Add(vrstica["Ime delnice"]);
A[i] = 0;
A[i] = (int)vrstica["ID"];
i++;
}
}
private void comboBoxPorti_SelectedIndexChanged(object sender, EventArgs e)
{
{
string trenutniIzbor = comboBoxPorti.SelectedItem.ToString();
comboBoxPorti.Text = trenutniIzbor;
DataRow VrsticaTabele = PortDT.Rows.Find(A[comboBoxPorti.SelectedIndex]);
textBoxID.Text = VrsticaTabele["ID"].ToString();
textBoxIme.Text = VrsticaTabele["Ime delnice"].ToString();
textBoxSYM.Text = VrsticaTabele["SYM"].ToString();
textBoxBorza.Text = VrsticaTabele["Borza"].ToString();
textBoxSektor.Text = VrsticaTabele["Sektor"].ToString();
textBoxCena.Text = VrsticaTabele["Cena"].ToString();
maskedTextBoxDatum.Text = VrsticaTabele["Datum"].ToString();
textBoxKoličina.Text = VrsticaTabele["Količina"].ToString();
textBoxPort.Text = VrsticaTabele["Port"].ToString();
textBoxProvizija.Text = VrsticaTabele["Provizija"].ToString();
textBoxValuta.Text = VrsticaTabele["Valuta"].ToString();
textBoxTečaj.Text = VrsticaTabele["Tečaj"].ToString();
maskedTextBoxZnesek.Text = VrsticaTabele["Celoten znesek"].ToString();
decimal s;
decimal.TryParse(VrsticaTabele["Celoten znesek"].ToString(), out s);
maskedTextBoxZnesek.Text = s.ToString("000.00€");
}
}
private void maskedTextBoxDatum_Click(object sender, EventArgs e)
{
this.maskedTextBoxDatum.Select(0, 0);
}
private void buttonDodaj_Click(object sender, EventArgs e)
{
try
{
PortDR = PortDT.NewRow();
PortDR["ID"] = textBoxID.Text;
PortDR["Ime delnice"] = textBoxIme.Text;
PortDR["SYM"] = textBoxSYM.Text;
PortDR["Borza"] = textBoxBorza.Text;
PortDR["Sektor"] = textBoxSektor.Text;
PortDR["Cena"] = textBoxCena.Text;
PortDR["Datum"] = maskedTextBoxDatum.Text;
PortDR["Količina"] = textBoxKoličina.Text;
PortDR["Port"] = textBoxPort.Text;
PortDR["Provizija"] = textBoxProvizija.Text;
PortDR["Valuta"] = textBoxValuta.Text;
PortDR["Tečaj"] = textBoxTečaj.Text;
PortDT.Rows.Add(PortDR);
MessageBox.Show("Dodano:" + textBoxSYM.Text + " delnica");
}
catch (SystemException x)
{
MessageBox.Show(x.Message);
}
finally
{
}
buttonShrani.Enabled = true;
}
private void buttonShrani_Click(object sender, EventArgs e)
{
try
{
PortCB = new OleDbCommandBuilder(PortDA);
PortCN.Open();
int delRows = PortDA.Update(PortDS, "Delnice");
MessageBox.Show("Shranjeno " + delRows);
}
catch (SystemException x)
{
MessageBox.Show(x.Message);
}
finally
{
PortCN.Close();
}
}
private void buttonSpremeni_Click(object sender, EventArgs e)
{
try
{
DataRow VrsticaTabele = PortDT.Rows.Find(A[comboBoxPorti.SelectedIndex]);
VrsticaTabele["ID"] = textBoxID.Text;
VrsticaTabele["Ime delnice"]= textBoxIme.Text;
VrsticaTabele["SYM"] = textBoxSYM.Text;
VrsticaTabele["Borza"]= textBoxBorza.Text;
VrsticaTabele["Sektor"] = textBoxSektor.Text;
VrsticaTabele["Cena"]= textBoxCena.Text;
VrsticaTabele["Datum"]= maskedTextBoxDatum.Text;
VrsticaTabele["Količina"]= textBoxKoličina.Text;
VrsticaTabele["Port"]= textBoxPort.Text;
VrsticaTabele["Provizija"]= textBoxProvizija.Text;
VrsticaTabele["Valuta"]= textBoxValuta.Text;
VrsticaTabele["Tečaj"]= textBoxTečaj.Text;
VrsticaTabele.EndEdit();
MessageBox.Show("Spremenjeno");
buttonShrani.Enabled = true;
}
catch (System.Exception x)
{
MessageBox.Show(x.Message);
}
}
}
}