>Besides, i can use both textbox to search my database at the same time.
if you can search your database, what is the question?
serkan sendur
Postaholic
2,062 posts since Jan 2008
Reputation Points: 854
Solved Threads: 127
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).
serkan sendur
Postaholic
2,062 posts since Jan 2008
Reputation Points: 854
Solved Threads: 127
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:
"SELECT * FROM BOOK_DB WHERE BookName =" + nameOfNameTextBox.Text.ToString()
or
"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!
IdanS
Junior Poster in Training
96 posts since Jun 2009
Reputation Points: 22
Solved Threads: 13
You are more or less the right way.
A problem i can see in your code is in the line:
SqlCommand cmd = new SqlCommand("SELECT * from book WHERE dateofpurchase = '" + txtDate.Text.ToString());
It needs to be only ", and not '";
And after you have done:SqlDataReader dr;
dr = cmd.ExecuteReader();
You need to read the data so use
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?
IdanS
Junior Poster in Training
96 posts since Jun 2009
Reputation Points: 22
Solved Threads: 13
Wrap up source code with BB code tags.
Read this sample:
DateTime dt;
// if date is invalid DateTime.MinValue will be assigned to dt
DateTime.TryParse(textBox1.Text, out dt);
string q = "";
if (dt == DateTime.MinValue)
{
q = "select * from table1 where name like '" + textBox1.Text + "%'";
}
else
{
q = "select * from table1 where mydate='" + textBox1.Text + "'";
}
__avd
Posting Genius (adatapost)
8,647 posts since Oct 2008
Reputation Points: 2,136
Solved Threads: 1,241
the examples above are vulnerable to sql injection. user can enter ' into textbox and can drop the database.
serkan sendur
Postaholic
2,062 posts since Jan 2008
Reputation Points: 854
Solved Threads: 127
serkan sendur offcourse it vulnerable to sql injection,
but i dont know if you noticed above is a beginner to c#,
and as it seems he is a beginner in programing as well, so we are not dealing with vulnerabilty here, he simply wants to start to learn.
IdanS
Junior Poster in Training
96 posts since Jun 2009
Reputation Points: 22
Solved Threads: 13
you should add the parameters like below :
using System;
using System.Drawing;
using System.Collections;
using System.Data;
using System.Data.SqlClient;
using System.Data.SqlTypes;
public class MainClass
{
[STAThread]
static void Main()
{
string cstr = "server=(local)\\SQLEXPRESS;database=MyDatabase;Integrated Security=SSPI;";
using ( SqlConnection conn = new SqlConnection( cstr ) )
{
conn.Open();
string selstr = "select FirstName from Employee where lastname = @name";
SqlCommand cmd = new SqlCommand( selstr, conn );
SqlParameter name = cmd.Parameters.Add( "@name", SqlDbType.NVarChar, 15 );
name.Value = "Tang";
SqlDataReader rdr = cmd.ExecuteReader();
if ( rdr.Read() )
{
Console.WriteLine(rdr.GetString( 0 ) );
}
else
{
Console.WriteLine("not available yet" );
}
}
}
}
"Tang" will be your textbox.text.
serkan sendur
Postaholic
2,062 posts since Jan 2008
Reputation Points: 854
Solved Threads: 127
serkan sendur offcourse it vulnerable to sql injection,
but i dont know if you noticed above is a beginner to c#,
and as it seems he is a beginner in programing as well, so we are not dealing with vulnerabilty here, he simply wants to start to learn.
i posted an example as how to create parameters in secure way. this way it will be protected against injection.
serkan sendur
Postaholic
2,062 posts since Jan 2008
Reputation Points: 854
Solved Threads: 127
dummy c# you are now using two different methods of polling the data from the DB, either you want to use DataBind or you want to use SqlDataReader, choose one and we will continue from there.
and you have not still added the:
dr.Read()
Method
IdanS
Junior Poster in Training
96 posts since Jun 2009
Reputation Points: 22
Solved Threads: 13
i posted an example as how to create parameters in secure way. this way it will be protected against injection.
WOW you are so centered in your self that you quote me and tell me sonthing else, I KNOW HOW TO DO IT IN A SECURE WAY, BUT ITS NOT THE ISSUE...!!!
IdanS
Junior Poster in Training
96 posts since Jun 2009
Reputation Points: 22
Solved Threads: 13
what were you expecting ? am i going to be "you" centered, of course i am self-centered.
if you teach someone something, why not teach it correct from the beginning?
serkan sendur
Postaholic
2,062 posts since Jan 2008
Reputation Points: 854
Solved Threads: 127
Becuase when you teach a baby to talk you are not teaching him how to say "encyclopedia" from the begining you teaching him how to say "dad", "mom" and so on, when he learned that so you going and teach him how to say "cow", "cat" and after you teach him to say "encyclopedia".
I'm sure when you started to learn programing you didnt learn how to id in a secure way from the begining you learned the basicics and then you developed to what you know today, so take it easy with the fellow
IdanS
Junior Poster in Training
96 posts since Jun 2009
Reputation Points: 22
Solved Threads: 13
I dont think so, even in the microsoft websites beginning tutorials for novices, it teaches how to do it proper way from the beginning. Using one more object is better than making quotation mistakes in a select statement as well as security issue.
serkan sendur
Postaholic
2,062 posts since Jan 2008
Reputation Points: 854
Solved Threads: 127
Well obviously you are not a perent and not a teacher either.
If you still think that you are right so stay in your mind, I cant force you to change the way you think, but I'm sure that people here will agree with me.
IdanS
Junior Poster in Training
96 posts since Jun 2009
Reputation Points: 22
Solved Threads: 13