• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 379
  • Last Modified:

Simple search function in ASP.NET and C#


I want a simple search function:

-Preferably I want the search function to be available in all my pages, but it's no worries if I have a dedicated page where the search function fires from
-Search the field "Navn" in my "Pasientbok" table
-Search from a textbox
-Display it on a searchresultspage

I have tried this on my search page:
public string Sok
                        return txtSok.Text;


            private void Submit1_Click(object sender, System.EventArgs e)

This on my search results page:
public Default fp;
private void Page_Load(object sender, System.EventArgs e)
                  if (!IsPostBack)
                        fp = (Default) Context.Handler;

string strCommand = ("Select * From Pasientbok WHERE Navn LIKE '%fp.Sok%' ORDER BY Navn");

What's missing or wrong??
1 Solution
In your example, fp.Sok is codes as a string in the sql command. Use something like this:

string strCommand = ("Select * From Pasientbok WHERE Navn LIKE '%" + fp.Sok + "%' ORDER BY Navn");

If you are unsure about your SQL, you can always at the next line output the strCommand, like: Console.WriteLine(strCommand); to see the actual SQL query that will be send to the database.


Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now