Search and Desplay on DataGrid

Please support our ASP.NET advertiser: Intel Parallel Studio Home
Reply

Join Date: Mar 2008
Posts: 268
Reputation: Traicey is an unknown quantity at this point 
Solved Threads: 19
Traicey's Avatar
Traicey Traicey is offline Offline
Posting Whiz in Training

Search and Desplay on DataGrid

 
0
  #1
Jun 19th, 2009
Hi all

I have a datagrid filled with data from Oracle database, I have a textbox and a button search and on the textbox a person/user can enter the Title of the book he/she wana search the the results should be displayed in the datagrid

here is the code on btnSearch

  1. using (OracleConnection conn = new OracleConnection(stringConn))
  2. {
  3. string sql = "select BookID, ISBN, Title, PublishedDate, Edition, Publisher, DepartmentName from Books where Title like '%Title%'";
  4. OracleDataAdapter adap = new OracleDataAdapter(sql, conn);
  5. adap.SelectCommand.Parameters.Add("Title", OracleType.VarChar).Equals(txtSearch.Text);
  6. adap.SelectCommand.Connection.Open();
  7. adap.Fill(dt);
  8. }
  9. DataGrid1.DataSource = dt;
  10. DataGrid1.DataMember = "Books";
  11. DataGrid1.DataBind();
  12. }

but now when I click btnSearch the datagrid which is filled with all the data from the book table just got cleared, instead of desplaying all the books with the title entered in the textbox, please tell me what Im doing wrong

Thanks in advance
Some people get so rich they lose all respect for humanity. That's how rich I want to be.
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 2,609
Reputation: adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of 
Solved Threads: 460
Moderator
adatapost's Avatar
adatapost adatapost is offline Offline
Posting Maven

Re: Search and Desplay on DataGrid

 
1
  #2
Jun 19th, 2009
Some confusion:

1. Do now show the data when user leave textbox empty and click
on button.
For this case, do not execute you code - use if statement to
check the value of textbox.
2. Show a record for the book title entered into textbox.
Do not use like '..%' query. Use where columnname='somethig..'
Last edited by adatapost; Jun 19th, 2009 at 8:33 am. Reason: Spell mistake
Failure is not fatal, but failure to change might be. - John Wooden
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 268
Reputation: Traicey is an unknown quantity at this point 
Solved Threads: 19
Traicey's Avatar
Traicey Traicey is offline Offline
Posting Whiz in Training

Re: Search and Desplay on DataGrid

 
0
  #3
Jun 19th, 2009
String sql = "Select * From Book Where Title = " + txtSearch.Text
I dont know why I didnt think about that, I guess it is true that inexperience programmers like complicating things

Thanks dude, hope Oracle is gonna love it coz if he does it will goodbye to this line
adap.SelectCommand.Parameters.Add("Title", OracleType.VarChar).Equals(txtSearch.Text);
Last edited by peter_budo; Jun 19th, 2009 at 12:47 pm. Reason: Keep It Organized - For easy readability, always wrap programming code within posts in [code] (code blocks) and [icode] (inline code) tags.
Some people get so rich they lose all respect for humanity. That's how rich I want to be.
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 2,609
Reputation: adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of 
Solved Threads: 460
Moderator
adatapost's Avatar
adatapost adatapost is offline Offline
Posting Maven

Re: Search and Desplay on DataGrid

 
0
  #4
Jun 19th, 2009
Here is code for your reference.

  1. string cnstr = "data source=abc;user id=hr;password=hr";
  2.  
  3. // append pattern mach character
  4. textBox1.Text = textBox1.Text + "%";
  5. // Use & - prefix with parametername
  6. string sql = "select * from employees where first_name like
  7. &test";
  8. // No need to set reference of connecton object.
  9. OracleDataAdapter adp = new OracleDataAdapter(sql,
  10. cnstr);
  11. adp.SelectCommand.Parameters.Add("&test",
  12. OracleType.VarChar );
  13. adp.SelectCommand.Parameters[0].Value = textBox1.Text;
  14.  
  15. DataTable dt = new DataTable();
  16. // Fill method automatically opens & close the connection
  17. adp.Fill(dt);
  18. DataGrid1.DataSource = dt;
  19. DataGrid1.DataBind();
Failure is not fatal, but failure to change might be. - John Wooden
Reply With Quote Quick reply to this message  
Join Date: Sep 2009
Posts: 2
Reputation: obicerno is an unknown quantity at this point 
Solved Threads: 0
obicerno obicerno is offline Offline
Newbie Poster

Re: Search and Desplay on textbox or label

 
0
  #5
Sep 23rd, 2009
i have a problem on searching data from table and displaying it into the textbox or label...pls help me on this problem tnx..
Reply With Quote Quick reply to this message  
Reply

Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC