2014-04-12 25 views
1

我非常新的ASP.NET C# 我有一點問題,並希望有人可以幫我請。Asp.net不能類型「字符串」轉換爲System.Data.DataRowView」

什麼我想要做的就是使用c#從下拉列表中自動填充asp.net中的一些文本框,當他們點擊列表並選擇零售商時,文本框將自動填充來自Access數據庫的匹配信息。

下面是我的代碼,到目前爲止,這是一個有點亂,但是那是因爲我還在寫它。

namespace ChocoMamboAsp 
{ 
    public partial class SalesOrderForm : System.Web.UI.UserControl 
    { 
     SalesOrder _order = null; 


     protected void Page_Load(object sender, EventArgs e) 
     { 
      if (System.Web.HttpContext.Current.Session["SalesOrderID"] == null) 
      { 
       _order = new SalesOrder(); 
       populateCustomerCombo(); 
       populateEmployeeCombo(); 
       populateProductCombo(); 

      } 
      else 
      { 
       _order = new SalesOrder(long.Parse(System.Web.HttpContext.Current.Session["SalesOrderID"].ToString())); 
       populateCustomerCombo(); 
       populateEmployeeCombo(); 
       populateProductCombo(); 

       displayRecord(); 


      } 

     } 

     #region Mutators 
     /// <summary> 
     /// Pre-Condtion: Event Call 
     /// Post-Condition: calculates the item total 
     /// Description: Calculates the line total for each item in the grid list 
     /// </summary> 
     private void calculateLineTotal() 
     { 
      int intQty = 0; 
      decimal decPrice = 0.0M; 
      var total = 0; 
      foreach (GridViewRow row in dgvOrderLine.Rows) 
      { 
       var numberLabel = row.FindControl("LineTotal") as Label; 
       int number; 
       if (int.TryParse(numberLabel.Text, out number)) 
       { 
        total += number; 
       } 
      } 

      decimal decLineTotal = decPrice * intQty; 
      txtSubTotal.Text = decLineTotal.ToString("c2"); 
      calculateGrandTotal(); 
     } 

     /// <summary> 
     /// Pre-Condtion: Events call 
     /// Post-Condition: Calculate the total of all the items 
     /// Description: This will calculate the total of all the items in the list area. 
     /// </summary> 
     private void calculateGrandTotal() 
     { 
      try 
      { 
       decimal decGrandTotal = decimal.Parse(_order.getOrderLinesTable().Compute 
                 ("Sum(LineTotal)", "").ToString()); 
       lblTotal.Text = decGrandTotal.ToString("c2"); 
      } 
      catch (FormatException) 
      { 
       //this exception will occur if tblOrderLine is empty which we can safely ignore 
      } 
     } 


     #endregion 

     protected void btnInsert_Click(object sender, EventArgs e) 
     { 

       assignChildData(); 
       _order.OrderLineClass.addNewRecord(); 
       calculateGrandTotal(); 
       emptyControls(); 

     } 

     #region Accessors 
     /// <summary> 
     /// Pre-Condition: Second Constructor Call 
     /// Post-Condition: Gettes the information from the class getters and setters 
     /// Description: This method gets all the information relating to the table database that has been used. 
     /// </summary>  
     private void displayRecord() 
     { 
      cboRetailer.SelectedValue = _order.RetailerID.ToString(); 
      dtpSalesOrder.Text = _order.SalesDate.ToString(); 
      cboSalesAgent.SelectedValue = _order.EmployeeID.ToString(); 
      txtRetailerAddress.Text = _order.RetailerAddress; 
      txtRetailerPhone.Text = _order.RetailerPhone; 
      lblTotal.Text = _order.SaleTotal.ToString("c2"); 
      dgvOrderLine.DataSource = _order.getOrderLinesTable(); 
      dgvOrderLine.DataBind(); 

      System.Diagnostics.Debug.WriteLine(_order.getOrderLinesTable()); 
     } 
     /// <summary> 
     /// Pre-Condition: Construtor's call 
     /// Post-Condition:Populates the combo box with the requested information 
     /// Description: This method uses the method from the class in order to populate the combo box with the selcted information 
     ///    To display one or more Items. 
     /// </summary> 
     private void populateCustomerCombo() 
     { 
      cboRetailer.DataSource = _order.getCustomers(); 
      cboRetailer.DataValueField = "RetailerID"; 
      cboRetailer.DataTextField = "RetailerName"; 
      cboRetailer.DataBind(); 

     } 
     /// <summary> 
     /// Pre-Condition: Construtor's call 
     /// Post-Condition:Populates the combo box with the requested information 
     /// Description: This method uses the method from the class in order to populate the combo box with the selcted information 
     ///    To display one or more Items. 
     /// </summary> 
     private void populateEmployeeCombo() 
     { 
      cboSalesAgent.DataSource = _order.getEmployee(); 
      cboSalesAgent.DataValueField = "EmployesID"; 
      cboSalesAgent.DataTextField = "EmployeeFirstName"; 
      cboSalesAgent.DataBind(); 

     } 
     /// <summary> 
     /// Pre-Condition: Construtor's call 
     /// Post-Condition:Populates the combo box with the requested information 
     /// Description: This method uses the method from the class in order to populate the combo box with the selcted information 
     ///    To display one or more Items. 
     /// </summary> 
     private void populateProductCombo() 
     { 

      cboProducts.DataSource = _order.getProducts(); 
      cboProducts.DataTextField = "ProdustsName"; 
      cboProducts.DataValueField = "ProductsID"; 
      cboProducts.SelectedIndex = -1;//will make the combo box select nothing 
      cboProducts.DataBind(); 

     } 


     /// <summary> 
     /// Pre-Condition: Event method call 
     /// Post-Condition: Empties fields 
     /// Description: Method used to empty all the fields 
     /// </summary> 
     private void emptyTopControls() 
     { 
      cboSalesAgent.SelectedIndex = -1; 
      cboRetailer.SelectedIndex = -1; 
      txtRetailerAddress.Text = ""; 
      txtRetailerPhone.Text = ""; 
     } 

     //mutators 
     /// <summary> 
     /// Pre-Condition: Event call Method 
     /// Post-Condition: Empties the controls for the lower section 
     /// Description: This will empty the controls of the lower section so the user can in put another item 
     ///     In side the DataTable of the gridView we are casting the datasource as a data table in order 
     ///     for us to delete the information inside each of the rows of the data table. 
     /// </summary> 
     private void emptyControls() 
     { 
      cboProducts.SelectedIndex = -1; 
      txtPrice.Text = ""; 
      txtQty.Text = ""; 
      txtSubTotal.Text = string.Empty; 
      btnInsert.Enabled = true; 
      btnUpdate.Enabled = false; 
      dgvOrderLine.DataSource = _order.getOrderLinesTable(); 
     } 
     /// <summary> 
     /// Pre-Condtion: Event Call 
     /// Post-Condition: Send information to the list grid 
     /// Description: This method will send the infromation from the selection side into the display list 
     /// </summary> 
     private void assignChildData() 
     { 
      _order.OrderLineClass.ProductsID = long.Parse(cboProducts.SelectedValue.ToString()); 
      _order.OrderLineClass.Qty = long.Parse(txtQty.Text); 
      _order.OrderLineClass.Price = decimal.Parse(txtPrice.Text.Substring 
                  (txtPrice.Text.IndexOf('$') + 1)); 
      _order.OrderLineClass.SalesOrderID = _order.PKID; 
      _order.OrderLineClass.LineTotal = decimal.Parse(txtSubTotal.Text); 
      _order.OrderLineClass.ProdustsName = cboProducts.Text; 
     } 


     /// <summary> 
     /// Pre-Condition: Event Call 
     /// Post-Condition: Assings informatin to the text/combo fields 
     /// Description: This is called when ever the fields have information that needs displayed 
     /// </summary> 
     private void assignData() 
     { 
      DateTime date = Convert.ToDateTime(dtpSalesOrder.Text); 
      _order.RetailerID = long.Parse(cboRetailer.SelectedValue.ToString()); 
      _order.SalesDate = date; 
      _order.EmployeeID = long.Parse(cboSalesAgent.SelectedValue.ToString()); 
      _order.RetailerAddress = txtRetailerAddress.Text; 
      _order.RetailerPhone = txtRetailerPhone.Text; 
      _order.SaleTotal = decimal.Parse(lblTotal.Text.Substring 
               (lblTotal.Text.IndexOf('$') + 1)); 
     } 
     #endregion 

     protected void cboRetailer_SelectedIndexChanged(object sender, EventArgs e) 
     { 

      DataRowView drvCost = (DataRowView)cboRetailer.SelectedValue; 
      txtRetailerAddress.Text = drvCost["RetailerAddress"].ToString(); 
      txtRetailerPhone.Text = drvCost["RetailerPhone"].ToString(); 
     } 
    } 

回答

1

cboRetailer.SelectedValue是一個字符串,不能將其轉換爲DataRowView

你可以嘗試這樣的事情:

編輯:

如果GetCustomers的()返回一個數據表,你可以這樣做:

protected void cboRetailer_SelectedIndexChanged(object sender, EventArgs e) 
{ 
    var dt = _order.getCustomers(); 

    for (int i = 0; i < dt.Rows.Count; i++) 
    { 
     if (dt.Rows[i]["RetailerID"].ToString() == cboRetailer.SelectedValue) 
     { 
      txtRetailerAddress.Text = dt.Rows[i]["RetailerAddress"].ToString(); 
      txtRetailerPhone.Text = dt.Rows[i]["RetailerPhone"].ToString(); 

     } 
    } 

} 

或者,如果GetCustomers的()返回IEmunerable<Customer>,你可以這樣做:

protected void cboRetailer_SelectedIndexChanged(object sender, EventArgs e) 
{ 
    int retailerID = 0; 
    if(int.TryParse(cboRetailer.SelectedValue, out retailerID) && retailerID > 0) 
    { 
     var retailer = _order.getCustomers().Where(x => x.RetailerID == retailerID).FirstOrDefault(); 
     if (retailer != null) 
     { 
      txtRetailerAddress.Text = retailer.RetailerAddress; 
      txtRetailerPhone.Text = retailer.RetailerPhone; 
     } 
    } 
} 
+0

嗨謝謝你的回覆,不確定我是否遵循你在做的這裏我把你給m代碼的樣本通過這個錯誤,但它通過這個錯誤Error 'System.Data.DataTable'不包含定義爲'Where'和沒有擴展方法'Where'接受類型'System.Data.DataTable'的第一個參數可以找到(你是否缺少using指令或程序集引用?)我需要一個訪問選擇方法在那裏或者我錯過了一些對不起,想再明白你在做什麼 – user1744348

+0

GetCustomers返回什麼?它是否返回一個DataTable?我編輯了我的答案。 – afzalulh

+0

非常感謝你,我可以理解,這是非常有用的,我看到了定義的位置,不知道那是什麼。但拉入數據表,然後在表中循環,以匹配下拉列表中選定的值,然後在該行填充真棒謝謝你的幫助。 – user1744348

相關問題