locked
Values in Array RRS feed

  • Question

  • User-1499457942 posted

    Hi

      I have Data in a field like this 'Del','KAr','MAh'. I want to store these values in array and then check that values in array exists in Master table or not.

    Thnaks

    Monday, August 27, 2018 4:40 AM

All replies

  • User-369506445 posted

    hi

    we need more info about your problem .what technology do you want to use? the master table in database or HTML?

    Monday, August 27, 2018 4:46 AM
  • User-1499457942 posted

    Hi

     I am using Sql . In Database field it gets stored like above.

    Thanks

    Monday, August 27, 2018 6:35 AM
  • User-369506445 posted

    please try below

    var arr = new string[] { "Del", "KAr", "MAh" };
    
                using (var con = new SqlConnection("connection String"))
                {
                    using (var com = new SqlCommand("select yourFieldName from yourTable", con))
                    {
                        con.Open();
                        using (var reader = com.ExecuteReader())
                        {
                            if (reader.HasRows)
                            {
                                while (reader.Read())
                                {
    // check the array value with database rows if (arr.Any(x=>x.Equals(reader.GetString(0)))) { // Exist in database } } } else { Response.Write("No rows found."); } } } }

    Monday, August 27, 2018 6:47 AM
  • User-1499457942 posted

    Hi Vahid

      I master Table Data will be like below

    Code

    1. Mah
    2. Kar
    3. Delhi

    In Transaction Table Data will be like below

         Code

         mah,Kar,

         Delhi

         Chd

    Data to be read from Transaction table.

       In first case mah also exists in master table & Kar also exists.  In third record Chd but Chd does not exists in Master able so i get error message

    Thanks

    Monday, August 27, 2018 6:56 AM
  • User-369506445 posted

    you can write 2 query and load them into your project then check them with C# <g class="gr_ gr_3 gr-alert gr_spell gr_inline_cards gr_run_anim ContextualSpelling ins-del multiReplace" id="3" data-gr-id="3">linq</g> below like

              List<string> listMaster = new List<string>();
    
                using (var con = new SqlConnection("connection String"))
                {
                    using (var com = new SqlCommand("select name from MasterTable", con))
                    {
                        con.Open();
                        using (var reader = com.ExecuteReader())
                        {
                            if (reader.HasRows)
                            {
                                while (reader.Read())
                                {
                                    listMaster.Add(reader.GetString(0));
                                }
                            }
                        }
                    }
                }
    
    
    // select rows from the Transaction Table using (var con = new SqlConnection("connection String")) { using (var com = new SqlCommand("select name from TransactionTable", con)) { con.Open(); using (var reader = com.ExecuteReader()) { if (reader.HasRows) { while (reader.Read()) { var arr = reader.GetString(0).Split(','); foreach (var item in arr) { if (listMaster.Any(x=>x.Equals(item))) { // here data in TransactionTable Exist in Master Table } } } } } } }

    Monday, August 27, 2018 7:21 AM