OneA One

OneA One

  • NA
  • 12
  • 2.6k

Store null value into Dictionary.

Aug 30 2014 9:19 AM
I use DataSet and DataRow to retrieve values from XLS.

So, I have to rows in my DataSet;
Row One - has field names.
Row Two - has field values.

I have one Dictionary<string, string> l = new Dictionary<string, string>();

I use foreach loop, to retrieve the field names for the Row One.


Only after I have fetched all the field names from row, I can go to the second row (Row Two) with the - foreach - loop.

Here, I want to fill all the field names, then in my second iteration can fill all the field values into the same Dictionalry object.

But Dictionary does not allow me to fill null value in the second cell.

I don't want to use Array because I can't know the size of columns in any given row, they change.

My sample test case also attached.

--------------------------Code -----------------
public void CodedUITestMethod1()
        {
            string filterFilePath = "D:\\songs\\b.xls";
            string connectionString = string.Format("Provider = Microsoft.ACE.OLEDB.12.0;Data Source ={0};Extended Properties = Excel 12.0;", filterFilePath);
            OleDbConnection con = new OleDbConnection(connectionString);
            OleDbCommand cmd = new OleDbCommand();
            con.Open();
            cmd.Connection = con;
            cmd.CommandText = String.Format("select * from [Sheet1$]");
            cmd.ExecuteNonQuery();
            OleDbDataAdapter dataAdapter = new OleDbDataAdapter();
            dataAdapter.SelectCommand = cmd;
            DataSet ds = new DataSet();
            dataAdapter.Fill(ds);
            DataRow[] foundRows = ds.Tables[0].Select("TestCaseID Like 'Two%'");


            Dictionary<string, string> dictionaryObj = new Dictionary<string, string>();


            //Row iteration
            foreach (DataRow dr in foundRows)
            {
                //Column iteration within the row
                foreach (Object c in dr.ItemArray)
                {
                    //Storing column value into dictionary
                    dictionaryObj.Add(c.ToString(), "");
                }
            }
        }

Answers (2)