Hi all,
I want to do a search page for library using visual web developer and it has several text boxs. The user can search by the keyword, title, author, publisher, .. others..
I can search using only one option. For example, if the user write the title i the Title textbox, it will give the result.
But if the user want to display all the records that has the specific title and aurthor, he can't ..
I writ the code, but it is not work ( it works in PHP, but in C# doesn't work !) ;;
This is my code :
protected void SearchButton_Click(object sender, EventArgs e)
{
String strKeyWord = KeywrodTextBox.Text;
String strTitle = TitleTextBox.Text;
String strAut = AuthorTextBox.Text;
String strPub = PubTextBox.Text;
String strSubj = SubTextBox.Text;
String strYear = YearTextBox.Text;
// This is work ..
if (strKeyWord.Length > 0)
{
GridView1.Visible = true;
con = new SqlConnection(strConn);
query = "Select * From BooksRecord where (Title Like '%" + KeywrodTextBox.Text.ToString() + "%') OR (Author Like '%" + KeywrodTextBox.Text.ToString() + "%') OR (Publisher Like '%" + KeywrodTextBox.Text.ToString() + "%') OR (Subject Like '%" + KeywrodTextBox.Text.ToString() + "%') OR (Year Like '%" + KeywrodTextBox.Text.ToString() + "%')";
com = new SqlCommand(query, con);
objAdapter = new SqlDataAdapter(com);
objDataSet = new DataSet();
objAdapter.Fill(objDataSet);
GridView1.DataSource = objDataSet;
GridView1.DataBind();
con.Close();
FillFields.Visible = false;
}
// This is not working ..
else if (strTitle.Length > 0 && strAut.Length > 0)
{
GridView1.Visible = true;
con = new SqlConnection(strConn);
query = "Select * From BooksRecord where (Title Like '%" + TitleTextBox.Text.ToString() + "%') AND (Author Like '%" + AuthorTextBox.Text.ToString() + "%')";
com = new SqlCommand(query, con);
objAdapter = new SqlDataAdapter(com);
objDataSet = new DataSet();
objAdapter.Fill(objDataSet);
GridView1.DataSource = objDataSet;
GridView1.DataBind();
con.Close();
FillFields.Visible = false;
}
else
{
FillFields.Visible = true;
GridView1.Visible = false;
}
I tried to use AND & OR for the command, but it doesn't display the records that contains the information on the 2 or more text boxs
Can some one help me, please ?