2016-09-06 2 views
0

Ich bin neu in der Programmierwelt und Stackflow, also bitte vergeben Sie die 'noob' Frage ... Ich habe diese Methode hier (mit Windows-Formular verknüpft) und die Idee hier ist, Informationen zu den Textfeldern hinzufügen und habe es sowohl zur Gridview als auch zur Datenbank hinzugefügt. das Problem ist, dass nur ein Teil (die ID Weicht wird automatisch generiert) die Information hinzugefügt wird, auf der (der ID) zurück angezeigt wirdDie hinzugefügten Werte werden nicht in Gridview angezeigt? C#

private void btnAddNewAdmin_Click(object sender, EventArgs e) 
     { 
      if (!(txtAdminFname.Text == string.Empty || txtAdminLname.Text == string.Empty || 
        txtPassword.Text == string.Empty)) 
      { 
       MessageBox.Show("ERROR: all the boxes have to be filled and try again."); 
      } 
      // Here it will ADD a new Admin based on the information entered in the form, the New ID for the Admin is generated by the program. 
       try 
       { 
       using (Hawthor_HS_Entities db = new Hawthor_HS_Entities()) 
       { 
        Administrator addAdministrator = new Administrator();// currently adding only the AID and nothing else. 
        { 
         addAdministrator.FName = txtAdminFname.Text; 
         addAdministrator.LName = txtAdminLname.Text; 
         addAdministrator.Password = txtNewPass.Text; 
        } 

         db.Administrators.Add(addAdministrator); 
         db.SaveChanges(); 

         DataGridLoad(); 

         MessageBox.Show("Success! New Administrator was Added"); 
        } 

       } 
       catch (Exception) 
       { 
        MessageBox.Show("Error: Could not Add new Administrator. Please try again."); 
       } 
      } 

ive bekam eine „Edit“ Methode zu Wich scheint ganz gut zu funktionieren sobald diese Zeile erstellt wurde (nur mit der ID). schätzen ich Hilfe ... Vielen Dank im Voraus

heren die DataGridLoad()

private void DataGridLoad() 
     { 
      using (Hawthor_HS_Entities db = new Hawthor_HS_Entities()) 
      { 
       // Loads information into the DataGridView from the Administrators db. 
       var adminLIST = (from c in db.Administrators 
        select new 
        { 
         AID = c.AID, 
         FirstName = c.FName, 
         LastName = c.LName 
        }).ToList(); 

       ADDdataGridView.DataSource = adminLIST; 


       // loads information into the AdminID combo box in the EDIT tab. 
       cboAdminID.DisplayMember = "AID"; 
       cboAdminID.ValueMember = "AID"; 
       cboAdminID.DataSource = adminLIST; 

       //loads information into the AID combo box in the DELETE tab. 
       cboAID.DisplayMember = "AID"; 
       cboAID.ValueMember = "AID"; 
       cboAID.DataSource = adminLIST; 
      } 
     } 
+0

Sie können in der Lage zu bekommen die ID aus der Modellklasse einmal nach 'Savechanges()' ausgeführt .. In Ihrem Fall zu laden , können Sie die ID von 'addAdministrator'-Objekt wie' addAdministrator.ID' erhalten –

+0

Sind die Felder Fname, Lname und Passwort in die Datenbank geschrieben? – Pikoh

+0

Sie haben db.Administrators.Add (addAdministrator) verwendet; zum Hinzufügen, haben Sie versucht, diese db.AddToAdministrators (addAdministrator);, Haben Sie diese Option nach db gesehen? – Waqas

Antwort

0

nach Ihrem Kommentar, dass Sie Daten erfolgreich in der Datenbank erhalten, aber Sie haben ein Problem, Daten ordnungsgemäß geladen wird, s o

Lassen Sie mich ein Beispiel teilen, vielleicht wird es Ihnen helfen. wenn nicht, dann lass es mich wissen,

Sie folgenden Code verwenden können, um datagridview

var context=new Hawthor_HS_Entities(); 
BindingSource bi = new BindingSource(); 
bi.DataSource = context.Administrators; 
dataGridView1.DataSource = bi; 
dataGridView1.Refresh(); 
0

Sie können wieder in der Lage, die ID von der Modellklasse zu erhalten, nachdem SaveChanges() ausgeführt .. In Ihrem Fall können Sie holen Sie sich das ID von addAdministrator Objekt wie

try 
{ 
    using (Hawthor_HS_Entities db = new Hawthor_HS_Entities()) 
    { 
     Administrator addAdministrator = new Administrator();// currently adding only the AID and nothing else. 
     { 
      addAdministrator.FName = txtAdminFname.Text; 
      addAdministrator.LName = txtAdminLname.Text; 
      addAdministrator.Password = txtNewPass.Text; 
     } 

     db.Administrators.Add(addAdministrator); 
     db.SaveChanges(); 
     DataGridLoad(); 
     int Id = addAdministrator.ID; // you can able to get the id for the record you attempt to save. 
     MessageBox.Show("Success! New Administrator was Added"); 
    } 
} 
catch (Exception) 
{ 
    MessageBox.Show("Error: Could not Add new Administrator. Please try again."); 
} 
+0

Ich habe keine Sache, dies hat etwas mit OP-Problem zu tun, da das Problem ist, dass Fname, Lname und Passwort nicht gespeichert werden, wenn ich OP Frage verstehen – Pikoh

+0

ah ja ... Entschuldigung, ich hätte ein wenig klarer sein sollen ... ja, es ist der Fname, Lname und Passwort, die ich angezeigt werden möchte ... die ID zeigt schon ... sorry – Joubert

Verwandte Themen