I have created a database within Webmatrix, it is one Table called Fish. It stores a number of fish, where they are caught, how they are caught etc. I created a front end within Webmatrix too so that it will be possible to search the database. When I type a fish into the search box and then hit search it throws up an error which I cannot figure out. My Database is called 'Sourcing Matrix' and the table is called 'Fish'. Here is the code I have done for the search function;
@{ var db = Database.Open("Sourcing Matrix"); var selectCommand = "SELECT * FROM Fish"; var searchTerm = "";
if(!Request.QueryString["searchField"].IsEmpty() ) { selectCommand = "SELECT * FROM Fish WHERE Fish = @0"; searchTerm = Request.QueryString["searchField"]; } if(IsPost){ var selectedData = db.Query(selectCommand, searchTerm); } } <h1>Search for Fish stored in the Database</h1>Search: Search
@if(!Request.QueryString["searchField"].IsEmpty() ){ foreach(var row in db.Query(selectCommand, searchTerm)) { <div class="col_12 box"> <form method="post"> <!--Entry in Search box--> </form> </div> } } else { <div class="col_12 box"> <form method="post"> <!--No entry in Search Box--> </form> </div> }
Here is the error which is returned:
The column name is not valid. [ Node name (if any) = ,Column name = Fish ] Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.Data.SqlServerCe.SqlCeException: The column name is not valid. [ Node name (if any) = ,Column name = Fish ]
Source Error:
Line 25: Line 26:
@if(!Request.QueryString["searchField"].IsEmpty() ){ Line 27:
foreach(var row in db.Query(selectCommand, searchTerm)) { Line 28:
Line 29:
I have tried various different column names, they stop the error from happening but they wont return any results either. The only thing which seems logical to me would be to have the whole table to select from, in case the common name, latin name or catch area is searched for, and not just info from one column.