Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
How to prevent possible error in this code?
Message
General information
Forum:
ASP.NET
Category:
Coding, syntax and commands
Environment versions
Environment:
C# 4.0
OS:
Windows 7
Network:
Windows 2003 Server
Database:
MS SQL Server
Application:
Web
Miscellaneous
Thread ID:
01599006
Message ID:
01599008
Views:
43
>Hi everybody,
>
>I am writing this code:
>
>
> for (Int16 i = 1; i <= 6; i++ )
>                {
>                    insertCommand.Parameters.Add(String.Format("@pr_ctr_{0}", i), SqlDbType.Int).Value = drItem.Field<Int32>(String.Format("pr_ctr_{0}", i));
>
>                    insertCommand.Parameters.Add(String.Format("@PC{0}Amt", i), SqlDbType.Money).Value =  splitAmounts[i-1] ;
>                }
>
>splitAmounts variable defined as
>
List<Decimal>
>
>I want to prevent possibility of that list having less than 6 elements, so in this case I would assign 0 to my parameter. Is there a way to do it in one line (change the last line) or I should add an extra if to check
>
>
i-1 < splitAmounts.Count
>
>?
>
>I guess I can use IIF C# analogue here.
insertCommand.Parameters.Add(String.Format("@PC{0}Amt", i), SqlDbType.Money).Value =  (i-1 < splitAmounts.Count) ? splitAmounts[i-1] : 0 ;
:-)
Against Stupidity the Gods themselves Contend in Vain - Johann Christoph Friedrich von Schiller
The only thing normal about database guys is their tables.
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform