2017-08-08 51 views
0

使用LINQ to Entities。不是SQLConnections或任何其他方法。linq to entities檢索數據庫表的行值並顯示爲label.Text C#.net

我有一個網頁,我需要在同一個數據庫中顯示一個DataTable中的'ID'和另一個DataTable中相應的'Name'。 ID和名稱應顯示爲lblOrderId.Text = OrderId.ToString();和lblName.Text = Name.ToString();分別。
不一定是一個字符串,我只是想從標籤中的數據庫的值。

因爲它現在我沒有得到任何標籤文本,但如果我使用代碼在這一切的底部,我得到的是訂單和交貨日期。

包含OrderId的數據表有兩列; OrderId是自動生成的,並且位於列0和ProductId中。

DATATABLE含有名稱具有6列,名稱在列1被作爲列0成立自動生成的客戶ID

所有我需要的是最後添加的名稱和ID將被顯示爲標籤。

我一直在研究這個網站和其他人在過去的5個小時,似乎無法找到一個適合我的需求的解決方案。這是所有的SQL連接和一個稀疏的數據庫值的文本框。

請參閱下面的代碼,我已經取得了什麼,如果您需要任何進一步的編碼或信息,請不要猶豫,問。

非常感謝您提供任何建議或幫助。

ASPX.CS(後端)

protected void Page_Load(object sender, EventArgs e) 
    { 
     using (Entities myEntities = new Entities()) 
     { 
      int OrderId = // Not sure what to put here, but if I don't have it I get the error *The name OrderId does not exist in the current context* 
      var orderId = (from ord in myEntities.Orders 
          where ord.OrderId.Equals(OrderId) // Error here 
          select ord).Last();     

      lblOrderID.ForeColor = Color.Red; 
      lblOrderID.Text = orderId.ToString(); 

面對與關於名稱下面的方法完全相同的錯誤。這兩種方法都包含在相同的使用實體{}大括號內。

var name = (from ord in myEntities.Customer 
         where ord.Name.Equals(Name) 
         select ord).Last(); 

      lblName.ForeColor = Color.Red; 
      lblName.Text = name.ToString(); 

合,但仍然在Page_Load {大括號我有以下代碼以添加訂單日期和交貨日期所看到下面

 DateTime date = DateTime.Now; 
     DateTime DeliveryDate = date.AddDays(5); 

     lblDate.ForeColor = Color.Red; 
     lblDeliveryDate.ForeColor = Color.Red; 

     lblDate.Text = date.ToLongDateString(); 
     lblDeliveryDate.Text = DeliveryDate.ToLongDateString(); 

全部塊內}的使用實體{}波形括號的Page_Load

protected void Page_Load(object sender, EventArgs e) 
    { 
     using (Entities myEntities = new Entities()) 
     { 
      //int OrderId = Convert.ToInt32(); // Not sure what to put here, but if I don't have it I get the error *The name OrderId does not exist in the current context* 

      var orderId = (from ord in myEntities.Orders 
          where ord.OrderId.Equals(OrderId)// Error here 
          select ord).Last(); 

      lblOrderID.ForeColor = Color.Red; 
      lblOrderID.Text = orderId.ToString(); 


      var name = (from ord in myEntities.Customer 
         where ord.Name.Equals(Name) 
         select ord).Last(); 

      lblName.ForeColor = Color.Red; 
      lblName.Text = name.ToString(); 
     } 

     DateTime date = DateTime.Now; 
     DateTime DeliveryDate = date.AddDays(5); 

     lblDate.ForeColor = Color.Red; 
     lblDeliveryDate.ForeColor = Color.Red; 

     lblDate.Text = date.ToLongDateString(); 
     lblDeliveryDate.Text = DeliveryDate.ToLongDateString(); 
    } 

ASPX(前端)

<p> 
      Hi 
     <asp:Label ID="lblName" runat="server" Text=""></asp:Label> 
      Thank you for shopping at Cineplex.<br /> 
      <br /> 

      Your Order ID number is: 
     <asp:Label ID="lblOrderID" runat="server" Text=""></asp:Label><br /> 
      <br /> 

      Your order was placed on: 
     <asp:Label ID="lblDate" runat="server" Text=""></asp:Label><br /> 
      <br /> 

      Delivery date: 
     <asp:Label ID="lblDeliveryDate" runat="server" Text=""></asp:Label> 
     </p> 

對名稱和訂單ID進行硬編碼並刪除實體和查詢可以在後端使用下面的代碼看到,但是這是不可接受的。客戶在購買成功後需要查看他們的訂單ID。
這裏唯一的安慰是我在客戶端可以讀取的所有四個標籤中獲得一個值。

protected void Page_Load(object sender, EventArgs e) 
    { 
     lblName.ForeColor = Color.Red; 
     lblName.Text = "User"; 

     lblOrderID.ForeColor = Color.Red; 
     lblOrderID.Text = "1"; 

     DateTime date = DateTime.Now; 
     DateTime DeliveryDate = date.AddDays(5); 

     lblDate.ForeColor = Color.Red; 
     lblDeliveryDate.ForeColor = Color.Red; 

     lblDate.Text = date.ToLongDateString(); 
     lblDeliveryDate.Text = DeliveryDate.ToLongDateString(); 
    } 

回答

0

你的問題都是關於查詢數據庫中的東西。你可以從查詢中刪除所有ASP的東西,因爲它只會分散你的問題。

相反,您應該向我們展示實體類(Order,Customer)。據我所知,您需要Order.OrderIdCustomer.Name(訂購的客戶)訂購特定訂單,其中您不知道如何識別訂單。

假設你去與通常的客戶/訂單的樣品模型,您的實體必須看大約是這樣的:

public class Customer 
{ 
    public int CustomerId; // identifies a customer 
    public string Name; // the customer's name 
    public ICollection<Order> Orders; // collection of customer's orders 
} 

public class Order 
{ 
    public int OrderId; // identifies an order 
    public int CustomerId { get; } // ID of the customer who's order it is. 
    public Customer Customer { get; } // The Customer entity of that customer 
} 

在Page_Load裏面(離開走什麼是不相關的)

// 1st find out the ID of the one and only order the page is about 
int orderId = 4711; // can't help here - where do you intend to get it from? 

// 2nd retrieve from DB whatever we need later 
string customerName; 
using (var myEntities = new Entities()) 
{ 
    var orderInfo = (
     from order in myEntities.Orders 
     where order.OrderId == orderId 
     select new { CustomerName = order.Customer.Name } 
     ).SingleOrDefault(); 
    if (orderInfo == null) // error handling if orderId is bad 
     throw new Exception("No such order found."); 
    customerName = orderInfo.CustomerName; 
} 

// 3rd use what we know to update controls 
lblName.Text = customerName; 
+0

謝謝你的迴應。真的非常感謝您的意見,提出一個更簡潔的問題。至於int orderId = 4711; //在這裏無法幫助 - 你打算從哪裏得到它?我打算從我在數據庫中的訂單表中檢索它。訂單表包含自動生成的OrderId和CustomerId以及訂單的放置日期。 –

0

結果。 我在兩個不同的會話保存的初始變量,在那裏他們被像這樣創建的aspx.cs頁面上:

int custID = Cust.CustomerId; 
Session["CustID"] = Cust.CustomerId; 

int newOrderID = ord.OrderId; 
Session["newOrderID"] = ord.OrderId; 

然後aspx.cs頁面上,我想查詢數據庫表我做了以下內容:

protected void Page_Load(object sender, EventArgs e) 
    { 
     if (Session["CustID"] != null) 
     { 
      int CID = (int)Session["CustID"]; 

      using (var myEntities = new Entities()) 
      { 
       var CustName = (from customer in myEntities.Customer 
           where customer.CustomerId == (CID) 
           select customer.Name).SingleOrDefault(); 
       if (CustName == null) // error handling if customer name does not exist 
        throw new Exception("No such Customer found."); 
       lblName.ForeColor = Color.Red; 
       lblName.Text = CustName; 
      } 
     } 
     if (Session["newOrderID"] != null) 
     { 
      int OID = (int)Session["CustID"]; 

      using (var myEntities = new Entities()) 
      { 
       var OrderNum = (from order in myEntities.Orders 
           where order.OrderId == (OID) 
           select order.OrderId).SingleOrDefault(); 
       if (OrderNum == 0) // error handling if customer name does not exist 
        throw new Exception("No such Order found."); 
       lblOrderID.ForeColor = Color.Red; 
       lblOrderID.Text = OrderNum.ToString(); 
      } 
     } 

     DateTime date = DateTime.Now; 
     DateTime DeliveryDate = date.AddDays(5); 

     lblDate.ForeColor = Color.Red; 
     lblDeliveryDate.ForeColor = Color.Red; 

     lblDate.Text = date.ToLongDateString(); 
     lblDeliveryDate.Text = DeliveryDate.ToLongDateString(); 
    } 

謝謝你們的建議和建議。非常感激。

+0

(1)CustomerId是多餘的:如果您在Session中擁有OrderId,則給出CustomerId。 (2)使用EF的力量在一個查詢中獲得所有需要,類似於我的做法。 – tinudu

+0

謝謝Tinudu。我已經設法解決了這個問題,但將來我會更接近你的方法,因爲它是有道理的。編寫不必要的代碼不僅是及時的,而且可以讓錯誤進入並丟失。 –