简体   繁体   中英

Filter data from datatable for one of columns in asp.net

I have a datatable which fetches some records. So there is one column name as UPDATED_STATUS . In that column either Pre Hoto or Post Hoto value will come.

So what I want is, Either any one of those values should be their in that column then only the it should move ahead otherwise it should prompt alert as

Either Pre Hoto or Post Hoto can be their

Below is sample image for reference

IMG

Below is the code for getting the datatable with the UPDATED_STATUS column

if (strFlag == "")
                    {
                        dtStatus = GET_STATUS_FROM_SAPID_FOR_HOTO(dtExcelRows.Rows[i]["Current SAPID"].ToString());

                        if (dtStatus == null && dtStatus.Rows.Count < 0)
                        {
                            ClientScript.RegisterStartupScript(Page.GetType(), "erroralert", "alert('Status cannot be blank for SAP ID entered');", true);
                        }
                        else
                        {
                            dtExcelRows.Rows[i]["UPDATED_STATUS"] = dtStatus.Rows[0][1].ToString();
                            dtExcelRows.AcceptChanges();
                        }
                    }

Your current check ( if (dtStatus == null && dtStatus.Rows.Count < 0) ) is wrong:

  1. when dtStatus is null, you continue checking dtStatus.Rows, which throws a nullref exception (you just found out that it was null);
  2. Rows.Count is never less than zero.

Try if (dtStatus == null || dtStatus.Rows.Count == 0) to check whether there is no status at all (it is null ) or no status rows (count is zero). The || will prevent checking for dtStatus.Rows when it was found that dtStatus is null.

&& means that both sides must be true at the same time.
|| means that at least of the sides must be true (both true is also fine).
Both don't evaluate the second test when the first already decided the outcome (false && whatever is always false, true || whatever is always true)

Are you looking for like this !

foreach (DataRow row in dtStatus.Rows)
{
  if (string.IsNullOrEmpty(Convert.ToString(row["UPDATED_STATUS"])) || 
     (Convert.ToString(row["UPDATED_STATUS"]).ToLower() != "pre hoto" && 
     Convert.ToString(row["UPDATED_STATUS"]).ToLower() != "post hoto"))
     {
          ClientScript.RegisterStartupScript(Page.GetType(), "erroralert", "alert('Status cannot be blank for SAP ID entered');", true);
          break;
     }
     else { }
}

I have got a way to get this done.. Here I go

if (strFlag == "")
                    {
                        dtStatus = GET_STATUS_FROM_SAPID_FOR_HOTO(dtExcelRows.Rows[i]["Current SAPID"].ToString());

                        if (dtStatus == null && dtStatus.Rows.Count < 0)
                        {
                            ClientScript.RegisterStartupScript(Page.GetType(), "erroralert", "alert('Status cannot be blank for SAP ID entered');", true);
                        }
                        else
                        {
                            dtExcelRows.Rows[i]["UPDATED_STATUS"] = dtStatus.Rows[0][1].ToString();
                            dtExcelRows.AcceptChanges();
                        }
                    }
                }

                DataTable dtGetHotoPre = null;
                var rows = dtExcelRows.AsEnumerable().Where(x => x.Field<string>("UPDATED_STATUS") == "PRE HOTO");

                if (rows.Any())
                {
                    dtGetHotoPre = rows.CopyToDataTable();
                }

                DataTable dtGetHotoPost = null;
                var rowsPost = dtExcelRows.AsEnumerable().Where(x => x.Field<string>("UPDATED_STATUS") == "POST HOTO");

                if (rowsPost.Any())
                {
                    dtGetHotoPost = rowsPost.CopyToDataTable();
                }
                string strFlagStatus = "";
                if (dtGetHotoPre != null)
                {
                    if (dtGetHotoPost != null)
                    {
                        strFlagStatus = "No Process";
                    }
                    else
                    {
                        strFlagStatus = "Process";
                        grdDvHoto.DataSource = dtGetHotoPost;
                    }
                }
                else
                {
                    if (dtGetHotoPost != null)
                    {
                        strFlagStatus = "Process";
                        grdDvHoto.DataSource = dtGetHotoPre;
                    }
                    else
                    {
                        strFlagStatus = "No Process";
                    }
                }

               // if(dtGetHotoPre != null && dtGetHotoPost != null)

                if (strFlagStatus == "No Process")
                {
                    ClientScript.RegisterStartupScript(Page.GetType(), "erroralert", "alert('The sites contains both Pre and Post Hoto Status, so it cannot be uploaded');", true);
                }
                else 
                {
                    // will move ahead.                        
                    grdDvHoto.DataBind();
                }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM