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

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

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

Recommended Answers

All 4 Replies

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..'

commented: Thanx dude +2

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);

Here is code for your reference.

string cnstr = "data source=abc;user id=hr;password=hr";

           // append pattern mach character
            textBox1.Text = textBox1.Text + "%";  
           // Use & - prefix with parametername
            string sql = "select * from employees where first_name like 
                                 &test";
           // No need to set reference of connecton object.
            OracleDataAdapter adp = new OracleDataAdapter(sql, 
                                          cnstr);
            adp.SelectCommand.Parameters.Add("&test", 
                                                               OracleType.VarChar );
            adp.SelectCommand.Parameters[0].Value = textBox1.Text;

            DataTable dt = new DataTable();
            // Fill method automatically opens & close the connection
            adp.Fill(dt);
            DataGrid1.DataSource = dt;    
            DataGrid1.DataBind();

i have a problem on searching data from table and displaying it into the textbox or label...pls help me on this problem tnx..

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.