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;
}
}
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 –
Sind die Felder Fname, Lname und Passwort in die Datenbank geschrieben? – Pikoh
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