检查数据表中的值并将值添加到datatable

时间:2014-12-19 11:40:26

标签: c# asp.net

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

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

Orderdbl

1 个答案:

答案 0 :(得分:0)

这是你想要的。首先看看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;
        }


    }
}