Click here to Skip to main content
15,894,405 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
Hi All,
I have a requirement to compare a text box input (Which would have a list of alphanumeric numbers separated by a delimiter) with a list of numbers in a dynamic datatable. The result on positive comparison is to show which ones among the list of alphanumeric numbers are in the datatable in an alert pop-up box.
And show a different alert if none of the containers in the list match the criteria.

I am currently able to achieve this for one input string. I just need sample code to split the string, store in an array and compare it with datatable. Thanks.

C#
public void Search_click(Object sender, EventArgs E)
    {
        string path = @"C:\TEMP\SearchString.xlsx";
        string connStr = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + path + ";Extended Properties=Excel 12.0;";
        OleDbConnection objConn = new OleDbConnection(connStr);
        try
        {
            objConn.Open();
            OleDbCommand objCmdSelect = new OleDbCommand("SELECT * FROM MyRange", objConn);
            OleDbDataAdapter objAdapter1 = new OleDbDataAdapter();
            objAdapter1.SelectCommand = objCmdSelect;
            DataSet objDataset1 = new DataSet();
            objAdapter1.Fill(objDataset1, "XLData");
            string strExcelInfo = "";
            strInfoText = _hiddentextInfo.Value;
            bool foundinExcel = false;

            if (objDataset1.Tables[0].Rows.Count > 0)
            {
                for (int i = 0; i < objDataset1.Tables[0].Rows.Count; i++)
                {
                    strExcelInfo = Convert.ToString(objDataset1.Tables[0].Rows[i]["Sheet1"].ToString());

                    if (strInfoText.Contains(strExcelInfo))
                    {
                        foundinExcel = true;
                        break;
                    }
                    else
                    {
                        continue;
                    }
                }
            }
            if (foundinExcel == true)
            {
                ClientScript.RegisterClientScriptBlock(this.GetType(), "alert", "alert('" + strInfoText + " is in the excel file')", true);

            }

            else if (foundinExcel == false)
            {
                ClientScript.RegisterClientScriptBlock(this.GetType(), "alert", "alert('" + strInfoText + " is not in the excel file.')", true);

            }
        }
Posted
Updated 19-Jan-16 10:40am
v3
Comments
Maciej Los 19-Jan-16 15:44pm    
What have you tried? Where are you stuck?

1 solution

One of the simplest way is to use Linq to DataSet[^].

C#
void Main()
{
	string myDelimitedText = "A123,C521,A224,B221,";
	DataTable dt = CreateDataTable();
	
	var dl = myDelimitedText.Split(new char[]{','}, StringSplitOptions.RemoveEmptyEntries)
			.Select(x=>new
				{
					Alpha = x,
					IsInDt = dt.AsEnumerable().Any(y=>y.Field<string>("MyField")==x)
				});
	foreach (var a in dl)
	{
		Console.WriteLine("{0} {1}", a.Alpha, a.IsInDt);
	}

	
}

// Define other methods and classes here
public static DataTable CreateDataTable()
{
	DataTable dt = new DataTable();
	dt.Columns.Add(new DataColumn("MyField", Type.GetType("System.String")));

	dt.Rows.Add(new Object[]{"A123"});
	dt.Rows.Add(new Object[]{"B123"});
	dt.Rows.Add(new Object[]{"C123"});
	dt.Rows.Add(new Object[]{"A224"});
	dt.Rows.Add(new Object[]{"C324"});
	
	return dt;

}


Result:
Alpha IsInDt
A123 True 
C521 False 
A224 True 
B221 False 



For futher information, please see: Queries in LINQ to DataSet[^]
 
Share this answer
 
v2
Comments
Rajesh_1980 19-Jan-16 16:32pm    
Thank you Maciej Los. I will add my code to the question. Kindly direct me from there.
Maciej Los 19-Jan-16 16:33pm    
You're welcome. Have you tried my code?
Rajesh_1980 19-Jan-16 17:06pm    
Maciej Los Thank you again.I am new to Linq. I will research on it and try your code. Will update you if I have any questions. Can we use Linq on older platform like ASP.Net 2.0?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900