Convert Null db field to string
-
I use this code to read some records from a access database with contacts exported from Outlook. I get an error when the content of a field in the db is NULL bc it cannot be converted to string. How could I correct this and the convert a Null field in a " " string? Thanks static void Main() { Program instan = new Program(); instan.ReadContacts(); Console.Read(); } public void ReadContacts() { try { OleDbDataReader reader = null; OleDbConnection conn = new OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=c:\\AGH\\epafes_outlook_2007.accdb;Persist Security Info=False"); string comStr = "SELECT FirstName, LastName, BusinessPhone FROM Contacts"; OleDbCommand cmd = new OleDbCommand(comStr, conn); conn.Open(); reader = cmd.ExecuteReader(); int i=1; while (reader.Read()) { string FName = (string)reader["FirstName"]; string LName = (string)reader["LastName"]; string BPhone = (string)reader["BusinessPhone"]; Console.Write("{0,-4}", i); Console.Write("{0,-13}", FName); Console.Write("{0,-13}", LName); Console.Write("{0}", BPhone); Console.WriteLine(); i++; } Console.WriteLine("\nFinish"); conn.Close(); reader.Close(); } catch (Exception e) { Console.Write(e); } }
-
I use this code to read some records from a access database with contacts exported from Outlook. I get an error when the content of a field in the db is NULL bc it cannot be converted to string. How could I correct this and the convert a Null field in a " " string? Thanks static void Main() { Program instan = new Program(); instan.ReadContacts(); Console.Read(); } public void ReadContacts() { try { OleDbDataReader reader = null; OleDbConnection conn = new OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=c:\\AGH\\epafes_outlook_2007.accdb;Persist Security Info=False"); string comStr = "SELECT FirstName, LastName, BusinessPhone FROM Contacts"; OleDbCommand cmd = new OleDbCommand(comStr, conn); conn.Open(); reader = cmd.ExecuteReader(); int i=1; while (reader.Read()) { string FName = (string)reader["FirstName"]; string LName = (string)reader["LastName"]; string BPhone = (string)reader["BusinessPhone"]; Console.Write("{0,-4}", i); Console.Write("{0,-13}", FName); Console.Write("{0,-13}", LName); Console.Write("{0}", BPhone); Console.WriteLine(); i++; } Console.WriteLine("\nFinish"); conn.Close(); reader.Close(); } catch (Exception e) { Console.Write(e); } }
string FName = string.Empty;
object temp = reader["FirstName"];
if (temp != DBNull.Value)
FName = (string)temp;
Upcoming events: * Glasgow: Mock Objects, SQL Server CLR Integration, Reporting Services, db4o, Dependency Injection with Spring ... "I wouldn't say boo to a goose. I'm not a coward, I just realise that it would be largely pointless." My website
-
I use this code to read some records from a access database with contacts exported from Outlook. I get an error when the content of a field in the db is NULL bc it cannot be converted to string. How could I correct this and the convert a Null field in a " " string? Thanks static void Main() { Program instan = new Program(); instan.ReadContacts(); Console.Read(); } public void ReadContacts() { try { OleDbDataReader reader = null; OleDbConnection conn = new OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=c:\\AGH\\epafes_outlook_2007.accdb;Persist Security Info=False"); string comStr = "SELECT FirstName, LastName, BusinessPhone FROM Contacts"; OleDbCommand cmd = new OleDbCommand(comStr, conn); conn.Open(); reader = cmd.ExecuteReader(); int i=1; while (reader.Read()) { string FName = (string)reader["FirstName"]; string LName = (string)reader["LastName"]; string BPhone = (string)reader["BusinessPhone"]; Console.Write("{0,-4}", i); Console.Write("{0,-13}", FName); Console.Write("{0,-13}", LName); Console.Write("{0}", BPhone); Console.WriteLine(); i++; } Console.WriteLine("\nFinish"); conn.Close(); reader.Close(); } catch (Exception e) { Console.Write(e); } }
kallileo wrote:
string FName = (string)reader["FirstName"]; string LName = (string)reader["LastName"]; string BPhone = (string)reader["BusinessPhone"];
If a column is nullable in the DB, you should check for nullity in your code. Unfortunately, it is not the same as .NET
null
. You have to do it this way:if (reader.IsDBNull(reader.GetOrdinal("FirstName")) { FName = null; // Or String.Empty, as you see fit. } else { FName = (string)reader["LastName"]; }
` ----- _If atheism is a religion, then not collecting stamps is a hobby._ -- Unknown `
-
kallileo wrote:
string FName = (string)reader["FirstName"]; string LName = (string)reader["LastName"]; string BPhone = (string)reader["BusinessPhone"];
If a column is nullable in the DB, you should check for nullity in your code. Unfortunately, it is not the same as .NET
null
. You have to do it this way:if (reader.IsDBNull(reader.GetOrdinal("FirstName")) { FName = null; // Or String.Empty, as you see fit. } else { FName = (string)reader["LastName"]; }
` ----- _If atheism is a religion, then not collecting stamps is a hobby._ -- Unknown `
Le Centriste wrote:
You have to
"have to" is a bit strong. You don't "have to" do it that way (although it is a reasonable way to check) as there are other reasonable alternatives.
Upcoming events: * Glasgow: Mock Objects, SQL Server CLR Integration, Reporting Services, db4o, Dependency Injection with Spring ... "I wouldn't say boo to a goose. I'm not a coward, I just realise that it would be largely pointless." My website
-
Le Centriste wrote:
You have to
"have to" is a bit strong. You don't "have to" do it that way (although it is a reasonable way to check) as there are other reasonable alternatives.
Upcoming events: * Glasgow: Mock Objects, SQL Server CLR Integration, Reporting Services, db4o, Dependency Injection with Spring ... "I wouldn't say boo to a goose. I'm not a coward, I just realise that it would be largely pointless." My website
Yes, you're right. Wrong choice of words. I should have said "Here is an example".
----- If atheism is a religion, then not collecting stamps is a hobby. -- Unknown
-
I use this code to read some records from a access database with contacts exported from Outlook. I get an error when the content of a field in the db is NULL bc it cannot be converted to string. How could I correct this and the convert a Null field in a " " string? Thanks static void Main() { Program instan = new Program(); instan.ReadContacts(); Console.Read(); } public void ReadContacts() { try { OleDbDataReader reader = null; OleDbConnection conn = new OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=c:\\AGH\\epafes_outlook_2007.accdb;Persist Security Info=False"); string comStr = "SELECT FirstName, LastName, BusinessPhone FROM Contacts"; OleDbCommand cmd = new OleDbCommand(comStr, conn); conn.Open(); reader = cmd.ExecuteReader(); int i=1; while (reader.Read()) { string FName = (string)reader["FirstName"]; string LName = (string)reader["LastName"]; string BPhone = (string)reader["BusinessPhone"]; Console.Write("{0,-4}", i); Console.Write("{0,-13}", FName); Console.Write("{0,-13}", LName); Console.Write("{0}", BPhone); Console.WriteLine(); i++; } Console.WriteLine("\nFinish"); conn.Close(); reader.Close(); } catch (Exception e) { Console.Write(e); } }
I think I found it....and it seems to work. But I'm not realy sure if this is the right way to do it. string FName = Convert.ToString(reader["FirstName"]); string LName = Convert.ToString(reader["LastName"]); string BPhone = Convert.ToString(reader["BusinessPhone"]); Thanks -- modified at 9:17 Friday 20th July, 2007 This also works: string BPhone = reader["BusinessPhone"].ToString();
-
I think I found it....and it seems to work. But I'm not realy sure if this is the right way to do it. string FName = Convert.ToString(reader["FirstName"]); string LName = Convert.ToString(reader["LastName"]); string BPhone = Convert.ToString(reader["BusinessPhone"]); Thanks -- modified at 9:17 Friday 20th July, 2007 This also works: string BPhone = reader["BusinessPhone"].ToString();
Curiously, if you look at what Convert.ToString actually does, it just boils down to the same as:
string FName = reader["FirstName"].ToString();
So, is
DBNull.Value.ToString()
the value that you really want back?
Upcoming events: * Glasgow: Mock Objects, SQL Server CLR Integration, Reporting Services, db4o, Dependency Injection with Spring ... "I wouldn't say boo to a goose. I'm not a coward, I just realise that it would be largely pointless." My website
-
I think I found it....and it seems to work. But I'm not realy sure if this is the right way to do it. string FName = Convert.ToString(reader["FirstName"]); string LName = Convert.ToString(reader["LastName"]); string BPhone = Convert.ToString(reader["BusinessPhone"]); Thanks -- modified at 9:17 Friday 20th July, 2007 This also works: string BPhone = reader["BusinessPhone"].ToString();
-
Hi Since string is a reference type you could also do it like this:
string FName = reader["FirstName"] as string; string LName = reader["LastName"] as string; string BPhone = reader["BusinessPhone"] as string;
greets M@um@u wrote:
Hi Since string is a reference type you could also do it like this: string FName = reader["FirstName"] as string; string LName = reader["LastName"] as string; string BPhone = reader["BusinessPhone"] as string; greets M@u
Thanks....
Colin Angus Mackay wrote:
Curiously, if you look at what Convert.ToString actually does, it just boils down to the same as: string FName = reader["FirstName"].ToString(); So, is DBNull.Value.ToString() the value that you really want back?
Yes, it is ok to me...
-
Curiously, if you look at what Convert.ToString actually does, it just boils down to the same as:
string FName = reader["FirstName"].ToString();
So, is
DBNull.Value.ToString()
the value that you really want back?
Upcoming events: * Glasgow: Mock Objects, SQL Server CLR Integration, Reporting Services, db4o, Dependency Injection with Spring ... "I wouldn't say boo to a goose. I'm not a coward, I just realise that it would be largely pointless." My website
DBNull.Value.ToString() returns an empty string, so it boils down to be the same as the examples we give, I believe.
----- If atheism is a religion, then not collecting stamps is a hobby. -- Unknown