0

在数据库中,每个 ID 应该有多个项目。

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 的多个项目。我怎么才能得到它?

4

2 回答 2

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;
                  }
                }
              }
            }
于 2012-09-17T11:28:43.643 回答
0

您正在Order为每一行创建一个对象:Order order = new Order()在您的循环内。什么类型order.items?那不应该是一个集合之类的List<OrderItem>东西吗?

于 2012-09-17T11:15:15.403 回答