2012-09-17 57 views
0

在數據庫中,每個ID應該有多個項目。通過單擊按鈕將多個值插入數據庫中使用enity框架選擇複選框

protected void Button1_Click(object sender, EventArgs e) 
{ 
    using (CMSEntities cmsmodel = new CMSEntities()) 
    { 
    if (GridView1.Rows.Count > 0) 
    { 
     foreach (GridViewRow row in GridView1.Rows) 
     { 
     Order order = new Order(); 

     TextBox box1 = (TextBox)row.FindControl("itemTextBox"); 
     TextBox box2 = (TextBox)row.FindControl("priceTextBox"); 
     TextBox box3 = (TextBox)row.FindControl("txtQuantity"); 
     CheckBox chkBx = (CheckBox)row.FindControl("ChkAddToOrder"); 

     if (chkBx.Checked) 
     { 
      order.items = (box1.Text); 
      order.price = Convert.ToInt32(box2.Text); 
      order.quantity = Convert.ToInt32(box3.Text); 
      order.dateoforder = Convert.ToDateTime(DateTime.Now.ToString()); 

      var types = (from p in cmsmodel.Items 
         //join w in cmsmodel.Orders on p.items equals order.items 
         where p.items == order.items 
         select p.itemid).FirstOrDefault(); 
      order.itemid = Convert.ToInt32(types); 
      String name = (String)Session["UserDetails"]; 

      var types2 = (from p in cmsmodel.Logins 
         //join w in cmsmodel.Orders on p.UserName equals name 
         where p.UserName == name 
         select p.ID).FirstOrDefault(); 

      order.empid = types2; 
      //string result="NotFullfill"; 
      //var type3 = (from p in cmsmodel.Status 
      //    where p.result== result 
      //    select p.statusid); 

      order.statusid = 2; 

      int a = Convert.ToInt32(box2.Text); 
      int b = Convert.ToInt32(box3.Text); 
      int totalprice = a * b; 
      order.totalprice = totalprice; 

      cmsmodel.Orders.AddObject(order); 
      cmsmodel.SaveChanges();  
     }    

     Label1.Text = "inserted sucessfully"; 
     Label1.Visible = true; 
     } 
    } 
    } 
} 

但我得到每個ID只有一個項目,我應該得到多個項目與每個ID。我怎麼才能得到它?

+0

正在等待響應 – user1677287

回答

0

您正在爲每一行創建一個Order對象:Order order = new Order()位於您的循環內部。什麼類型的是order.items?應該不是像List<OrderItem>之類的集合呢?

+0

項是在數據的基礎列,以便順序訪問它,我已經寫像order.items – user1677287

+0

如果我不能寫入順序次序=新訂單()環內可以ü一個對象請告訴我在哪裏可以寫訂單order = new Order()這一個,這樣它就可以工作 – user1677287

+0

等待你的迴應 – user1677287

0
Please try this ....   
protected void Button1_Click(object sender, EventArgs e) 
      { 
       using (CMSEntities cmsmodel = new CMSEntities()) 
       { 
       if (GridView1.Rows.Count > 0) 
       { 
        foreach (GridViewRow row in GridView1.Rows) 
        { 
        Order order = new Order(); 

        TextBox box1 = (TextBox)row.FindControl("itemTextBox"); 
        TextBox box2 = (TextBox)row.FindControl("priceTextBox"); 
        TextBox box3 = (TextBox)row.FindControl("txtQuantity"); 
        CheckBox chkBx = (CheckBox)row.FindControl("ChkAddToOrder"); 

        if (chkBx.Checked) 
        { 
         order.items = (box1.Text); 
         order.price = Convert.ToInt32(box2.Text); 
         order.quantity = Convert.ToInt32(box3.Text); 
         order.dateoforder = Convert.ToDateTime(DateTime.Now.ToString()); 


         String name = (String)Session["UserDetails"]; 

         var types2 = (from p in cmsmodel.Logins 
            //join w in cmsmodel.Orders on p.UserName equals name 
            where p.UserName == name 
            select p.ID).FirstOrDefault(); 

         order.empid = types2; 
         //string result="NotFullfill"; 
         //var type3 = (from p in cmsmodel.Status 
         //    where p.result== result 
         //    select p.statusid); 

         order.statusid = 2; 

         int a = Convert.ToInt32(box2.Text); 
         int b = Convert.ToInt32(box3.Text); 
         int totalprice = a * b; 
         order.totalprice = totalprice; 
      var types = (from p in cmsmodel.Items 
            //join w in cmsmodel.Orders on p.items equals order.items 
            where p.items == order.items 
            select p.itemid).ToList(); 
     foreach(var x in types) 
     { 
     order.itemid = Convert.ToInt32(x); 
     cmsmodel.Orders.AddObject(order); 
     } 
         cmsmodel.SaveChanges();  
        }    

        Label1.Text = "inserted sucessfully"; 
        Label1.Visible = true; 
        } 
       } 
       } 
      } 
+0

嗨thanx的答覆,但我也獲得輸出,但在SQL數據庫中他們是訂單ID作爲主鍵,我們不能創建重複的值,所以我創建了新的列呼叫ID,以便我可以得到重複值相同的ID號碼,多行可以插入,所以我如何插入這樣的請告訴我,每個ID號應該有多個值 – user1677287

+0

等待您的回覆 – user1677287

相關問題