Autor Beitrag
27sharp
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 17



BeitragVerfasst: Mo 27.05.13 22:12 
Hallo Leute ,

also ich habe einen Wert von meiner Datenbank in ein Label geholt und den habe ich in eine andere Form mitgegeben.

ausblenden C#-Quelltext
1:
2:
3:
4:
  public string _label1
        {
            get { return Preis.Text; }
        }


So soweit alles in Ordnung.
Jetzt wollte ich mit diesem Wert Rechnen also Subtrahieren.

ausblenden C#-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
         int z1 = 0 ;
            int z2 = 0;

         


            z1 = int.Parse(label3.Text);
            z2 = int.Parse(label7.Text);

            int erg = z1 - z2;

            label8.Text = erg.ToString();


Nun kriege ich die folgende fehlermeldung bei Label3.Text (Wert von Datenbank)

ausblenden C#-Quelltext
1:
Die Eingabezeichenfolge hat das falsche Format.					


Woran kann das liegen ?

Vielen Dank im voraus!
jfheins
ontopic starontopic starontopic starontopic starontopic starontopic starofftopic starofftopic star
Beiträge: 918
Erhaltene Danke: 158

Win 10
VS 2013, VS2015
BeitragVerfasst: Mo 27.05.13 22:21 
Die .Parse() Methode schlägt fehl, weil der übergebene string kein integer ist.

Für eine Analyse warum dass so ist, brauchen wir schon genauere Angaben. Zum Beispiel den genauen String, den du da konvertierst.
27sharp Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 17



BeitragVerfasst: Di 28.05.13 13:28 
Ja also der string wird so erstellt:

ausblenden C#-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
     SQLiteConnection connection = new SQLiteConnection("Data Source=Database.dat");
                string Query = "select * from Kasse where (Seriennumer= " + textBox1.Text + ")";
                SQLiteCommand cmd = new SQLiteCommand(Query, connection);
                SQLiteDataReader reader;

                try
                {

                    connection.Open();
                    reader = cmd.ExecuteReader();

                    while (reader.Read())
                    {
                        Artikel.Text = reader["Artikelbeschreibung"].ToString();
                        pr = reader["Preis"].ToString() + " €";
                        Preis.Text = pr;


                    }


Preis.text ist Label3 , also wird zu einer anderen Form transportiert.
Th69
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Moderator
Beiträge: 4803
Erhaltene Danke: 1060

Win10
C#, C++ (VS 2017/19/22)
BeitragVerfasst: Di 28.05.13 13:33 
Und da ist der Fehler:
ausblenden C#-Quelltext
1:
reader["Preis"].ToString() + " €"					

int.Parse interpretiert den ganzen Text als Zahl und das Leer- sowie Eurozeichen führen daher zu der Format-Exception.
27sharp Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 17



BeitragVerfasst: Di 28.05.13 13:35 
OHH Man ..

Vielen Dank nochmal .. wie kann man das nur übersehen :) :oops:
Ralf Jansen
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 4708
Erhaltene Danke: 991


VS2010 Pro, VS2012 Pro, VS2013 Pro, VS2015 Pro, Delphi 7 Pro
BeitragVerfasst: Di 28.05.13 13:37 
Wenn da ein € Zeichen drin steckt ist es nicht mehr als Zahl interpretierbar. Einen Wert in einem Control zu verwahren den man später mal für irgendwas braucht ist auch, sagen wir mal, mehr als suboptimal. Das kann nur regelmäßig in die Hose gehen. Denk dir eine Property, Klasse für deine Daten aus die das als korrekten (verarbeitbaren) Datentyp vorhalten und verwandel das Ganze nur in einen String wenn du es irgendwo anzeigen mußt. Wenn du denn Wert für was anderes brauchst wende die nicht an die UI sondern an dein Datenmodel dahinter. Denn wenn keine Umwandlung Typ->String->Typ nötig ist hast du auch keine Probleme damit.


Ich finde es übrigens schräg ein Control Preis zu nehmen (Der Control name ist was Klasseninternes) die Veröffentlichung als Property aber Label. Jemand anderes der das benutzen muß wird wohl schwer verwirrt sein. Und du selbst auch wenn du da mal wieder ran mußt wenn der Code ein paar Monate mal rumgelegen hat.