[code]
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.OleDb;
namespace zadatak2
{
public partial class Izvestaj : Form
{
private OleDbConnection connection = new OleDbConnection();
private BindingSource bs;
public Izvestaj()
{
InitializeComponent();
}
OleDbDataAdapter sda;
DataTable dt;
private void Izvestaj_Load(object sender, EventArgs e)
{
connection.ConnectionString = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\base.mdb;User Id=admin;Password=;";
dt = new DataTable();
sda = new OleDbDataAdapter("SELECT users.user,events.event,Tomislav.Date FROM events INNER JOIN (users INNER JOIN Tomislav ON users.ID = Tomislav.user_ID) ON events.ID = Tomislav.event_ID;", connection);
sda.Fill(dt);
dataGridView1.DataSource = dt;
dataGridView1.Columns[0].Width = 120;
dataGridView1.Columns[1].Width = 150;
dataGridView1.Columns[2].Width = 150;
bs = new BindingSource(dt,null);
}
private void textBox3_TextChanged(object sender, EventArgs e)
{
if (textBox3.Text == string.Empty)
{
bs.RemoveFilter();
}
else
{
bs.Filter = string.Format("event LIKE '*{0}*'", textBox3.Text);
}
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
if (textBox1.Text == string.Empty)
{
bs.RemoveFilter();
}
else
{
bs.Filter = string.Format("user LIKE '*{0}*'", textBox1.Text);
}
}
private void button1_Click(object sender, EventArgs e)
{
if(dateTimePicker1.Value>dateTimePicker2.Value)
{
MessageBox.Show("That’s not possible. Please select another range.", "Range date", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
else{
string query = "select users.user, events.event, Date from users, events, Tomislav where Date BETWEEN ? AND ?";
OleDbCommand cmd = new OleDbCommand(query, connection);
cmd.Parameters.AddWithValue("SDate", DbType.DateTime).Value = dateTimePicker1.Value;
cmd.Parameters.AddWithValue("EDate", DbType.DateTime).Value = dateTimePicker2.Value;
DataSet ds = new DataSet();
sda= new OleDbDataAdapter();
connection.Open();
sda.SelectCommand = cmd;
sda.Fill(ds, "Tomislav");
dataGridView1.DataSource = ds.Tables[0];
connection.Close();
}
}
private void unosUBazuToolStripMenuItem_Click(object sender, EventArgs e)
{
this.Hide();
Form1 baza = new Form1();
baza.ShowDialog();
}
private void exitToolStripMenuItem1_Click(object sender, EventArgs e)
{
if (MessageBox.Show("Are you sure?", "Exit", MessageBoxButtons.OKCancel, MessageBoxIcon.Question) == DialogResult.OK)
{
Application.Exit();
}
}
}
}
[/code]
Pradeep ShetPosted Oct 12, 2014, 4:19 PM
Dont it this way, instead form a string of both the condition using ViewState or hiddenfield.
I would say this is a bad practice of writing the code this way, why are you hitting the database 2 times when you want to filter both the condition instead if possible or permits, on a click of some filter button pass both the condition to query itself and fetch only required records from database instead of fetching whole data and filtering it here.
Hope you get it what I want to say.