Click here to Skip to main content
15,885,537 members
Please Sign up or sign in to vote.
3.67/5 (3 votes)
See more:
dr[columnname] = datavalue.Trim() == "" ? DBNull.Value : Convert.ToInt32(datavalue);


what is the correct coding?

if datavalue.Trim() == "" then i want to pass the blank space to my column dr[columnname].
Posted
Updated 22-Jun-12 3:54am
v4
Comments
Rajeev Jayaram 22-Jun-12 9:41am    
Can you share the error message?
Kamalkant(kk) 22-Jun-12 9:54am    
Type of conditional expression cannot be determined because there is no implicit conversion between 'System.DBNull' and 'int'
Rajeev Jayaram 22-Jun-12 10:01am    
Keep your second and third operand the same type. Or explicitly cast it. See my answer, if that helps.
Clifford Nelson 22-Jun-12 13:11pm    
Have any of these answers helped. I would like to know the answer, so please mark as answered any that help.
Kamalkant(kk) 25-Jun-12 4:14am    
Hi Nelson Still I am not getting correct answer from any body.Can u plz give your Email_ID of gmail or any Contact Num.
My Email_ID is:kamalakanta.nayak09@gmail.com

You are tring impossible:

See the below code:
XML
DataTable table = new DataTable();
            Nullable<int> X = 10;
            X = null;
            table.Columns.Add("Dosage", X.GetType() );
            string datavalue = "123";
            DataRow row = table.NewRow();
            row["Dosage"] = datavalue.Trim() == "" ? 0 : Convert.ToInt32(datavalue);


In this instead of 0, if you try null or DBNull or anything your application will not work, you should considering 0 instead
 
Share this answer
 
You are mixing types in your expression, DBNull.Value[^] is not an Int32.
 
Share this answer
 
Try this,

dr[columnname] = datavalue.Trim() == "" ? String.Empty : Convert.ToInt32(datavalue).ToString();


Or

dr[columnname] = datavalue.Trim() == "" ? (int?)null : Convert.ToInt32(datavalue);
 
Share this answer
 
v2
Comments
Kamalkant(kk) 22-Jun-12 10:09am    
my datavalue is integer type no need to convert it into string type
Rajeev Jayaram 22-Jun-12 10:29am    
In that case try this,

dr[columnname] = datavalue.Trim() == "" ? (int?)null : Convert.ToInt32(datavalue);

Kamalkant(kk) 22-Jun-12 10:14am    
Showing error as follows;

Input string was not in a correct format.Couldn't store <> in quantity Column. Expected type is Int32.
Rajeev Jayaram 22-Jun-12 10:32am    
The bottomline is, you should tell the compiler what is the return type. It gets confused if both the operands are of different type. Try explicit casting in any one of the operands atleast.
You can pass null,

add dr[columnname] = datavalue.Trim() == "" ? null : Convert.ToInt32(datavalue);
 
Share this answer
 
Comments
phil.o 22-Jun-12 10:56am    
False. There is no implicit conversion between null and Int32.
Your statement would return the same error message as OP's one.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900