dotnet0824
asked on
Using IFF statement in C#
I want to use IIF statement equivalient in C# coding
if strName is Empty I want to pass System.DBnull.value to be inserted in DB else strName would be passed
string strName;
strName = TxtName.text.value;
strName = string.empty ? System.dbnull.value : strName
Whatz the correct syntax. I get some Errors
if strName is Empty I want to pass System.DBnull.value to be inserted in DB else strName would be passed
string strName;
strName = TxtName.text.value;
strName = string.empty ? System.dbnull.value : strName
Whatz the correct syntax. I get some Errors
ASKER
then whatz the solution for this. I want to pass System.dbnull.value to the stored procedure if value not passed in the variable.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Also, String.Empty is a constant that represents an empty string. If you want to check if a particular string is empty you can use string.IsNullOrEmpty(strNa me);
So it would be:
if (string.IsNullOrEmpty(strn ame))
// Insert System.DbNull
else
// Insert strname
So it would be:
if (string.IsNullOrEmpty(strn
// Insert System.DbNull
else
// Insert strname
ASKER
I couldnt find such thing string.IsNullorEmpty ...
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
kumar_jac's is actually a pretty good solution when combined with hiteshgoldeneye's idea of only inserting it when you are building the query. You will need to alter it to cast str to an object though:
public object StringEmptyToDBNull(string str)
{
return (str == string.Empty) ? DBNull.Value : (object)str;
}
public object StringEmptyToDBNull(string
{
return (str == string.Empty) ? DBNull.Value : (object)str;
}
Sorry, you'll also want to check if the string is null, just for robustness:
public static object StringEmptyToDBNull(string str)
{
return (str == null || str == string.Empty) ? DBNull.Value : (object)str;
}
(Also note that it can be static)
public static object StringEmptyToDBNull(string
{
return (str == null || str == string.Empty) ? DBNull.Value : (object)str;
}
(Also note that it can be static)
hongjun