How to read data from csv file C#

 How to read data from CSV file


We need to save our data and read it from there through some code most of the time. It is not possible to read it from the console all the time. So exchanging text file format is the common way to transfer data from one program to another or to someone who might require that data for some purpose. CSV is one of the popular data formats for storing the data.  Now why csv ?  csv is Comma Separated Values and the benefit is you can store data just like a key-value pair and you can open that file in Excel.  So storing the data is really very easy and easy to understand.  


One thing is very clear here: we will not create a csv parser , we will use an existing parser available in the market.  Here in this blog, I will use csvHelper for C#. . 


Here I am going to use VSCode to implement the code. You can use same code for any C# editor.  


Let's install csvHelper from Nuget  .


  1. Navigate to https://www.nuget.org  .

  2. Search for csvHelper  or direct navigate to https://www.nuget.org/packages?q=csvHelper


  1. dotnet add package CsvHelper --version 15.0.5     

Here I am going to use the latest version of csvHeler. 



Project Creating in VSCode


  1.  Open VSCode.

  2. Goto view ->Tarminal

  3. Write “dotnet new console”    . This will create a sample project




Installing csvHelper from Terminal


  1. Open Terminal

  2. Write  “dotnet add package CsvHelper --version 15.0.5

Now we can use “using CsvHelper”; in our code.  



Before writing the code we need to create a csv file first. My csv file will look like this. 



If I open it in Excel or calc, it will just like excel format  .



Here my intention is user will provide the row name and column and code will return the cell value .  Row name and column should be unique.  



using System;

using CsvHelper;

using System.IO;

using System.Data;

 

 

namespace TestProject

{

   class Program

    {

        

        static void Main(string[] args)

        {

             DataTable dataTable=null;

             string rowname =Console.ReadLine();

             string columnname=Console.ReadLine();

            using (var reader = new StreamReader(@"C:\Users\anjan\Desktop\mycsv.csv"))

            using (var csv = new CsvReader(reader, System.Globalization.CultureInfo.InvariantCulture))

            {                  

               

                using (var dr = new CsvDataReader(csv))

                {        

                    dataTable = new DataTable();

                    dataTable.Load(dr);

                }

                DataRow[] rowsdataTable.Select("ROWNAME='"+rowname+"'");

 

                foreach(DataRow row in rows)

                {

                    Console.WriteLine("value is  ="+ row[columnname]);

                }

                

            }

        }

    }

}

 



Output 


Time Elapsed 00:00:05.17

PS C:\Users\anjan\TestProject> dotnet run

ROW1

COLUMN3

value is  =data3

PS C:\Users\anjan\TestProject>






How to Upload file in selenium using c#

How to Upload file in selenium using c#

We are facing a problem when we have a situation like an upload file in the server in the automation testing because most of the cases we have seen upload dialog box is not a web-based popup, it is a window-based popup. 


File upload box


Currently, Selenium can not handle it because it only supports web-based applications only, so we need to take help from 3rd party applications like autoIT or robot to handle this kind of popup. 

But we can handle this upload popup without any 3rd part application if we use C# language for selenium. There is a NuGet package available in .NET which can handle this upload pop up without any 3rd party applications. 

Package name: OpenDialogWindowHandler -v0.0.0.1

You need to import it from the NuGet package manager before using it inside the class.

Method to upload file

public void UploadFile(string filepath,string filename){
    HandleOpenDialog hndOpen =  new  HandleOpenDialog();
    hndOpen.fileOpenDialog(filepath,filename);
}

There first we need to create an object of HandleOpenDialog and then call the function
fileOpenDialog(). This function has 2 parameter 1st one is file path eg.  "c:\\helpfile"
and next one is file name. eg "help.txt".