Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. datagridView1_CellContentClick Problem

datagridView1_CellContentClick Problem

Scheduled Pinned Locked Moved C#
databasehelpquestionannouncement
5 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    MumbleB
    wrote on last edited by
    #1

    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;
    
    L M 2 Replies Last reply
    0
    • M MumbleB

      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;
      
      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      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 ...

      M 1 Reply Last reply
      0
      • L Lost User

        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 ...

        M Offline
        M Offline
        MumbleB
        wrote on last edited by
        #3

        Hi. Yes, it does have a primary key. I am using the primary key to get the exact record in the cdatabase to update.

        Excellence is doing ordinary things extraordinarily well.

        1 Reply Last reply
        0
        • M MumbleB

          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;
          
          M Offline
          M Offline
          Mycroft Holmes
          wrote on last edited by
          #4

          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

          M 1 Reply Last reply
          0
          • M Mycroft Holmes

            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

            M Offline
            M Offline
            MumbleB
            wrote on last edited by
            #5

            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.

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups