0

I am trying to create forms where the user can input text in up to 3 forms. Each of the forms will correspond to columns in an SQL Server CE database table. Based on the text input into the forms, the page will display all rows that contain all of the specified text (with null values being treated as wildcards).

I have this working fine for 1 form. The form text is being assigned as a value for a variable and passed between the pages no problem. I am just not sure how in include the 2 other columns in the query.

Here is the code for the input page with the forms:

@{ 
var formValue1 = Request.Form["FN"];
var formValue2 = Request.Form["LN"];
var formValue3 = Request.Form["CPNY"];  
if (IsPost) 
 { 
       Response.Redirect("DisplaySearch.cshtml?firstname=" + formValue1);
       Response.Redirect("DisplaySearch.cshtml?lastname=" + formValue2);       
 } 
 }

<form action="" method="post">
First Name:<input type="text" ID="FN" name="FN" value=""></input><br>
Last Name:<input type="text" ID="LN" name="LN"></input><br>
Company:<input type="text" ID="CPNY" name="CPNY"></input>
<br>
<input type="submit" value="Search"></input></form>

Here is the code for the display page:

@using WebMatrix.Data 

@{
var Keyword1 = Request.QueryString["firstname"];    //Retrieves passed variable from    the database search page for First Names
var Keyword2 = Request.QueryString["lastname"];    //Retrieves passed variable from the database search page for First Names
var Keyword3 = Request.QueryString["company"];    //Retrieves passed variable from the database search page for First Names
var db= Database.Open("Orientation"); 
var sqlQ = "SELECT * FROM Orientation WHERE FirstName LIKE @0";
var data = db.Query(sqlQ,  "%" + Keyword1 + "%");  
 }

<div class="content-box-main">

<ol>
@foreach(var row in data){
<li>
@row.LastName, @row.FirstName, @row.MiddleName, @row.Email, @row.Company, @row.Date
</li>
}
</ol><br>

</div>

Help is greatly appreciated! I am a noob to create web applications and my only programming experience up to 2 weeks ago was the odd VBA macro in Office. I will also mention again, I am using WebMatrix and SQL Server CE. I know that there are some limitations with SQL Server CE, although I enjoy the ease of use for me being a noob. If anyone has any suggestions for different SQL Server program to use, I would be open to hearing them.

Thanks!

James

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459

1 Answers1

0
   Response.Redirect("DisplaySearch.cshtml?firstname=" + formValue1 + "&lastname=" + formValue2 + "&company=" + formValue3);

and

var sqlQ = "SELECT * FROM Orientation WHERE FirstName LIKE @0 AND LastName LIKE @1 AND Company LIKE @2";
var data = db.Query(sqlQ,  "%" + Keyword1 + "%", "%" + Keyword2 + "%", "%" + Keyword3 + "%");
sblom
  • 26,911
  • 4
  • 71
  • 95
  • Thanks sblom! That worked like a charm! I have a "bonus" question for you too if you up for it. What if I wanted to display records on page load that were entered within in a dynamic date range (like today - 2 months)? Assuming that I have a column called "Date" that records a datestamp when the record is entered. – user1544485 Jul 22 '12 at 21:52
  • I'd recommend asking that as a separate question. I have to run right now, but someone else can probably help you out--it'll be pretty straightforward. – sblom Jul 22 '12 at 21:56