Mehdi Haghgoo
Mehdi Haghgoo

Reputation: 3484

OleDbCommand select does not return expected rows

I have two Access tables, namely Projects, including the rows of projectTitle and partyID, and ProjectParty, including the rows of title and ID.

 private void btnSearch_Click(object sender, EventArgs e)
        {
            OleDbConnection conn = new OleDbConnection();
            conn.ConnectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=HesabKetab.accdb;Persist Security Info=False;";

            //search in the database
            OleDbCommand oleCmd = new OleDbCommand();
            oleCmd.Connection = conn;
            if (radioBtnByTitle.Checked)
            {
                oleCmd.CommandText = "SELECT * FROM Projects WHERE projectTitle=@projectTitle";
                oleCmd.Parameters.AddWithValue("@projectTitle", txtProjectTitle.Text);
            }
            else if (radioBtnByParty.Checked)
            {
                oleCmd.CommandText = "SELECT * FROM Projects WHERE partyID=@partyID";
                oleCmd.Parameters.AddWithValue("@partyID", comboParty.SelectedValue.ToString());
            }

            //execute query
            OleDbDataAdapter ole_da = new OleDbDataAdapter(oleCmd);
            DataTable dt= new DataTable();
            try
            {
                conn.Open();
                ole_da.Fill(dt);

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
            dataGridViewDisplaySearchResults.DataSource = dt;          
            conn.Close();

        }

In the above code I am trying to retrieve the values of the Projects Access database table. The second if is successful and it loads the queried rows into DataGridView. But the first if (when true) does not return the expected values. In fact, it loads nothing into the DataGridView. I have no idea why the query does not work when I try to do the select based on projectTitle. I tried debugging but I got no clue which parameters were being passed to the select command. Where am I wrong?

Upvotes: 0

Views: 977

Answers (1)

Ginish
Ginish

Reputation: 191

instead txtProjectTitle.ToString() in the first condition, isn't it txtProjectTitle.Text

Upvotes: 1

Related Questions