...

View Full Version : Returning Search Results



bdenny20
02-02-2013, 02:16 AM
Hi again,
Require some more help.
So, I'm using WebMatrix as a learning platform with ASP.NET. Having a good time.

I ran through the "Bakery" demo from W3Schools, and I am trying to make a database search function based on what I learned from the demo. I haven't had much luck.

Well, I made a demo database (Airports.sdf) with a table by the same name. It has 5 sample rows with 5 columns. There is an ID column, but the primary column users would use to search by is the column called 'ICAO', a four letter code.


So, from the demo, I learned about
var db = Database.Open("Airports");
var query = db.Query("SELECT * FROM Airports ORDER BY ID");

with


@foreach(var row in query)

in an HTML table. Makes all the entries show up nicely. Cool.


What I am trying to do is take a text input bar, and a submit button, and have a user be able to enter the 4 letter ICAO, and after clicking the submit button, the corresponding entry and all it's columns appear in a HTML table.

So far I haven't gotten the right phrasing of the db.Query and I don't know how to tie it to a button.

Google suggestions gave me incredibly complicated solutions that I can not understand at this point, at first I thought this was a simple process, but could I be wrong?


Thanks in advance.

Billr

Old Pedant
02-03-2013, 05:00 AM
It depends on if you want to use "pure" ASP.NET or you don't mind a bit of hacking.

If you are using pure ASP.NET, you would want to create a FormView, most likely.

If you don't mind hacking:


<html>
<body>
<form action="findAirport.aspx" method="post">
ICAO code: <input name="icao" />
<input type="submit" />
</form>
</body>
</html>
and then your "findAirport.aspx" page is dirt simple:


var db = Database.Open("Airports");
var query = db.Query("SELECT * FROM Airports " + Request.Form("icao") + " ORDER BY ID");
... etc. ...

But if you were to use ASP.NET form controls, you'd use something like


<form runat="server" >
<asp:TextBox runat="server" id="icao" />
<asp:Button OnClick="dosearch" Text="Search by ICAO" runat="server" />
</form>

and then your backend code would do:


void dosearch(Object ignored, EventArgs alsoignored)
{
var db = Database.Open("Airports");
var query = db.Query("SELECT * FROM Airports " + icao.Value + " ORDER BY ID");
...

These are glittering generalities. Read through a few more tutorials and you'll get the hang of it.

bdenny20
02-04-2013, 04:27 PM
Sorry, haven't checked in for a minute. Thanks for getting back to me. I get what you're saying, which is awesome, because normally that doesn't happen. Still pretty green.

Now, on a similar note, after I posted that, I started looking at alternatives to my goal, to kinda broaden the hypothetical horizons. I've created a MySQL database and am manipulating it with PHP. I actually did get a working form on that, real simple though, only accepts uppercase letters, exact matches (KSAN and SAN are the same thing) and nothing special happens if a non-valid entry is entered.

BUT

That's neither here nor there, I'll post something in the PHP forum later,

My question is, is there a particular advantage to using PHP and MySQL compared to ASP.NET and SQL? The only thing that really stands out is ASP.NET seems to a be a little more user friendly and easier to understand (var vs. $) Also, having phpMyAdmin is a nice tool.

Just wondering...

Old Pedant
02-04-2013, 11:37 PM
Ummm...actually, var is a terrible hack, if you are using C# for your coding language. Using var means that the compiler has to do runtime type checking, so you get a lot less efficiency than if you use the proper type names. C# added var to make it easier to code, but I hate it. It means you sacrifice performance because the programmer doesn't want to learn the language.

The real advantage to using ASP.NET is that, if you use a tool such as Web Developer Express, you can use drag-and-drop to create your web pages (the basics, that is) and then you just have to write the "backend" code to handle events on the pages.

But if you aren't using such a tool, and if performance isn't an issue, then there's not much difference between ASP.NET and PHP coding.

alykins
02-05-2013, 12:17 PM
fwiw- As Old Pedant stated, his code is a hack.... it can be refined a h*** of a lot more, but (and I assume he posted it so it would be on your playing field of understanding) there are many ways of doing this- and it all depends on your style of coding (or if you are working for someone their requirements/style of coding). For example if I were doing this I would (in the code behind) on page load (!postback) hit the db and populate a collection (maybe a datatable, if needed a custom class) and then create the appropriate html objects and litterally add them to the page at runtime. This allows you to seperate your data access layer and buisiness layers from your presentation layer. But that is a waaaaaay to much to dump on someone learning.

bdenny20
02-05-2013, 05:32 PM
Yep, as I was reading that I swear I started to smoke a little out of my ears. It's okay, I'm not doing anything for anyone. Just a little experimentation on my own behalf, so I can go at my own rate and not worry.

If there is anything I've learned for sure so far, it that's there are many ways to go about doing the same thing, and everyone's got there own method. Kind of counter productive though when someone gives me a pointer on something, and when I re-post latter on the subject, someone else is all.. "no, no, no, do it like this..." Oh well, nature of the game I suppose.

bdenny20
02-05-2013, 05:32 PM
Yep, as I was reading that I swear I started to smoke a little out of my ears. It's okay, I'm not doing anything for anyone. Just a little experimentation on my own behalf, so I can go at my own rate and not worry.

If there is anything I've learned for sure so far, it that's there are many ways to go about doing the same thing, and everyone's got there own method. Kind of counter productive though when someone gives me a pointer on something, and when I re-post latter on the subject, someone else is all.. "no, no, no, do it like this..." Oh well, nature of the game I suppose.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum