0

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?

1
  • For the first if (when true), I get only an empty row inside the dataGridViewDisplaySearchResults Commented Feb 15, 2015 at 9:33

1 Answer 1

1

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

Sign up to request clarification or add additional context in comments.

3 Comments

Thank you. I fixed it, but still get nothing in datagridview.
your code is working fine. May be some space in the projectTitle value in db ? have you tried with different values?
Yes, thanks. It works with other values. What is the word on that? I don't understand :(

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.