How to concatenate strings in conditional operator [Resolved]

Posted by Self-Innovator under ASP.NET MVC on 4/29/2016 | Points: 10 | Views : 310 | Status : [Member] | Replies : 2
Hi All,

I wanted to assign some strings to conditional oprator.


  body += " Store Number:" + storeRegistration.StoreNumber!=null?storeRegistration.StoreNumber:Convert.ToInt32(string.Empty);
body += " Store Name:" + storeRegistration.StoreName!=null?storeRegistration.StoreNumber:Convert.ToInt32(string.Empty);


In this case if string is empty i'm not able to concatenate the string "Store Number or Store Name" it works only if we have some values, not concatenating in case of empty strings

Join Hands Change lives
Thanks & Regards
Straight Edge Society



Responses

Posted by: Rajnilari2015 on: 5/1/2016 [Member] [Microsoft_MVP] [MVP] Platinum | Points: 50

Up
0
Down

Resolved
Try this

var sb = new StringBuilder();
sb.Append(" Store Number:");
sb.Append(storeRegistration.StoreNumber!=null? storeRegistration.StoreNumber:"0");
sb.Append(Environment.NewLine);
sb.Append(" Store Name:");
sb.Append(storeRegistration.StoreName!=null? storeRegistration.StoreNumber:"0");
body = sb.toString();


--
Thanks & Regards,
RNA Team

Self-Innovator, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Sheonarayan on: 4/29/2016 [Administrator] HonoraryPlatinum | Points: 25

Up
1
Down
It is because Convert.ToInt32(string.Empty) throws error - input string was not in the correct format.

You can't convert an empty string to integer. So instead either keep it "" or "0".

body += " Store Number:" + storeRegistration.StoreNumber!=null?storeRegistration.StoreNumber:"";
body += " Store Name:" + storeRegistration.StoreName!=null?storeRegistration.StoreNumber:Convert.ToInt32("0");

Thanks

Regards,
Sheo Narayan
http://www.dotnetfunda.com

Self-Innovator, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response