locked
ASP.net check if called SP is equal to a certain string RRS feed

  • Question

  • User-870004586 posted

    Hi

    I have the following code:

    //call SP
     using (var context = new CheckStatusEntities())
                {
                    var courses = context.CheckUrlStatus(url.url1);
     
                 //check if the result of the called sp is equal to a certain string
                //not working
                if(context.ToString() == "A")
                    {
                        ViewBag.error = "Deze url is al afgewzen";
                         //not working
                    }else if(courses.ToString() == "G") 
                    {
                        ViewBag.error = "Deze url is al aanwezig in het systeem";
                    }
                        //not working
                    else if(courses.ToString() == "V")
                    {
                        ViewBag.error = "Deze url is door een beheerder verwijderd";
                    }
                  //this works..
                    else if(courses.Count() == 0)  



    but somehow the if statement to check the result of the SP is not working .

    Can someone tell me what is wrong with my code?



    Wednesday, January 15, 2020 8:16 PM

All replies

  • User475983607 posted

    There is not enough information to answer this question accurately.  What is "courses"?  Is it an array of strings, a single string, or a character?

    Wednesday, January 15, 2020 8:32 PM
  • User-870004586 posted

    courses is just the name of the var.

    Wednesday, January 15, 2020 8:34 PM
  • User475983607 posted

    Demond

    courses is just the name of the var.


    Yeah... what type is the var? Can you share the source code for CheckUrlStatus(url.url1)?

    Wednesday, January 15, 2020 8:51 PM
  • User-870004586 posted

    it's a string.

    and this is the code for  CheckUrlStatus(url.url1)

    USE [LinkToMe]
    GO
    /****** Object:  StoredProcedure [dbo].[CheckUrlStatus]    Script Date: 1/15/2020 10:26:27 PM ******/
    SET ANSI_NULLS ON
    GO
    SET QUOTED_IDENTIFIER ON
    GO
    ALTER PROCEDURE [dbo].[CheckUrlStatus] @url nvarchar(MAX)
    AS
    SELECT status FROM url WHERE @url = url
    
    



    Wednesday, January 15, 2020 9:27 PM
  • User475983607 posted

    Demond

    it's a string.

    and this is the code for  CheckUrlStatus(url.url1)

    USE [LinkToMe]
    GO
    /****** Object:  StoredProcedure [dbo].[CheckUrlStatus]    Script Date: 1/15/2020 10:26:27 PM ******/
    SET ANSI_NULLS ON
    GO
    SET QUOTED_IDENTIFIER ON
    GO
    ALTER PROCEDURE [dbo].[CheckUrlStatus] @url nvarchar(MAX)
    AS
    SELECT status FROM url WHERE @url = url
    



    You still have not answered the questions.  I the result is always a single string then...

    using (var context = new CheckStatusEntities())
    {
    	var courses = context.CheckUrlStatus(url.url1);
    	//check if the result of the called sp is equal to a certain string //not working
    	
    	if(courses != null) {
    		if(courses[0] == "A")
    		{
    			ViewBag.error = "Deze url is al afgewzen"; //not working
    		}else if(courses[0] == "G")
    		{
    			ViewBag.error = "Deze url is al aanwezig in het systeem";
    		} //not working
    		else if(courses[0] == "V")
    		{
    			ViewBag.error = "Deze url is door een beheerder verwijderd";
    		} //this works..
    		else if(courses.Count() == 0)
    			
    	}

    Please use the Visual Studio debugger to step through your code and view what's returned from the Stored procedure call.

    https://docs.microsoft.com/en-us/visualstudio/debugger/debugger-feature-tour?view=vs-2019

    Perhaps go through a few Getting started tutorials if you are new to Web Forms.

    Wednesday, January 15, 2020 10:27 PM
  • User288213138 posted

    Hi Demond,

    var courses = context.CheckUrlStatus(url.url1);

    As AgaveJoe said, I suggest you set breakpoints to debug your code and check if courses are null.

    Best regards,

    Sam

    Thursday, January 16, 2020 9:39 AM
  • User753101303 posted

    Hi,

    Demond

    is not working

    Instead tell explicitly what happens. Do you have an exception ? Else it could be casing issue (I suspect you are comparing for exemple "v" with "V") etc...

    If confirmed use ToUpper or a string insensitive comparison. Also most often when testing for this kind of multiple cases I add a fallback case that throws (so that if later I add an extra "Z" case or whatever my code will fail to tell me the "Z" option is not yet handled by my code rather falling back silently to some default option).

    IMHO always take an extra couple of minutes to understand what happens and then to look at the code to see why THIS happens rather than looking at the code and wondering what COULD happen.

    Thursday, January 16, 2020 10:26 AM