Connecting Tech Pros Worldwide Forums | Help | Site Map

Handling Null in C# win application

Newbie
 
Join Date: Nov 2008
Posts: 21
#1: Nov 21 '08
Hi
My application will select and read records using datareader from mysql table and do insert or update on access data base.My problem is when I set the mdb database to allow zero length string the code below works fine.But if it is set to no.I get this error.I want to however insert a null in Access database.Any help would be great! Thanks

// Field 'patientinfo.Patient Last' cannot be a zero-length string. //

Expand|Select|Wrap|Line Numbers
  1.  
  2.  
  3.  string firstname1 = myreader["firstname"].ToString();
  4.  
  5.   string lastname1 = myreader["lastname"].ToString();
  6.  
  7.   string CI_address11 = myreader["CI_address1"].ToString();
  8.  
  9.   string CI_address21 = myreader["CI_address2"].ToString();
  10.  
  11. // insert query 
  12.  
  13.  OdbcCommand mycommand4 = new OdbcCommand("insert into patientinfo([patientid],[patient first],[patient last],[pt DOB],[SSN],[address],[address2],[city],[state],[zipcode],[sex]) values ('" + MRN1 + "','" + firstname1 + "','" + lastname1 + "','" + dob1 + "','" + ssn1 + "','" + CI_address11 + "','" + CI_address21 + "','" + CI_city1 + "','" + CI_state1 + "','" + CI_zip1 + "','" + sex1 + "')", mdbconn);
  14.  
  15.  int k = mycommand4.ExecuteNonQuery();
  16.  

Plater's Avatar
Moderator
 
Join Date: Apr 2007
Location: New England
Posts: 7,161
#2: Nov 21 '08

re: Handling Null in C# win application


Take a look at DBNull.Value
Newbie
 
Join Date: Nov 2008
Posts: 21
#3: Nov 21 '08

re: Handling Null in C# win application


Thank you ,will try it.
Reply