2016-09-06 25 views
0

私はプログラミングの世界とstackflowに新しいので、 'noob'の質問を許してください... ここには(Windowsフォームにリンクされた)このメソッドがあります。ここでのアイデアはテキストボックスに情報を追加することですGridviewとデータベースの両方に追加してください。問題は、追加情報の一部のみが、(IDのウィッヒが自動生成されます)(ID)追加された値はGridviewに表示されませんか? 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は「編集」方式すぎウィッヒがうまく動作するようですしまったに戻って表示されていることですその行が作成されると(IDだけで)作成されます。事前に 私は任意の助けに感謝...感謝をHERES

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

をロードするには、次のコードを使用することができます

、私に知らせて'addAdministrator.ID'のように' addAdministrator'オブジェクトからIDを取得することができます –

+0

Fname、Lname、およびPasswordフィールドはDatabaseに書き込まれていますか? – Pikoh

+0

db.Administrators.Add(addAdministrator)を使用しました。追加のために、このdb.AddToAdministrators(addAdministrator);を試しました;あなたはdbの後にこのオプションを見ましたか? – Waqas

答えて

0

ようIDからaddAdministratorオブジェクトを取得していますが、sのデータを適切にロードするための問題を持っていますo

私が例を共有してみましょう、それはあなたを助けるかもしれません。ないならば、あなたはあなたが `あなたのケースでは...)一度`のSaveChanges(後にモデルクラスからIDを取得することができ、実行することができますdatagridview

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

することはできあなたのケースでは...一度SaveChanges()を実行した後にモデルクラスからIDを取得することができ、することができますあなたは、データベース内のデータを正常に取得するあなたのコメントによると

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

OP質問 – Pikoh

+0

ahはいを理解すると、問題はFname、Lnameとパスワードが保存されていないので、これはOP問題とは関係ありません。申し訳ありませんが、私は少しはっきりしていたはずです...はい、そのFname、Lnameとパスワードを表示したい... IDはすでに表示...申し訳ありません – Joubert

関連する問題