datagridView1_CellContentClick Problem
-
Hi guys. I am having a problem with the above. I am trying to update the changes made to the gridview back to the database but for some reason I get a "Data Type mismatch in criteria expression" OleDbException. I have tried a number of updates but nothing seems to work. Below is the code I am using. I have delete section which works fine. Just the Edit portion that doesn't work. Can somebody please advise what I am missing here?
private void dataGridView1\_CellContentClick(object sender, DataGridViewCellEventArgs e) { int currentRow = int.Parse(e.RowIndex.ToString()); int rowint = 0; string sql = "SELECT TimesheetDetailID, EmployeeID, TSProjectCodeID, TimeSheetDate, HoursWorked, TaskCode, Notes from tblTimesheetDetails WHERE TimesheetDetailID ='" + rowint + "'"; string empID = dataGridView1\[1, currentRow\].Value.ToString(); string projcode = dataGridView1\[2, currentRow\].Value.ToString(); string timedate = Convert.ToString(dataGridView1\[3, currentRow\].Value.ToString()); double hrs = Convert.ToDouble(string.Format("{0:f}", (dataGridView1\[4, currentRow\].Value.ToString()))); string taskcode = dataGridView1\[5, currentRow\].Value.ToString(); string notes = dataGridView1\[6, currentRow\].Value.ToString(); OleDbDataAdapter da = new OleDbDataAdapter(sql, conn); OleDbCommandBuilder cmb = new OleDbCommandBuilder(da); da.Fill(dataTable); string updateString = "Update tblTimesheetDetails SET EmployeeID ='" + empID + "', TimesheetID ='" + "" + "', TSProjectID ='" + projcode + "', TimeSheetDate ='" + timedate + "', HoursWorked ='" + hrs + "',TaskCode ='" + taskcode + "', Notes ='" + notes + "' WHERE TimesheetDetailID ='" + rowint + "'"; try { conn.Open(); string timesheetString = dataGridView1\[0, currentRow\].Value.ToString(); rowint = int.Parse(timesheetString); } catch (Exception ex) { MessageBox.Show(ex.Message.ToString()); } if (dataGridView1.Columns\[e.ColumnIndex\] == deleteButton && currentRow >= 0) { string queryDeleteString = "DELETE FROM tblTimesheetDetails where TimesheetDetailID = " + rowint + ""; OleDbCommand sqlDelete = new OleDbCommand(); sqlDelete.CommandText = queryDeleteString;
-
Hi guys. I am having a problem with the above. I am trying to update the changes made to the gridview back to the database but for some reason I get a "Data Type mismatch in criteria expression" OleDbException. I have tried a number of updates but nothing seems to work. Below is the code I am using. I have delete section which works fine. Just the Edit portion that doesn't work. Can somebody please advise what I am missing here?
private void dataGridView1\_CellContentClick(object sender, DataGridViewCellEventArgs e) { int currentRow = int.Parse(e.RowIndex.ToString()); int rowint = 0; string sql = "SELECT TimesheetDetailID, EmployeeID, TSProjectCodeID, TimeSheetDate, HoursWorked, TaskCode, Notes from tblTimesheetDetails WHERE TimesheetDetailID ='" + rowint + "'"; string empID = dataGridView1\[1, currentRow\].Value.ToString(); string projcode = dataGridView1\[2, currentRow\].Value.ToString(); string timedate = Convert.ToString(dataGridView1\[3, currentRow\].Value.ToString()); double hrs = Convert.ToDouble(string.Format("{0:f}", (dataGridView1\[4, currentRow\].Value.ToString()))); string taskcode = dataGridView1\[5, currentRow\].Value.ToString(); string notes = dataGridView1\[6, currentRow\].Value.ToString(); OleDbDataAdapter da = new OleDbDataAdapter(sql, conn); OleDbCommandBuilder cmb = new OleDbCommandBuilder(da); da.Fill(dataTable); string updateString = "Update tblTimesheetDetails SET EmployeeID ='" + empID + "', TimesheetID ='" + "" + "', TSProjectID ='" + projcode + "', TimeSheetDate ='" + timedate + "', HoursWorked ='" + hrs + "',TaskCode ='" + taskcode + "', Notes ='" + notes + "' WHERE TimesheetDetailID ='" + rowint + "'"; try { conn.Open(); string timesheetString = dataGridView1\[0, currentRow\].Value.ToString(); rowint = int.Parse(timesheetString); } catch (Exception ex) { MessageBox.Show(ex.Message.ToString()); } if (dataGridView1.Columns\[e.ColumnIndex\] == deleteButton && currentRow >= 0) { string queryDeleteString = "DELETE FROM tblTimesheetDetails where TimesheetDetailID = " + rowint + ""; OleDbCommand sqlDelete = new OleDbCommand(); sqlDelete.CommandText = queryDeleteString;
-
Do your table in the database have PK ? I know sounds a bit Odd , but I was facing some problems in OleDB AcceptChanges() things , And solved by Adding PK to Mt Table in the database thanks
I know nothing , I know nothing ...
-
Hi guys. I am having a problem with the above. I am trying to update the changes made to the gridview back to the database but for some reason I get a "Data Type mismatch in criteria expression" OleDbException. I have tried a number of updates but nothing seems to work. Below is the code I am using. I have delete section which works fine. Just the Edit portion that doesn't work. Can somebody please advise what I am missing here?
private void dataGridView1\_CellContentClick(object sender, DataGridViewCellEventArgs e) { int currentRow = int.Parse(e.RowIndex.ToString()); int rowint = 0; string sql = "SELECT TimesheetDetailID, EmployeeID, TSProjectCodeID, TimeSheetDate, HoursWorked, TaskCode, Notes from tblTimesheetDetails WHERE TimesheetDetailID ='" + rowint + "'"; string empID = dataGridView1\[1, currentRow\].Value.ToString(); string projcode = dataGridView1\[2, currentRow\].Value.ToString(); string timedate = Convert.ToString(dataGridView1\[3, currentRow\].Value.ToString()); double hrs = Convert.ToDouble(string.Format("{0:f}", (dataGridView1\[4, currentRow\].Value.ToString()))); string taskcode = dataGridView1\[5, currentRow\].Value.ToString(); string notes = dataGridView1\[6, currentRow\].Value.ToString(); OleDbDataAdapter da = new OleDbDataAdapter(sql, conn); OleDbCommandBuilder cmb = new OleDbCommandBuilder(da); da.Fill(dataTable); string updateString = "Update tblTimesheetDetails SET EmployeeID ='" + empID + "', TimesheetID ='" + "" + "', TSProjectID ='" + projcode + "', TimeSheetDate ='" + timedate + "', HoursWorked ='" + hrs + "',TaskCode ='" + taskcode + "', Notes ='" + notes + "' WHERE TimesheetDetailID ='" + rowint + "'"; try { conn.Open(); string timesheetString = dataGridView1\[0, currentRow\].Value.ToString(); rowint = int.Parse(timesheetString); } catch (Exception ex) { MessageBox.Show(ex.Message.ToString()); } if (dataGridView1.Columns\[e.ColumnIndex\] == deleteButton && currentRow >= 0) { string queryDeleteString = "DELETE FROM tblTimesheetDetails where TimesheetDetailID = " + rowint + ""; OleDbCommand sqlDelete = new OleDbCommand(); sqlDelete.CommandText = queryDeleteString;
You are passing all your criteria as strings, I suspect the date format is incompatible with the database. Set a break point on
conn.open()
and inspect the string, try and execute the string in SSMS.Never underestimate the power of human stupidity RAH
-
You are passing all your criteria as strings, I suspect the date format is incompatible with the database. Set a break point on
conn.open()
and inspect the string, try and execute the string in SSMS.Never underestimate the power of human stupidity RAH
Hi Mycroft. Sorry for taking this long to reply. I have actualy set the DateColumn in the DB to a string column and I am passing a string. I have now tried numerous things and it just won't work. I have now decided to abandon the save changes from the datagrid and just to delete the row and the users can retype. I know it is not the best thing to do but if I can't et it working then I am stuck with doing that.
Excellence is doing ordinary things extraordinarily well.