C# 检查datatable中的值并向datatable添加值
在下面的代码中,我有一个静态方法,其中有一个带有值的数据表和另一个空数据表C# 检查datatable中的值并向datatable添加值,c#,asp.net,C#,Asp.net,在下面的代码中,我有一个静态方法,其中有一个带有值的数据表和另一个空数据表Orderdbl,并添加3列 现在我的目标是将locationid、productid、quantity发送到静态方法。 现在我想检查datatabledtGrid中的productid,如果它没有该值,它应该检查另一个datatableOrderdbl如果它没有该值,然后将这些值添加到datatableOrderdbl中,并将其存储在会话中。请帮我解决这个问题 [WebMethod(EnableSession = tr
Orderdbl
,并添加3列
现在我的目标是将locationid、productid、quantity
发送到静态方法。现在我想检查datatable
dtGrid
中的productid
,如果它没有该值,它应该检查另一个datatableOrderdbl
如果它没有该值,然后将这些值添加到datatableOrderdbl
中,并将其存储在会话中。请帮我解决这个问题
[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;
}
}
}
}
[WebMethod(EnableSession=true)]
公共静态void InsertData(字符串位置ID、字符串产品ID、字符串数量)
{
MastersClient objIndent=新的MastersClient();
DataTable dtGrid=(DataTable)HttpContext.Current.Session[“VSOrderForm”];
//数据集ds=objIndent.CheckForExistingOrder(Int32.Parse(LocationID),ProductID);
var DataCheck=dtGrid.Select(“ProductID=”+ProductID+”);
如果(DataCheck.Length!=0)
{
//做点什么。。。
}
//如果(ds!=null&&ds.Tables.Count>0)
//{
//}
其他的
{
DataTable Orderdbl=新DataTable();
Add(“LocationID”,typeof(string));
Add(“ProductID”,typeof(string));
添加(“数量”,类型(字符串));
DataRow row=Orderdbl.NewRow();
//if(string.IsNullOrEmpty((string)Orderdbl.Rows[i][j].value))
if(Orderdbl==null)
{
行[“LocationID”]=LocationID;
行[“ProductID”]=ProductID;
Orderdbl.Rows.Add(行);
HttpContext.Current.Session[“OrderForm”]=Orderdbl;
}
其他的
{
字符串FilterCond1=“ProductID=”+ProductID;
DataRow[]newrow=Orderdbl.Select(FilterCond1);
如果(newrow.Length>0)
{
for(int i=0;i
这是您想要的。如果未找到值,则首先在dtGrid表中查找,然后在第二个datatable中查找,如果仍然未找到,则添加新行
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;
}
}
}
公共静态void InsertData(字符串位置ID、字符串产品ID、字符串数量)
{
MastersClient objIndent=新的MastersClient();
DataTable dtGrid=(DataTable)HttpContext.Current.Session[“VSOrderForm”];
DataTable Orderdbl=(DataTable)HttpContext.Current.Session[“OrderForm”];
//数据集ds=objIndent.CheckForExistingOrder(Int32.Parse(LocationID),ProductID);
//签入第一个表格
var DataCheck=dtGrid.Select(“ProductID=”+ProductID+”);
如果(DataCheck.Length!=0)
{
//做点什么。。。
}
//如果(ds!=null&&ds.Tables.Count>0)
//{
//}
其他的
{
//在第一个中未找到现在签入第二个talbe
if(Orderdbl!=null)
{
字符串FilterCond1=“ProductID=”+ProductID;
DataRow[]newrow=Orderdbl.Select(FilterCond1);
//如果长度>0,则表示在第二个表中找到,
如果(newrow.Length>0)
{
for(int i=0;i
对不起,朋友。这里没有人会为您编写代码。如果您遇到任何奇怪的错误或遇到一些问题,然后人们会在他们的想法上帮助你。@Thangadurai我已经写了代码,请告诉我是什么mistake@MairajAhmad首先,我尝试将值存储在datatable中,并在下次存储另一个值时保持会话状态datatable计数为0我想将所有值存储在datatable中并存储在会话中我想签入productiddatatable dtGrid如果没有该值,则应与另一个datatable Orderdbl检查,如果它没有该值,则将这些值添加到datatable Orderdbl并将其存储在会话中您已将值分配给会话[“OrderForm”],并且正在从会话[“VSOrderForm”]读取是这样吗?我必须通过将productid和store传递到会话[“OrderForm”]来检查会话datatable。每次插入会话[“OrderForm”]时,datatable Orderdbl计数都为0。请立即检查答案。上一个代码中存在一些问题。