3

I have a datatable. I need to fetch a certain column value based on the user input. For example, lets say the datatable has three column intpkdata,intFrom,intTo Here is my some code,

                drCurrentRow = dtCurrentTable.NewRow();
                drCurrentRow["intpkdata"] = new Random().Next(0, 99999).ToString();
                drCurrentRow["intFrom"] = txtFrom.Text;
                drCurrentRow["intTo"] = txtTo.Text;

                dtCurrentTable.Rows.Add(drCurrentRow);
                ViewState["Pcidata"] = dtCurrentTable;
                gdvpciData.DataSource = dtCurrentTable;
                gdvpciData.DataBind();

Requirement :

if intFrom/intTo data is already exist in datatable then msg should come.Pleas see the image for records

enter image description here

Imran Azam
  • 89
  • 9
  • Possible duplicate of [How to get data between two columns in datatable?](http://stackoverflow.com/questions/34786195/how-to-get-data-between-two-columns-in-datatable) –  Apr 20 '16 at 11:49

3 Answers3

2

try this

string fromValue = "some value";
string toValue = "some value";

if(dtCurrentTable.AsEnumerable().Any(row => fromValue == row.Field<string>("intFrom") && toValue == row.Field<string>("intTo")))
{
 //exists 
}

see this ques. Check if value exists in dataTable?

Check if String / Record exists in DataTable

Community
  • 1
  • 1
Ajay
  • 6,418
  • 18
  • 79
  • 130
  • I am getting this error " Operator '==' cannot be applied to operands of type 'string' and 'int' " – Imran Azam Apr 20 '16 at 12:37
  • what is the data type of `intFrom` ? – Ajay Apr 20 '16 at 12:41
  • Thanks a lot,Working fine you save my lot of time bro. Actually i forgot to convert. – Imran Azam Apr 20 '16 at 12:44
  • @ImranAzam Welcome :) – Ajay Apr 20 '16 at 12:44
  • How to search data between intfrom and intTo column, let say first time i have added 10 to 30 and next time i am trying to add value 15 to 40 then msg should come bcz 15 is already exist in between 10 to 30 any idea how to achieve this task @Ajay Punekar – Imran Azam Apr 21 '16 at 04:35
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/109749/discussion-between-imran-azam-and-ajay-punekar). – Imran Azam Apr 21 '16 at 04:42
0

Let txtFrom and txtTo be the two values, that have to be checked in the intFrom row and intTo columns respectively.(assuming that are having values 10 and 30). So the filter condition will be as follows, if the count > 0 means there are such rows existing in the table so the message can be printed.

string txtFrom = "10";
string txtTo = "30";
if (dtCurrentTable.AsEnumerable().Where(row => row.Field<string>("intFrom") == txtFrom && row.Field<string>("intTo") == txtTo).Count() > 0)
{
    // Display message here that it already exist
}
sujith karivelil
  • 28,671
  • 6
  • 55
  • 88
0

Check this code....

{

   String strName = "name";

    bool contains = dtCurrentTable.AsEnumerable().Any(row => strName == row.Field<String>("intFrom"));

    String strName2 = "name";

    bool contains2 = dtCurrentTable.AsEnumerable().Any(row => strName2 == row.Field<String>("intTo"));

    if (contains && contains2)
    {
        // Do your code
    }
}
Ajay
  • 6,418
  • 18
  • 79
  • 130
Dipan
  • 1
  • 1