【问题标题】:To check values in datatable and add values to datatable检查数据表中的值并将值添加到数据表
【发布时间】:2014-12-19 11:40:26
【问题描述】:

在下面的代码中,我有一个静态方法,其中我有一个带有值的数据表和另一个空数据表 Orderdbl 并添加 3 列。

现在我的目标是将locationid,productid,quantity 发送到静态方法。
现在我想检查数据表dtGrid 中的productid 是否没有该值,如果它没有该值,它应该检查另一个数据表Orderdbl 然后将值添加到数据表Orderdbl 并将其存储在一个会话。请帮我解决这个问题。

 [WebMethod(EnableSession = true)]
public static void InsertData(string LocationID, string ProductID, string Quantity)
{
   MastersClient objIndent = new MastersClient();
   DataTable dtGrid = (DataTable)HttpContext.Current.Session["VSOrderForm"];
   // DataSet ds = objIndent.CheckForExistingOrder(Int32.Parse(LocationID), ProductID);

   var DataCheck = dtGrid.Select("ProductID = '" + ProductID + "'");
   if (DataCheck.Length != 0)
   {
       // do something...
   }
    //if (ds != null && ds.Tables.Count > 0)
    //{
    //}
    else
    {
        DataTable Orderdbl = new DataTable();
        Orderdbl.Columns.Add("LocationID", typeof(string));
        Orderdbl.Columns.Add("ProductID", typeof(string));
        Orderdbl.Columns.Add("Quantity", typeof(string));
        DataRow row = Orderdbl.NewRow();
        //if (string.IsNullOrEmpty((string)Orderdbl.Rows[i][j].value))
        if (Orderdbl == null)
        {
            row["LocationID"] = LocationID;
            row["ProductID"] = ProductID;

            Orderdbl.Rows.Add(row);
            HttpContext.Current.Session["OrderForm"] = Orderdbl;
        }
        else
        {
            string FilterCond1 = "ProductID=" + ProductID;
            DataRow[] newrow = Orderdbl.Select(FilterCond1);
            if (newrow.Length > 0)
            {
                for (int i = 0; i < newrow.Length; i++)
                {
                    if (newrow[i]["ProductID"].ToString() == ProductID)
                    {
                        // YOUR CODE HERE 
                    }
                }
            }
            else
            {
                row["LocationID"] = LocationID;
                row["ProductID"] = ProductID;

                Orderdbl.Rows.Add(row);
                HttpContext.Current.Session["OrderForm"] = Orderdbl;
            }
        }
    }
}

【问题讨论】:

  • 对不起朋友。这里没有人会为您编写代码。如果您遇到任何奇怪的错误或遇到一些问题,那么人们会帮助您提出他们的想法。
  • @Thangadurai 我写了代码请告诉我是错误
  • @Mairaj Ahmad 首先我试图将值存储在数据表中并在下次存储另一个值时保持会话中数据表计数为 0 我想将所有值存储在数据表中并存储在会话中

标签: c# asp.net


【解决方案1】:

这就是你想要的。如果没有找到值,首先在 dtGrid 表中查找,然后在第二个数据表中查找,如果仍然没有找到,则添加新行

public static void InsertData(string LocationID, string ProductID, string Quantity)
{
    MastersClient objIndent = new MastersClient();
    DataTable dtGrid = (DataTable)HttpContext.Current.Session["VSOrderForm"];
    DataTable Orderdbl = (DataTable)HttpContext.Current.Session["OrderForm"];
    // DataSet ds = objIndent.CheckForExistingOrder(Int32.Parse(LocationID), ProductID);


    //Check in first table
    var DataCheck = dtGrid.Select("ProductID = '" + ProductID + "'");
    if (DataCheck.Length != 0)
    {
        // do something...
    }
    //if (ds != null && ds.Tables.Count > 0)
    //{
    //}
    else
    {
        //Not found in first now check in second talbe
        if (Orderdbl != null)
        {
            string FilterCond1 = "ProductID=" + ProductID;
            DataRow[] newrow = Orderdbl.Select(FilterCond1);
            //If Length > 0 it means found in second table,
            if (newrow.Length > 0)
            {

                for (int i = 0; i < newrow.Length; i++)
                {
                    if (newrow[i]["ProductID"].ToString() == ProductID)
                    {
                        // YOUR CODE HERE 
                    }
                }
            }
            else
            {
                //Not found in second talbe now add new row

                DataRow row = Orderdbl.NewRow();
                //if (string.IsNullOrEmpty((string)Orderdbl.Rows[i][j].value))
                row["LocationID"] = LocationID;
                row["ProductID"] = ProductID;
                Orderdbl.Rows.Add(row);
                HttpContext.Current.Session["OrderForm"] = Orderdbl;
            }
        }
        else
        {
            //This will run first time when session has no value.
            Orderdbl = new DataTable();
            Orderdbl.Columns.Add("LocationID", typeof(string));
            Orderdbl.Columns.Add("ProductID", typeof(string));
            Orderdbl.Columns.Add("Quantity", typeof(string));
            DataRow row = Orderdbl.NewRow();
            //if (string.IsNullOrEmpty((string)Orderdbl.Rows[i][j].value))
            row["LocationID"] = LocationID;
            row["ProductID"] = ProductID;
            Orderdbl.Rows.Add(row);
            HttpContext.Current.Session["OrderForm"] = Orderdbl;
        }


    }
}

【讨论】:

  • 如果没有值,我想检查数据表 dtGrid 中的 productid,如果没有值,它应该检查另一个数据表 Orderdbl,然后将值添加到数据表 Orderdbl 并将其存储在会话
  • 你已经为 Session["OrderForm"] 赋值并且你正在从 Session["VSOrderForm"] 中读取,对吗?
  • 我必须通过传递 productid 来检查会话数据表并存储到 Session["OrderForm"]
  • 每次插入Session["OrderForm"]时数据表Orderdbl计数为0
  • 更新的答案现在检查它是否存在prevoius代码中的一些问题。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-12-07
  • 1970-01-01
  • 2016-01-23
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多