0

Hey :) I actually code a dispatch system with a database for the operations. But now i have the problem that i a method the textboxes don't work.

public void Alarmieren(string Einheiten)
{
    MessageBox.Show(Datum.Text);
    SqlConnection con = new SqlConnection("data source = 178.192.159.151; initial catalog = Leitstelle; user id = grls; password = 123");
    try
    {
        con.Open();
        SqlCommand cmd = con.CreateCommand();
        cmd.CommandText = "INSERT INTO einsaetze(Datum, Uhrzeit, Melder, Telefonnummer, Patient, Geburtsdatum, Objekt, Ort, Strasse, Stichwort, Meldung, Einheit) VALUES('" + Datum.Text + "', '" + Uhrzeit.Text + "', '" +     Melder.Text + "', '" + Telefonnummer.Text + "', '" + Patient.Text + "', '" + Geburtsdatum.Text + "', '" + Objekt.Text + "' , '" + Ort.Text + "' , '" + metroTextBox1.Text + "', '" + stichwort.Text + "', '" + Meldung.Text + "', '" + Einheiten + "')";
        cmd.ExecuteNonQuery();
    }
    catch(Exception ex)
    {
        MessageBox.Show(ex.Message, "Message", MessageBoxButtons.OK, MessageBoxIcon.Error);
    }
}

And now i want to call this method from another class:

Einsatzmaske maske = new Einsatzmaske();
maske.Alarmieren(einheiten);

I don't figure out whats wrong. I can use Datum.Text in all methods but in the method Alarmieren it doesn't work.

Hope you can help me guys.

maccettura
  • 10,514
  • 3
  • 28
  • 35
Fl3xy03
  • 1
  • 2
  • Different [scopes](https://msdn.microsoft.com/en-us/library/ms973875.aspx) :) – ProgrammingLlama Jun 21 '18 at 15:40
  • 3
    Your code is _extremely_ vulnerable to SQL Injection attacks. You need to use parameterized queries – maccettura Jun 21 '18 at 15:41
  • 1
    Don't call methods on textboxes from another class, just pass the values into the function: `public void Alarmieren(string Einheiten, string datum, ........)` – DavidG Jun 21 '18 at 15:44
  • I'm not sure what `Einsatzmaske` is but you are creating a new one before calling `Alarmieren` on it. I'm guessing that its a form with textboxes on (ie Datum is a textbox). If so then you are creating a new one which will have its own textboxes. If you want to refer to the textboxes on the original `Einsatzmaske` (assuming there is one) then you'll need to keep a reference to that and use that – Chris Jun 21 '18 at 15:47
  • I don't want to access to a variable in a other class. The method is in the same class like the textboxes. – Fl3xy03 Jun 21 '18 at 15:56
  • OK but it's another instance of the class. Think of a class as a model of car. You can buy one, and I can buy one, but changes I make to mine aren't applied to yours because they aren't the same cars, just the same model. In C# you create instances of classes (a class is the model, the instance is an individual car) – ProgrammingLlama Jun 21 '18 at 16:08
  • 1
    `Einsatzmaske maske = new Einsatzmaske();` creates a new instance, with its own variables in their own memory positions. If the method is in the same class, you should just call `Alarmieren(einheiten);` without all the other stuff. – ProgrammingLlama Jun 21 '18 at 16:09
  • No i want to call Alarmieren(einheiten) from another class. But the problem is in the method Alarmieren(einheiten) all the types Datum.Text and the others don't work. If i look in the database after that all is empty Datum. Text = ""; but there is something inside – Fl3xy03 Jun 21 '18 at 16:40
  • Maybe its important, that this are forms? – Fl3xy03 Jun 21 '18 at 16:46
  • The form that is open (Einsatzmaske) is not the same form as you _create_ using `Einsatzmaske maske = new Einsatzmaske();`. If you want to access the original's variables, you need to pass the information in in some way. See [here](https://stackoverflow.com/questions/22471648/how-to-access-textbox-text-in-another-form) and [here](https://stackoverflow.com/questions/7797975/how-to-refresh-a-form-from-another-form) to get some ideas. – ProgrammingLlama Jun 21 '18 at 23:48

0 Answers0