Leading zeros are not adding to DataTable columns C# Asp.Net with PadLeft or String.Format -


leading zeros not adding datatable columns padleft or string.format.

initially copied user uploaded excel data datatable. i'm trying add zeros in front of datatable column values if length less 8 digits , after have compare table matching records. if don't have leading zeros i'm missing records while matching other datatable columns. want them leading zeros can matched correct results.

ex: have column "code" in datatable values 30500, 501080, 5020900, 19010300 etc , have results 00030500, 00501080, 05020900, 19010300

note: change data in datatable not in sql query retrieves data. i don't want code converting int string leading zeros. tried in way didn't fix issue.

i tried couple of ways didn't solve. what's wrong code. it's not working. used below how add leading zeros in datatable columns still not changed anything. don't consider post duplicate, tried ways still problem exist, hence posting here.

approach 1:

 foreach (datarow row in dataexcelinputtable.rows)                 {                     row["code"] = row["code"].tostring().padleft(8, '0');                 }                 dataexcelinputtable.acceptchanges(); 

approach 2:

foreach (datarow drin dataexcelinputtable.rows)                 {                    dr["code"] = string.format("{0:00000000}", int.parse(dr["code"].tostring()));                 }                 dataexcelinputtable.acceptchanges(); 

approach 3:

               int countsize = 0;                 int val = 0;                 foreach (datarow row in dataexcelinputtable.rows)                 {                     countsize = row["code"].tostring().length;                     val = int.parse(row["code"].tostring());                     if (countsize < 8)                     {                          row["code"] = val.tostring("d8");                          //response.write("<br/>" + val.tostring("d8"));                     }                  }                 dataexcelinputtable.acceptchanges(); 

update:

foreach (datarow row in dataexcelinputtable.rows)                 {                     if (row["code"].tostring().length < 8)                     {                         row["code"] = row["code"].tostring().padleft(8, '0');                     }                     response.write("<br/>" + row["code"]);                 }                 dataexcelinputtable.acceptchanges(); 

right printing below, not padding 0 front.

9040100 (<8) , 9070100 (<8) , 9090200 (<8) , 9090300 (<8)

10020300 (=8) , 10030300 (=8) , 11010100 (=8)

i tried @ end , getting expected output ... below test code

        system.data.datatable dt = new system.data.datatable();         dt.columns.add("code");         system.data.datarow r = dt.newrow();         r["code"] = "30500";         dt.rows.add(r);          foreach (system.data.datarow row in dt.rows)         {             row["code"] = row["code"].tostring().padleft(8, '0');         }         dt.acceptchanges(); 

//dt.rows[0][0] value 00030500


Popular posts from this blog

php - How should I create my API for mobile applications (Needs Authentication) -

python 3.x - PyQt5 - Signal : pyqtSignal no method connect -

5 Reasons to Blog Anonymously (and 5 Reasons Not To)