检查数据表中的值并向数据表添加值

本文关键字:数据表 添加 检查 | 更新日期: 2023-09-27 18:34:39

在下面的代码中,我有一个静态方法,其中我有一个带有值的数据表和另一个空数据表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;
            }
        }
    }
}

检查数据表中的值并向数据表添加值

这是你想要的。如果未找到值,则首先在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;
        }

    }
}