[ Lanaaa @ 06.07.2010. 13:51 ] @
E ovako:
Za fax trebam napraviti neku aplikaciju vezanu za racunovodstvo u VS. U VS-u sam napravila i bazu sa 3 tabele: Konto, Dokument, Transakcija; Transakcija je povezana na druge preko vanjskih kljuceva.
Na formu za unos nove transakcije postavljen combo box koji je preko 'use data bound' povezan na bazu:
Datasource - kontoBindingSource
Displaymember - Sifra
Valuemember - Sifra
Selectedvalue - transakcijaBindingSource-SKonta

Sifra je u Kontu primarni kljuc, a Skonta vanjski u Transakciji
Isto je uradjeno i sa drugim combo boxom koji prikazuje nazive dokumenta, a trebao bi pohranjivati sifre dokumenata u transakciju u SDokumenta (vanjski kljuc).

kod:

Code:
private void button1_Click(object sender, EventArgs e)
        {string tekstVeze = "Data Source=.\\SQLEXPRESS; AttachDbFilename = C:\\ProjektBazaNova.mdf; Integrated Security = true; Connect Timeout = 30; User Instance = True";
                SqlConnection veza = new SqlConnection(tekstVeze);
                veza.Open();

                string skonta = comboBox1.ValueMember;
                decimal dug = Convert.ToDecimal(textBox2.Text);
                decimal pot = Convert.ToDecimal(textBox3.Text);
                int sdok = Convert.ToInt32(comboBox2.ValueMember);
                string dat = dateTimePicker1.Text;

               string naredba = "INSERT INTO Transakcija (Datum, SKonta, Duguje, Potrazuje, SDokumenta) VALUES ('"+dat+"','"+skonta+"','"+dug+"','"+pot+"','"+sdok+"')";
                SqlCommand com = new SqlCommand(naredba, veza);
                com.ExecuteNonQuery();

Kada pokrenem aplikaciju i pokusam spremiti podatke u batu izbacuje error:

The INSERT statement conflicted with the FOREIGN KEY constraint "FK_Transakcija_Konto". The conflict occurred in database "C:\PROJEKTBAZANOVA.MDF", table "dbo.Konto", column 'Sifra'.
The statement has been terminated.

Dalje, klikom na taj isti button bi se trebali azurirati podaci u tablici Konto, sto sam htjela uraditi tako da prvo povucem stanje na duguje i potrazuje na kontu koji ima istu sifru kao u combo boxu, zbrojim je sa 'dug' odnosno 'pot' i onda update uradim. Ali opet greska:

Code:
string naredba2 = "SELECT Duguje FROM Konto WHERE Sifra = '" + skonta + "'";
                SqlCommand com2 = new SqlCommand(naredba2, veza);
                SqlDataReader citac = com2.ExecuteReader();
                citac.Read();
                decimal duguje = citac.GetDecimal(1);
                decimal dd = dug + duguje;

                string naredba3 = "SELECT Potrazuje FROM Konto WHERE Sifra = '" + skonta + "'";
                SqlCommand com3 = new SqlCommand(naredba3, veza);
                SqlDataReader citac2 = com3.ExecuteReader();
                citac2.Read();
                decimal potrazuje = citac2.GetDecimal(1);
                decimal pp = potrazuje + pot;

            string naredba4 = "UPDATE Konto SET Duguje='"+duguje+"', Potrazuje='"+potrazuje+"' WHERE Sifra='"+skonta+"'";
            SqlCommand com4 = new SqlCommand(naredba4, veza);
            com4.ExecuteNonQuery();


Invalid attempt to read when no data is present.
i oznacen ovaj red -> decimal duguje = citac.GetDecimal(1);

Koliko se meni cini da je problem sa combo boxom, ali pojma nemam sta kako :s

MOOOOLIIIIM pomoc, sutra trebam predati program :(
[ AMD guy @ 06.07.2010. 15:46 ] @
http://social.msdn.microsoft.c...d-bc2e-4afc-824e-b75f18a8b491/

Citat:
The error message is pretty clear. You have specified key values that doesn't exist in the parent table. The message indicates the constraint and table so you can find out which tables are involved. I think your problem is the following:

You are using IDENTITY column as primary key. And you have another column that references the identity column in the same table. When you do a DELETE it doesn't reset the seed for identity column. So if you had performed data manipulations before you are going to get new identity value based on the previous generated values and it will not start at 1 or whatever your seed is. TRUNCATE TABLE on the other hand will reset the identity seed to the original value. Or you can also use DBCC CHECKIDENT. Typically when you store this sort of parent child relationship you should have the root's parent as NULL to simplify your operations rather than the root id itself.


Probaj ovo, mislim da ti je greska u bazi a ne u programu.
[ Lanaaa @ 06.07.2010. 16:14 ] @
Citat:
AMD guyhttp://social.msdn.microsoft.c...d-bc2e-4afc-824e-b75f18a8b491/



Probaj ovo, mislim da ti je greska u bazi a ne u programu.


Uspjela sam rijesiti, nije bilo do baze, ipak je do boxa bilo, hvala u svakom slucaju :)