943,654 Members | Top Members by Rank

Ad:
  • C# Discussion Thread
  • Unsolved
  • Views: 4103
  • C# RSS
You are currently viewing page 1 of this multi-page discussion thread
Jun 28th, 2009
0

Simply Search Engine

Expand Post »
i am newbie in C#. i would like to make a button to function as search button to search by name and search by date. When i key in data inside textbox1 for search by name ,then press search ,can get the data from database. Same case to textbox2 for search by date, then press search. Besides, i can use both textbox to search my database at the same time. Example ,i key in name n date together to find a book that i bought ,then press search button. Please help me. i am no idea how to start.
Similar Threads
Reputation Points: 9
Solved Threads: 0
Newbie Poster
dummy C# is offline Offline
12 posts
since Jun 2009
Jun 28th, 2009
0

Re: Simply Search Engine

>Besides, i can use both textbox to search my database at the same time.

if you can search your database, what is the question?
Featured Poster
Reputation Points: 854
Solved Threads: 127
Banned
serkan sendur is offline Offline
2,057 posts
since Jan 2008
Jun 29th, 2009
0

Re: Simply Search Engine

>Besides, i can use both textbox to search my database at the same time.

if you can search your database, what is the question?
sorry. i mean i want to search from my database. i not yet start with the code
Reputation Points: 9
Solved Threads: 0
Newbie Poster
dummy C# is offline Offline
12 posts
since Jun 2009
Jun 29th, 2009
0

Re: Simply Search Engine

asking this kind of questions is not good. you are not stuck with a problem. you need to grab an ado.net book and read first. or basically go to windowsclient.net and watch video tutorials to learn about datadriven applications. i dont want to write code sample for this because there are milions of them out there. the concepts you are looking for are these : System.Data, System.Data.SqlClient name spaces. then you need SqlConnection and or SqlDataAdapter objects. SqlCommand object maybe. then you need to write your Select statement using SqlParameter object(it will prevent sql injection attacks).
Featured Poster
Reputation Points: 854
Solved Threads: 127
Banned
serkan sendur is offline Offline
2,057 posts
since Jan 2008
Jun 29th, 2009
0

Re: Simply Search Engine

ok. thx alot.
Reputation Points: 9
Solved Threads: 0
Newbie Poster
dummy C# is offline Offline
12 posts
since Jun 2009
Jun 29th, 2009
0

Re: Simply Search Engine

Hi,

What you need to do is to build your database, after its ready start working on your program.

When you start working on your program, put on your form two textbox and two buttons (when you have different search engine its better to work with different controls for each engine).

Give the textboxs and the buttons names that defined them,
after that press double click on the first button, it will create for you the event method to be fired when the user will press the button, when you have this event method write the code inside.
The code needs to be somthing like that if you using SQL server database:
C# Syntax (Toggle Plain Text)
  1. "SELECT * FROM BOOK_DB WHERE BookName =" + nameOfNameTextBox.Text.ToString()
or
C# Syntax (Toggle Plain Text)
  1. "SELECT * FROM BOOK_DB WHERE BookDate =" + nameOfDateTextBox.Text.ToString()
If not and you dont know how to build your select function for yours database so ask us.

I assum that you know where the querys need to go but if you dont so ask and we will help you.

Of course its just en example and you need to change the query to your needs, but as i said above if you need any help on that two, so ask and we will help you!
Reputation Points: 22
Solved Threads: 13
Junior Poster in Training
IdanS is offline Offline
96 posts
since Jun 2009
Jun 29th, 2009
0

Re: Simply Search Engine

tq for ur reply. i dont really know how the search engine concept work. pls help me recorrect my code below. thank you in advance.


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.Configuration;

namespace WindowsFormsApplication7
{
public partial class Form7 : Form
{
public Form7()
{
InitializeComponent();
}

private void button1_Click(object sender, EventArgs e)
{

SqlConnection myConn = new SqlConnection();
myConn.ConnectionString = ConfigurationManager.ConnectionStrings["myConn"].ToString();

myConn.Open();

if (txtTitle.Text != "")
{
SqlCommand cmd = new SqlCommand("SELECT * from book WHERE title = '" + txtTitle.Text.ToString());

SqlDataReader dr;
dr = cmd.ExecuteReader();

dr.Close();
}
else if (txtDate.Text != "")
{
SqlCommand cmd = new SqlCommand("SELECT * from book WHERE dateofpurchase = '" + txtDate.Text.ToString());

SqlDataReader dr;
dr = cmd.ExecuteReader();

dr.Close();
}


myConn.Close();




}
}
}
Reputation Points: 9
Solved Threads: 0
Newbie Poster
dummy C# is offline Offline
12 posts
since Jun 2009
Jun 29th, 2009
0

Re: Simply Search Engine

You are more or less the right way.
A problem i can see in your code is in the line:
Quote ...
SqlCommand cmd = new SqlCommand("SELECT * from book WHERE dateofpurchase = '" + txtDate.Text.ToString());
It needs to be only ", and not '";

And after you have done:
Quote ...
SqlDataReader dr;
dr = cmd.ExecuteReader();
You need to read the data so use
C# Syntax (Toggle Plain Text)
  1. dr.Read();
To read what you set above

Now what more help do you need, have you tried to run this code?,
is it working?, do you get what you want?
Reputation Points: 22
Solved Threads: 13
Junior Poster in Training
IdanS is offline Offline
96 posts
since Jun 2009
Jun 29th, 2009
0

Re: Simply Search Engine

Wrap up source code with BB code tags.

Read this sample:
C# Syntax (Toggle Plain Text)
  1. DateTime dt;
  2. // if date is invalid DateTime.MinValue will be assigned to dt
  3. DateTime.TryParse(textBox1.Text, out dt);
  4. string q = "";
  5. if (dt == DateTime.MinValue)
  6. {
  7. q = "select * from table1 where name like '" + textBox1.Text + "%'";
  8. }
  9. else
  10. {
  11. q = "select * from table1 where mydate='" + textBox1.Text + "'";
  12. }
Moderator
Reputation Points: 2136
Solved Threads: 1228
Posting Genius
adatapost is offline Offline
6,527 posts
since Oct 2008
Jun 29th, 2009
1

Re: Simply Search Engine

the examples above are vulnerable to sql injection. user can enter ' into textbox and can drop the database.
Featured Poster
Reputation Points: 854
Solved Threads: 127
Banned
serkan sendur is offline Offline
2,057 posts
since Jan 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C# Forum Timeline: Need some beginnerhelp
Next Thread in C# Forum Timeline: nifty little counter problem (timer involved)





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC