[RESOLVED]Advice on if Statement or Switch (UrlData)

Hey guys,

I am wanting to know the best way of changing a variable based on the UrlData input.

For example if I go to: http://app/View/1

I want to run a specific query (or change a var) based on the value - likewise if I put in another number, etc. 

What would be the best method to achieve this? I have done a bit of googling but it’s hard to articulate.. Let me know if I can clarify more, as always any assistance is much appreciated.

Cheers, Aaron

Not sure what you are asking, but the following code illustrates a basic if statement based on the value of the UrlData.

if(UrlData[0].AsInt() == 1)
{
    //do one thing
}
else
{
   //do another
}

Mikesdotnetting

Not sure what you are asking, but the following code illustrates a basic if statement based on the value of the UrlData.

if(UrlData[0].AsInt() == 1)
{
    //do one thing
}
else
{
   //do another
}

Thanks Mike.. This is what I am trying to do..

if(UrlData[0].AsInt() == 1)
{
    var GrabStuff = db.QuerySingle(@"SELECT * FROM Table1 WHERE Type=@0", Id) 
}
else
{
   var GrabStuff = db.QuerySingle(@"SELECT * FROM Table2 WHERE Type=@0", Id) 
}

I know (without trying) that this won’t work but hopefully you understand where I am going with it?

You need to declare the GrabStuff variable outside of the if or switch statemetn:

dynamic GrabStuff;
if(UrlData[0].AsInt() == 1)
{
    GrabStuff = db.QuerySingle(@"SELECT * FROM Table1 WHERE Type=@0", Id) 
}
else
{
   GrabStuff = db.QuerySingle(@"SELECT * FROM Table2 WHERE Type=@0", Id) 
}

Mikesdotnetting

You need to declare the GrabStuff variable outside of the if or switch statemetn:

dynamic GrabStuff;
if(UrlData[0].AsInt() == 1)
{
    GrabStuff = db.QuerySingle(@"SELECT * FROM Table1 WHERE Type=@0", Id) 
}
else
{
   GrabStuff = db.QuerySingle(@"SELECT * FROM Table2 WHERE Type=@0", Id) 
}

 

Thanks again Mike. I have put together a quick concept, to test the code – It looks like this:

@{
       var db= Database.Open("Data");
       var Id=UrlData[0].AsInt();
       
       dynamic GrabStuff;

if(UrlData[0].AsInt() == 1)
{   GrabStuff = db.QuerySingle("SELECT * FROM Table1 WHERE Id=@0", Id); }
else
{   GrabStuff = db.QuerySingle("SELECT * FROM Table2 WHERE Id=@0", Id); }

       }
<div>
@foreach(var row in GrabStuff){

    <p>@row.Name</p>
       }
</div>

I seem to be getting this error when I go to the application URL (http://localhost:37371/test/1) .. Cannot implicitly convert type ‘WebMatrix.Data.DynamicRecord’ to ‘System.Collections.IEnumerable’ on Line
14 

Is there something I have stuffed up here? I am tipping that is likely..

Cheers!

The QuerySingle method returns a single object. If you expect your query to return multiple rows, you should use the Query method. You will also need to change the data type of GrabStuff to IEnumerable<dynamic>:

@{
    var db= Database.Open("Data");
    var Id=UrlData[0].AsInt();
       
    IEnumerable<dynamic> GrabStuff;

    if(UrlData[0].AsInt() == 1)
    {
       GrabStuff = db.Query("SELECT * FROM Table1 WHERE Id=@0", Id); 
    }
    else
    {   
        GrabStuff = db.Query("SELECT * FROM Table2 WHERE Id=@0", Id); 
    }
}
<div>
@foreach(var row in GrabStuff){

    <p>@row.Name</p>
       }
</div>

If you only expect one row of data to be returned, then the QuerySingle method is the correct one to use, but you shouldn’t try to iterate rows (because there is only one):

@{
    var db= Database.Open("Data");
    var Id=UrlData[0].AsInt();
       
    dynamic GrabStuff;

    if(Id == 1)
    {   
        GrabStuff = db.QuerySingle("SELECT * FROM Table1 WHERE Id=@0", Id); 
    }
    else
    {   
        GrabStuff = db.QuerySingle("SELECT * FROM Table2 WHERE Id=@0", Id); 
    }
}
<div>
    @row.Name
</div>

Mikesdotnetting

The QuerySingle method returns a single object. If you expect your query to return multiple rows, you should use the Query method. You will also need to change the data type of GrabStuff to IEnumerable<dynamic>:

@{
    var db= Database.Open("Data");
    var Id=UrlData[0].AsInt();
       
    IEnumerable<dynamic> GrabStuff;

    if(UrlData[0].AsInt() == 1)
    {
       GrabStuff = db.Query("SELECT * FROM Table1 WHERE Id=@0", Id); 
    }
    else
    {   
        GrabStuff = db.Query("SELECT * FROM Table2 WHERE Id=@0", Id); 
    }
}
<div>
@foreach(var row in GrabStuff){

    <p>@row.Name</p>
       }
</div>

If you only expect one row of data to be returned, then the QuerySingle method is the correct one to use, but you shouldn’t try to iterate rows (because there is only one):

@{
    var db= Database.Open("Data");
    var Id=UrlData[0].AsInt();
       
    dynamic GrabStuff;

    if(Id == 1)
    {   
        GrabStuff = db.QuerySingle("SELECT * FROM Table1 WHERE Id=@0", Id); 
    }
    else
    {   
        GrabStuff = db.QuerySingle("SELECT * FROM Table2 WHERE Id=@0", Id); 
    }
}
<div>
    @row.Name
</div>

I made some changes as per your recommendations – I am using the first example. However I am now getting an error ‘Use of unassigned local variable ‘GrabStuff’  

Fixed it! Added a null in there.. 

@{
    var db= Database.Open("Data");
    var Id=UrlData[0].AsInt();
       
    IEnumerable<dynamic> GrabStuff = null;

    if(UrlData[0].AsInt() == 1)
    {
       GrabStuff = db.Query("SELECT * FROM Table1 WHERE Id=@0", Id); 
    }
    else
    {   
        GrabStuff = db.Query("SELECT * FROM Table2 WHERE Id=@0", Id); 
    }
}
<div>
@foreach(var row in GrabStuff){

    <p>@row.Name</p>
       }
</div>

Thanks Mike!!!!!

Leave a Reply