private void monthCalendar1_DateChanged(object sender, DateRangeEventArgs e)
{
try
{
OleDbConnection con = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source= C:\\Users\\JAY\\Desktop\\Employee.mdb");
OleDbCommand cmd = new OleDbCommand("select * from Emp_Details WHERE DOB="+ monthCalendar1.SelectionRange.Start.ToShortDateString() +"", con);
cmd.CommandType = CommandType.Text;
OleDbDataAdapter da = new OleDbDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds, "Emp_Details");
txtEmployeeNo.Text = ds.Tables[0].Rows[0][0].ToString();
txtName.Text = ds.Tables[0].Rows[0][1].ToString();
txtAddress.Text = ds.Tables[0].Rows[0][2].ToString();
comboBox1.Text = ds.Tables[0].Rows[0][3].ToString();
txtMobNo.Text = ds.Tables[0].Rows[0][4].ToString();
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
Basically i want to retrieve the data through monthcaledar control...but when i click to date of monthcalendar control i got exception there is no row at position 0
Don't use inline parameters, you can use Parameterized query as below
using (var con = new OleDbConnection(@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source= C:\Users\JAY\Desktop\Employee.mdb"))
using (var cmd = new OleDbCommand("select * from Emp_Details WHERE DOB= ?", con))
{
cmd.Parameters.AddWithValue("@P1", monthCalendar1.SelectionRange.Start);
using (var da = new OleDbDataAdapter(cmd))
{
da.Fill(ds, "Emp_Details");
if (ds.Tables["Emp_Details"] !=null && ds.Tables["Emp_Details"].Rows.Count>0)
{
DataRow dr = ds.Tables["Emp_Details"].Rows[0];
txtEmployeeNo.Text = dr[0].ToString();
txtName.Text = dr[1].ToString();
txtAddress.Text = dr[2].ToString();
comboBox1.Text = dr[3].ToString();
txtMobNo.Text = dr[4].ToString();
}
}
}