2
Answers

Unable to cast object of type 'System.DBNull' to type 'System.String

Ask a question
mali

mali

16y
14.8k
1

this is the code:

 

if (client_s.Search())

{

found_clients = new Client[client_s.ds.Tables[0].Rows.Count];

DataTable dt = new DataTable();

dt = client_s.ds.Tables[0];

foreach (DataRow dr in dt.Rows)

{

for (j = 0; j < dr.ItemArray.GetLength(0); j++)

{

   if (dr[i] == null)

{

dr[i] = "";

}

}

found_clients[i] = new Client((string)(dr["clientNum"]), (string)(dr["clientId"]), client_s.fileNums, (string)(dr["clientComment"]), (int)(dr["parentNum"]), (string)(dr["parentName"]), (string)(dr["parentLastName"]), (int)(dr["parentNatureNum"]), (string)(dr["parentAddres2"]), (string)(dr["parentStreet"]), (string)(dr["parentPOB"]), (int)(dr["parentCityNum"]), (string)(dr["parentZipCode"]), (int)(dr["parentCountryNum"]), (string)(dr["parentEmail"]), (string)(dr["parentPhon1"]), (string)(dr["parentPhon2"]), (string)(dr["parentPhon3"]), (string)(dr["parentPhon4"]));

i++;

}

when i'm runnig this code i get  "Unable to cast object of type 'System.DBNull' to type 'System.String'" messege. how can i convert from dbnull to string? (i want to allow "null" in my database)

I try also this code  to put between (part ot the code):

if ((string)(dr["parentPhon4"]) == null)

   (dr["parentPhon4"]) = "0";

 

but i get the seme messege

 

thanks mali

 

 

 


Answers (2)