CodeMan
CodeMan

Reputation: 133

how to validate datatable rows after loading data

I have a datatable that reads from Informix table and stores values in it, how ever the zipcode column has values that range from 3 to 9 so I need to validate this column and then send it to my merge method.

this is my Datatable Code

 public static DataTable GetGroup1Data()
        {
 string threeyearsago = (DateTime.Now.Year - 2).ToString();
OdbcConnection MyCon = new OdbcConnection();
 MyCon.ConnectionString = ConfigurationManager.ConnectionStrings["DBLive"].ConnectionString;
 MyCon.Open();
   OdbcCommand mycmd = new OdbcCommand();
   mycmd.Connection = MyCon;
   mycmd.CommandType = CommandType.Text;
 mycmd.CommandText = "{SET ISOLATION TO DIRTY READ} SELECT CASE WHEN LENGTH(REPLACE(zip,'-','')) <= '5' THEN RPad(RTRIM(zip),'9','0') WHEN LENGTH(REPLACE(zip,'-','')) > '5' AND LENGTH(REPLACE(zip,'-','')) < '9' THEN LPAD(LTRIM(zip),9,'0') ELSE Replace(zip,'-','') END AS ZIPCODE from sometable
DataSet custmaster = new DataSet();
 OdbcDataAdapter myadapter = new OdbcDataAdapter();
 myadapter.SelectCommand = mycmd;
 DataTable dt = new DataTable();
 dt.Load(mycmd.ExecuteReader());           
 mycmd.Dispose();
 MyCon.Close();
 return dt;
}

I want to now validate the zipcode values length and do left pad or right pad, how can I achieve that?

Upvotes: 0

Views: 981

Answers (1)

NicoRiff
NicoRiff

Reputation: 4883

You can try this:

foreach(DataRow d in dt.Rows)
{
     if(d["zip"].ToString().Length < 10) //example
     {
         //do something
     }
}

Tne you can use PadLeft and PadRight to do the padding.

Upvotes: 1

Related Questions