0

I'm using ASP.NET Razor.

<form style="display:inline" name="formular1" method="post" action="default.cshtml">
                           <select name="phone1" class="dropdown">
                            @foreach(var row in db.Query("SELECT * FROM Handy")){
              <option value="@row.Handyname">@row.Handyname</option>
            }
                                        </select>
        vs.
                           <select name="phone2" class="dropdown">
                                            @foreach(var row in db.Query("SELECT * FROM Handy")){
              <option value="@row.Handyname">@row.Handyname</option>
            }
                                        </select>
        <input type="submit"/ value="Compare">
        </form>
        @{
            var phoneOne = "";
            var phoneTwo = "";
                    if(IsPost){

                        // request input of the select forms
                        phoneOne = Request["phone1"];
                        phoneTwo = Request["phone2"];  
                    }
                }                                     
    </div>

    <div class="content">
        <div class="start"> 
            <p><h2>@phoneOne</h2></p>
            <ul>
            @{
                if(IsPost){
                    foreach(var row in db.Query("SELECT * FROM Handy WHERE Handyname=@phoneOne")){
              <li>processor: @row.Prozessor GHz</li>
                <li>memory: @row.RAM MB Ram</li>
                <li>weight: @row.Gewicht g</li>
                <li>display: @row.Display ''</li>
                <li>OS: @row.OS</li>
            }
                }
            }


            </ul>
        </div>

Getting an error with the query: WHERE Handyname=@phoneOne ...leaving it out all works fine. What am I doing wrong?

Thanks:)!

gausss
  • 343
  • 2
  • 4
  • 16

3 Answers3

1

Not very sure, but I think you need to replace this:

foreach(var row in db.Query("SELECT * FROM Handy WHERE Handyname=@phoneOne"))

With this:

foreach(var row in db.Query("SELECT * FROM Handy WHERE Handyname= " + phoneOne))
goenning
  • 6,514
  • 1
  • 35
  • 42
  • 1
    If you take that route, you will need to be very careful to prevent [SQL injection attacks](http://en.wikipedia.org/wiki/SQL_injection). – Rick Liddle Jul 22 '11 at 23:54
1

As described here, try the following:

foreach(var row in db.Query("SELECT * FROM Handy WHERE Handyname=@@phoneOne")){
Community
  • 1
  • 1
Rick Liddle
  • 2,684
  • 19
  • 31
0

Is db a reference to the database component in Razor? If so, it uses @0, @1, (indexes) not named parameters instead.

foreach(var row in db.Query("SELECT * FROM Handy WHERE Handyname=@0"))

And pass in the value through the collection of parameters in that method too.

Brian Mains
  • 50,520
  • 35
  • 148
  • 257