The filters work for myTicketsSubmitButton but not work allTicketsSubmitButton. The code is the same but I don’t understand why it works for one method and not another.
I am using WinForms and C# with Visual Studio 2010
private void myTicketsSubmitButton_Click(object sender, EventArgs e)
{
String sqlQuery = "SELECT u.CallerName, t.* FROM users u INNER JOIN tickets t ON u.id = t.user WHERE u.CallerName = '" + Environment.UserName.ToLower() + "'";
GetData(sqlQuery);
if (myTicketsAllRadioButton.Checked)
{
//GetData(sqlQuery);
ticketsBindingSource.Filter = "ProblemStatus LIKE '%'";
}
if (myTicketsClosedRadioButton.Checked)
{
//GetData(sqlQuery);
ticketsBindingSource.Filter = "ProblemStatus = 'Closed'";
}
if (myTicketsOpenRadioButton.Checked)
{
//GetData(sqlQuery);
ticketsBindingSource.Filter = "ProblemStatus = 'Open'";
}
}
private void allTicketsSubmitButton_Click(object sender, EventArgs e)
{
String sqlQuery = "SELECT u.CallerName, t.* FROM users u INNER JOIN tickets t ON u.id = t.user";
GetData(sqlQuery);
if (myTicketsAllRadioButton.Checked)
{
//GetData(sqlQuery);
ticketsBindingSource.Filter = "ProblemStatus LIKE '%'";
}
if (myTicketsClosedRadioButton.Checked)
{
//GetData(sqlQuery);
ticketsBindingSource.Filter = "ProblemStatus = 'Closed'";
}
if (myTicketsOpenRadioButton.Checked)
{
//GetData(sqlQuery);
ticketsBindingSource.Filter = "ProblemStatus = 'Open'";
}
}
private void GetData(string selectCommand)
{
OleDbDataAdapter dataAdapter = new OleDbDataAdapter();
BindingSource bindingSource1 = new BindingSource();
try
{
// Specify a connection string. Replace the given value with a
// valid connection string for a Northwind SQL Server sample
// database accessible to your system.
String connectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\\Users\\testdb.accdb";
// Create a new data adapter based on the specified query.
dataAdapter = new OleDbDataAdapter(selectCommand, connectionString);
// Create a command builder to generate SQL update, insert, and
// delete commands based on selectCommand. These are used to
// update the database.
OleDbCommandBuilder commandBuilder = new OleDbCommandBuilder(dataAdapter);
// Populate a new data table and bind it to the BindingSource.
DataTable table = new DataTable();
table.Locale = System.Globalization.CultureInfo.InvariantCulture;
dataAdapter.Fill(table);
bindingSource1.DataSource = table;
ticketsBindingSource = bindingSource1;
// Resize the DataGridView columns to fit the newly loaded content.
//ticketsDataGridView.AutoResizeColumns(DataGridViewAutoSizeColumnsMode.AllCellsExceptHeader);
ticketsDataGridView.DataSource = bindingSource1;
}
catch(OleDbException)
{
MessageBox.Show("To run this example, replace the value of the connectionString variable with a connection string that is valid for your system.");
}
}
I have 6 Radio buttons.
3 of them are for myTickets
–Open
–Closed
–All
3 of them are for allTickets
–Open
–Closed
–All
When I click the radio buttons for myTickets group, everything works
I made a small change to the code but the allTicketsSubmitButton doesn’t display all the tickets for Open or All tickets.
The database is relatively small so I can quickly test things as I go.
I have 5 entries
2 Open
2 Closed
1 Work In Progress
2 Entries are assigned to another user (so 3 of them are myTickets).
I noticed something weird
The results display correctly ONLY if I set the radio button for myTickets and allTickets to the same thing
(both open will display the open tickets)
if one is open and the other is closed, then nothing happens
If you have 6 radio buttons, 3 for the myTickets and 3 for allTickets then why do you have your myTickets being checked under your allTickets button click event?
I think you need to change your code from:
TO: