1

I want to give optional parameter value to objTest.TestNumber in my below code. My code as follows:-

 public class TestInfo
{
    public int Id { get; set; }
    public string TestNumber { get; set; }
}       



public string TestUpdate(TestInfo objTest)
    {
        int retVal = 0;
        try
        {
            dataContext = new AccretiveAPIContext(GetConnection(locationId));
            retVal = dataContext.Database.ExecuteSqlCommand(
                     "EXEC usp_TestUpdate @ID, @TestNumber",
                     new SqlParameter("@ID", objTest.Id),
                     new SqlParameter("@TestNumber", objTest.TestNumber),

                 );
        }
        catch (Exception)
        {
            throw;
        }
        return retVal.ToString();
    }

Please tell the changes I need to make to make "@TestNumber", objTest.TestNumber as optional parameter. I want to set its default value to zero ("0").

Please help.

3
  • 1
    You would do this on the creation of the stored procedure. Or if you did want to do it here couldn't you just use a ternary statement? new SqlParameter("@TestNumber", objTest.TestNumber == "" ? objTest.TestNumber : 0) Commented Feb 29, 2016 at 13:08
  • I want to make changes in code level not at procedure level Commented Feb 29, 2016 at 13:10
  • That isn't the way you should do this. Make it optional at procedure. Commented Feb 29, 2016 at 13:13

2 Answers 2

6

Or even easier -

            retVal = dataContext.Database.ExecuteSqlCommand(
                 "EXEC usp_TestUpdate @ID, @TestNumber",
                 new SqlParameter("@ID", objTest.Id),
                 new SqlParameter("@TestNumber", string.IsNullOrEmpty(objTest.TestNumber) ? "0" : objTest.TestNumber),
Sign up to request clarification or add additional context in comments.

4 Comments

my one parameter is integer, how can I use the above statement
Not sure what you mean? Your TestNumber property is a string and the Id is an integer.
if I change "string TestNumber { get; set; }" to "int TestNumber { get; set; }"
You could then make it a nullable int? and then the code would change to new SqlParameter("@TestNumber", objTest.TestNumber ?? 0)
4

Change your TestInfo class as following:

public class TestInfo
{
    private string _testNumber;

    public int Id { get; set; }
    public string TestNumber
    {
        get
        {
            if (string.IsNullOrEmpty(_testNumber))
            {
                _testNumber = "0";
            }
            return _testNumber;
        }
        set { _testNumber = value; }
    }
}

You could also modify your method and say something like:

var testNumber = string.IsNullOrEmpty(objTest.TestNumber) ? "0" : obj.TestNumber;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.