i have a class customer with three methods. first to get datagrid as a result of search then second is also to get datagird to get the records between to dates. the third method is used to update database with dataset.
the first two methods r working well when i try to update third method gives the runtime error "datatype mismatch in criteria expression."
i think the datatype match with my MS Access database field.
public class customer
{
// private static string strConn;
private static DataTable ds;
OleDbConnection con = new OleDbConnection(@"provider= Microsoft.Jet.OLEDb.4.0; Data Source = D:\My Documents\Visual Studio 2005\Projects\db2.mdb");
public DataTable getdataset(string fname, DataGridView dg)
{
con.Open();
OleDbDataAdapter thisadapter1 = new OleDbDataAdapter("Select * from tbl_client where fname= '" + fname + "'", con);
OleDbCommandBuilder cd = new OleDbCommandBuilder(thisadapter1);
ds = new DataTable();
thisadapter1.Fill(ds);
dg.BringToFront();
dg.Visible = true;
BindingSource bsource = new BindingSource();
bsource.DataSource = ds;
dg.DataSource = bsource;
dg.Columns[0].Visible = false;
return ds;
}
public DataTable date_gridview(DateTime d1,DateTime d2, DataGridView dg)
{
//MessageBox.Show(d1.ToString());
DateTime dta = Convert.ToDateTime(d1);
DateTime dtb = Convert.ToDateTime(d2);
// MessageBox.Show(dta.ToString);
DateTime dt1 = dta.Date;
DateTime dt2 = dtb.Date;
OleDbConnection con = new OleDbConnection(@"provider= Microsoft.Jet.OLEDb.4.0; Data Source = D:\My Documents\Visual Studio 2005\Projects\db2.mdb");
con.Open();
OleDbDataAdapter thisadapter1 = new OleDbDataAdapter("Select * from tbl_project where reg_date between #"+dta+"# and #"+dtb+"#", con);
//OleDbCommandBuilder cd = new OleDbCommandBuilder(thisadapter1);
ds = new DataTable();
thisadapter1.Fill(ds);
dg.BringToFront();
dg.Visible = true;
BindingSource bsource = new BindingSource();
bsource.DataSource = ds;
dg.DataSource = bsource;
return ds;
}
public int update()
{
//OleDbConnection con = new OleDbConnection(@"provider= Microsoft.Jet.OLEDb.4.0; Data Source = D:\My Documents\Visual Studio 2005\Projects\db2.mdb");
con.Open();
string sqlUpdate;
//sqlUpdate = " Update tbl_client Set mobile= 0 Where client_id= '@p1'";
sqlUpdate = "Update tbl_client Set lname='@p3', fname= '@p2' Where client_id= '@p1'";
pUpdate[0] = new OleDbParameter("@p1", OleDbType.Integer, 20, "client_id");
pUpdate[1] = new OleDbParameter("@p2", OleDbType.VarChar, 50, "Fname");
// pUpdate[1] = new OleDbParameter("@p2", OleDbType.DBDate, 30, "reg_date");
// pUpdate[2] = new OleDbParameter("@p3", OleDbType.VarChar, 40,"fname");
pUpdate[2] = new OleDbParameter("@p3", OleDbType.VarChar, 50,"Lname");
OleDbCommand cmdUpdate = new OleDbCommand(sqlUpdate,con);
// cmdUpdate.Parameters.AddRange(pUpdate);
OleDbDataAdapter da = new OleDbDataAdapter();
OleDbCommandBuilder cb = new OleDbCommandBuilder(da);
// DataSet ds = new DataSet();
da.UpdateCommand = cmdUpdate;
int i= da.Update(ds);
ds.AcceptChanges();
if (i > 0)
{
MessageBox.Show("database updated");
}
else {
MessageBox.Show("update error.");
}
return i;
}
Thanks in advance
Radhika
No one has replied yet! Why not be the first?
Sign in or Join us (it's free).