• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

search in c#

 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ?
 
reply
    Bookmark Topic Watch Topic
  • New Topic