Connecting Tech Pros Worldwide Help | Site Map

Empty string is stored as system.object in mysql column!

Member
 
Join Date: Feb 2009
Posts: 57
#1: Jun 5 '09
Hi,

I am passing empty string from Business Logic(C#) to Data Access Layer, but it is getting stored as "system.object" in mysql column after executing the insert query. Can anybody suggest what should i do, as i am expecting the empty string to be stored in mysql column as it is??

Thanks In Advance,
Lauren
Lives Here
 
Join Date: Sep 2006
Posts: 12,070
#2: Jun 5 '09

re: Empty string is stored as system.object in mysql column!


MySQL does not have a System.object type.
What makes you think that it's stored as System.object?
Member
 
Join Date: Feb 2009
Posts: 57
#3: Jun 5 '09

re: Empty string is stored as system.object in mysql column!


I have declared that column as varchar and the default value is null. But when the value for that column from business logic is coming as (""), then it is stored as "system.object" which is a string. It is not storing as a type.Can you please suggest where am i going wrong??

Thanks,
Lauren
Atli's Avatar
Moderator
 
Join Date: Nov 2006
Location: Iceland
Posts: 3,742
#4: Jun 5 '09

re: Empty string is stored as system.object in mysql column!


If it is being stored as "System.Object", I'm guessing C# is not aware that it is supposed to be a string when you insert it.

Perhaps you just need to cast it as a string before you insert it into the query.
Like:
Expand|Select|Wrap|Line Numbers
  1. string emptyString = (String)yourObject;
  2. string sql = String.Format("INSERT INTO whatever VALUES('{0}')", emptyString);
Member
 
Join Date: Feb 2009
Posts: 57
#5: Jun 5 '09

re: Empty string is stored as system.object in mysql column!


Thanks for the quick response.
I am using "DBType.string" for the input parameter to the stored procedure before executing it. Upto this point the string which is coming from the business logic as (""). But after executing the query when i look at the data, it is stored as "system.object".

Thanks,
Lauren
Atli's Avatar
Moderator
 
Join Date: Nov 2006
Location: Iceland
Posts: 3,742
#6: Jun 5 '09

re: Empty string is stored as system.object in mysql column!


Could you show us the code you use to create the query? (Or execute the procedure, or whatever is inserting the data.)

Without actually knowing what your code is doing, I can only guess that the problem is what I described in my last post.
Lives Here
 
Join Date: Sep 2006
Posts: 12,070
#7: Jun 5 '09

re: Empty string is stored as system.object in mysql column!


You are not making much sense. Where are you looking at it and seeing it as System.object? Like I said MySQL does not have that type. It's a .NET type.
Post the .NET code that led you to believe that the value was stored as a System.object type.
Member
 
Join Date: Feb 2009
Posts: 57
#8: Jun 5 '09

re: Empty string is stored as system.object in mysql column!


Expand|Select|Wrap|Line Numbers
  1. public int Create(ref Entity EntityInstance)
  2. {
  3.       QueryObject queryInstance = new QueryObject();
  4.       queryInstance.Query = "Create";
  5.       DbParam dbParamName = new DbParam("strName", DbType.String,  EntityInstance.Name);
  6.      queryInstance.AddInParameter(dbParamName);
  7.      DbConnection dbConnection;
  8.      DbTransaction databaseTransaction;
  9.     Database dbInstance = DBO.BeginTransaction(System.Data.IsolationLevel.ReadUncommitted, out dbConnection, out databaseTransaction);
  10.     // This writes the data into the table using the stored procedure "Create"
  11.     error = DBO.WriteDataItems(ref dbInstance, ref databaseTransaction, ref queryInstance);   
  12.  
  13. }
EntityInstance is filled by the business logic and EntityInstance.Name is coming as an empty string.
After writing the data into the table after executing the query its getting stored as a string "system.object".


// Procedure
DELIMITER $$

DROP PROCEDURE IF EXISTS `DB`.`test` $$
CREATE DEFINER=`root`@`%` PROCEDURE `test`(IN strName varchar(30))
BEGIN
DECLARE EXIT HANDLER FOR SQLEXCEPTION set ex = -1;
SET ex := 0;

INSERT INTO Test (Name) VALUES (strName);

END $$

DELIMITER ;

I have attached two snap shot for this issue.

Thanks
Lauren
Attached Thumbnails
test.jpg   testdb.jpg  
Atli's Avatar
Moderator
 
Join Date: Nov 2006
Location: Iceland
Posts: 3,742
#9: Jun 5 '09

re: Empty string is stored as system.object in mysql column!


Try changing line #5 in that code to:
Expand|Select|Wrap|Line Numbers
  1. DbParam dbParamName = new DbParam("strName", DbType.String,  (EntityInstance.Name as string));
If the EntityInstance.Name is set as an empty Object, rather than a empty string, then this should fix it.

If not, then I would guess the problem is deeper, withing the EntityInstance or DbParam classes.

In any case... somewhere in that code, your suppsoedly emtpy string, EntitiyInstance.Name, is being treated as an Object, which would make it's ToString return "System.Object", which is getting into your database.

Go through your code and make sure this string is being treated as a string everywhere.

Look for any place your string might be stored in a HasTable or such. They store all things as Objects, and need to be told to convert things back into their original type.
Reply