Search code examples
c#ms-access-2007

Type mismatch expression


Here is the code I tried to execute but error:

string g = dateTimePicker1.Value.ToString("dd-MM-yyyy");
string h = dateTimePicker2.Value.ToString("dd-MM-yyyy");

//string sql2 = @"select Loanno,PName,Duedate,sum(Rec) as rec,sum(Loanamt) as amt from Due where Duedate between [@date1] and [@date2] group by PName order by Loanno asc";

string sql2 = @"select Due.Loanno,Due.PName,Due.Duedate,sum(Due.Rec) as rec,sum(Due.Loanamt) as amt from Due  inner join Party_Det on Due.Loanno=Party_Det.Loanno where Due.Duedate between [@date1] and [@date2] group by Due.Loanno,Due.PName,Due.Duedate";

OleDbCommand cmd2 = new OleDbCommand(sql2, con);
cmd2.Parameters.AddWithValue("@date1", g);
cmd2.Parameters.AddWithValue("@date2", h);
cmd2.ExecuteNonQuery();

DataTable dt=new DataTable();
OleDbDataAdapter da = new OleDbDataAdapter(cmd2);
da.Fill(dt);

dataGridView1.DataSource = dt;
con.Close();

Solution

  • Try this

    string sql2 = @"select Due.Loanno, Due.PName, Due.Duedate, sum(Due.Rec) as rec,sum(Due.Loanamt) as amt from Due  inner join Party_Det on Due.Loanno=Party_Det.Loanno where Due.Duedate between @date1 and @date2 group by Due.Loanno,Due.PName,Due.Duedate";
    OleDbCommand cmd2 = new OleDbCommand(sql2, con);
    
    cmd2.Parameters.AddWithValue("@date1", dateTimePicker1.Value.Date);
    cmd2.Parameters.AddWithValue("@date2", dateTimePicker2.Value.Date);
    
    cmd2.ExecuteNonQuery();
    DataTable dt=new DataTable();
    OleDbDataAdapter da = new OleDbDataAdapter(cmd2);
    
    da.Fill(dt);
    dataGridView1.DataSource = dt;
    con.Close();    
    

    ADDED : Why do you need the join with Party_det table? You are not using any fields from that table.. Make sure that the Loanno column in both tables are of same data type.