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. problem using for() with datagrid - blank cell value

problem using for() with datagrid - blank cell value

Scheduled Pinned Locked Moved C#
databasesharepointcomhelpquestion
9 Posts 8 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.
  • J Offline
    J Offline
    Jassim Rahma
    wrote on last edited by
    #1

    Hi, I am using the following for() to loop through a datagrid to save to a database. My problem here is: when looping, I am getting all records correct except the last record which I am getting a blank cell value for it. what's wrong with my for() please?

    for (int i = 0; i <= gridContacts.RowCount; i++)
    {
    sql_command = new MySqlCommand("sp_add_new_employee_contact", sql_connection);
    sql_command.CommandType = CommandType.StoredProcedure;
    sql_command.CommandTimeout = Convert.ToInt32(string_encryptor.DecryptString(xmlClass.read_xml_value("HRMS\\HRMS", "CommandTimeOut"), "JassimRahma@731004167"));

    sql\_command.Parameters.AddWithValue("param\_employee\_id", employee\_id).MySqlDbType = MySqlDbType.Int32;
    sql\_command.Parameters.AddWithValue("param\_contact\_category", Convert.ToInt32(gridContacts.GetRowCellValue(i, "contact\_category"))).MySqlDbType = MySqlDbType.Int32;
    sql\_command.Parameters.AddWithValue("param\_contact\_details", Convert.ToString(gridContacts.GetRowCellValue(i, "contact\_details"))).MySqlDbType = MySqlDbType.VarChar;
    sql\_command.Parameters.AddWithValue("param\_contact\_description", Convert.ToString(gridContacts.GetRowCellValue(i, "contact\_description"))).MySqlDbType = MySqlDbType.VarChar;
    
    MessageBox.Show(Convert.ToString(gridContacts.GetRowCellValue(i, "contact\_details")));
    
    int result\_rows = sql\_command.ExecuteNonQuery();
    

    }

    Thanks, Jassim

    Technology News @ www.JassimRahma.com

    E D P S S 7 Replies Last reply
    0
    • J Jassim Rahma

      Hi, I am using the following for() to loop through a datagrid to save to a database. My problem here is: when looping, I am getting all records correct except the last record which I am getting a blank cell value for it. what's wrong with my for() please?

      for (int i = 0; i <= gridContacts.RowCount; i++)
      {
      sql_command = new MySqlCommand("sp_add_new_employee_contact", sql_connection);
      sql_command.CommandType = CommandType.StoredProcedure;
      sql_command.CommandTimeout = Convert.ToInt32(string_encryptor.DecryptString(xmlClass.read_xml_value("HRMS\\HRMS", "CommandTimeOut"), "JassimRahma@731004167"));

      sql\_command.Parameters.AddWithValue("param\_employee\_id", employee\_id).MySqlDbType = MySqlDbType.Int32;
      sql\_command.Parameters.AddWithValue("param\_contact\_category", Convert.ToInt32(gridContacts.GetRowCellValue(i, "contact\_category"))).MySqlDbType = MySqlDbType.Int32;
      sql\_command.Parameters.AddWithValue("param\_contact\_details", Convert.ToString(gridContacts.GetRowCellValue(i, "contact\_details"))).MySqlDbType = MySqlDbType.VarChar;
      sql\_command.Parameters.AddWithValue("param\_contact\_description", Convert.ToString(gridContacts.GetRowCellValue(i, "contact\_description"))).MySqlDbType = MySqlDbType.VarChar;
      
      MessageBox.Show(Convert.ToString(gridContacts.GetRowCellValue(i, "contact\_details")));
      
      int result\_rows = sql\_command.ExecuteNonQuery();
      

      }

      Thanks, Jassim

      Technology News @ www.JassimRahma.com

      E Offline
      E Offline
      Estys
      wrote on last edited by
      #2

      Maybe

      i < gridContacts.RowCount

      ? Cheers

      1 Reply Last reply
      0
      • J Jassim Rahma

        Hi, I am using the following for() to loop through a datagrid to save to a database. My problem here is: when looping, I am getting all records correct except the last record which I am getting a blank cell value for it. what's wrong with my for() please?

        for (int i = 0; i <= gridContacts.RowCount; i++)
        {
        sql_command = new MySqlCommand("sp_add_new_employee_contact", sql_connection);
        sql_command.CommandType = CommandType.StoredProcedure;
        sql_command.CommandTimeout = Convert.ToInt32(string_encryptor.DecryptString(xmlClass.read_xml_value("HRMS\\HRMS", "CommandTimeOut"), "JassimRahma@731004167"));

        sql\_command.Parameters.AddWithValue("param\_employee\_id", employee\_id).MySqlDbType = MySqlDbType.Int32;
        sql\_command.Parameters.AddWithValue("param\_contact\_category", Convert.ToInt32(gridContacts.GetRowCellValue(i, "contact\_category"))).MySqlDbType = MySqlDbType.Int32;
        sql\_command.Parameters.AddWithValue("param\_contact\_details", Convert.ToString(gridContacts.GetRowCellValue(i, "contact\_details"))).MySqlDbType = MySqlDbType.VarChar;
        sql\_command.Parameters.AddWithValue("param\_contact\_description", Convert.ToString(gridContacts.GetRowCellValue(i, "contact\_description"))).MySqlDbType = MySqlDbType.VarChar;
        
        MessageBox.Show(Convert.ToString(gridContacts.GetRowCellValue(i, "contact\_details")));
        
        int result\_rows = sql\_command.ExecuteNonQuery();
        

        }

        Thanks, Jassim

        Technology News @ www.JassimRahma.com

        D Offline
        D Offline
        Dave Kreskowiak
        wrote on last edited by
        #3

        Why are you recreating the same MySqlCommand and parameters objects every trip through the loop? Wouldn't it better to just create them ONCE, before the loop, and then just populate the parameters and execute the command inside the loop?

        A guide to posting questions on CodeProject

        How to debug small programs
        Dave Kreskowiak

        P 1 Reply Last reply
        0
        • D Dave Kreskowiak

          Why are you recreating the same MySqlCommand and parameters objects every trip through the loop? Wouldn't it better to just create them ONCE, before the loop, and then just populate the parameters and execute the command inside the loop?

          A guide to posting questions on CodeProject

          How to debug small programs
          Dave Kreskowiak

          P Offline
          P Offline
          PIEBALDconsult
          wrote on last edited by
          #4

          Hear! Hear!

          You'll never get very far if all you do is follow instructions.

          1 Reply Last reply
          0
          • J Jassim Rahma

            Hi, I am using the following for() to loop through a datagrid to save to a database. My problem here is: when looping, I am getting all records correct except the last record which I am getting a blank cell value for it. what's wrong with my for() please?

            for (int i = 0; i <= gridContacts.RowCount; i++)
            {
            sql_command = new MySqlCommand("sp_add_new_employee_contact", sql_connection);
            sql_command.CommandType = CommandType.StoredProcedure;
            sql_command.CommandTimeout = Convert.ToInt32(string_encryptor.DecryptString(xmlClass.read_xml_value("HRMS\\HRMS", "CommandTimeOut"), "JassimRahma@731004167"));

            sql\_command.Parameters.AddWithValue("param\_employee\_id", employee\_id).MySqlDbType = MySqlDbType.Int32;
            sql\_command.Parameters.AddWithValue("param\_contact\_category", Convert.ToInt32(gridContacts.GetRowCellValue(i, "contact\_category"))).MySqlDbType = MySqlDbType.Int32;
            sql\_command.Parameters.AddWithValue("param\_contact\_details", Convert.ToString(gridContacts.GetRowCellValue(i, "contact\_details"))).MySqlDbType = MySqlDbType.VarChar;
            sql\_command.Parameters.AddWithValue("param\_contact\_description", Convert.ToString(gridContacts.GetRowCellValue(i, "contact\_description"))).MySqlDbType = MySqlDbType.VarChar;
            
            MessageBox.Show(Convert.ToString(gridContacts.GetRowCellValue(i, "contact\_details")));
            
            int result\_rows = sql\_command.ExecuteNonQuery();
            

            }

            Thanks, Jassim

            Technology News @ www.JassimRahma.com

            P Offline
            P Offline
            PIEBALDconsult
            wrote on last edited by
            #5

            There is a lot of bad in that code. Others have pointed out some. But also... 0) Don't bother trying to set the MySqlDbType of the Parameters -- the framework infers it from the Value. 1) Don't use Convert -- casting and Parsing is more efficient

            You'll never get very far if all you do is follow instructions.

            1 Reply Last reply
            0
            • J Jassim Rahma

              Hi, I am using the following for() to loop through a datagrid to save to a database. My problem here is: when looping, I am getting all records correct except the last record which I am getting a blank cell value for it. what's wrong with my for() please?

              for (int i = 0; i <= gridContacts.RowCount; i++)
              {
              sql_command = new MySqlCommand("sp_add_new_employee_contact", sql_connection);
              sql_command.CommandType = CommandType.StoredProcedure;
              sql_command.CommandTimeout = Convert.ToInt32(string_encryptor.DecryptString(xmlClass.read_xml_value("HRMS\\HRMS", "CommandTimeOut"), "JassimRahma@731004167"));

              sql\_command.Parameters.AddWithValue("param\_employee\_id", employee\_id).MySqlDbType = MySqlDbType.Int32;
              sql\_command.Parameters.AddWithValue("param\_contact\_category", Convert.ToInt32(gridContacts.GetRowCellValue(i, "contact\_category"))).MySqlDbType = MySqlDbType.Int32;
              sql\_command.Parameters.AddWithValue("param\_contact\_details", Convert.ToString(gridContacts.GetRowCellValue(i, "contact\_details"))).MySqlDbType = MySqlDbType.VarChar;
              sql\_command.Parameters.AddWithValue("param\_contact\_description", Convert.ToString(gridContacts.GetRowCellValue(i, "contact\_description"))).MySqlDbType = MySqlDbType.VarChar;
              
              MessageBox.Show(Convert.ToString(gridContacts.GetRowCellValue(i, "contact\_details")));
              
              int result\_rows = sql\_command.ExecuteNonQuery();
              

              }

              Thanks, Jassim

              Technology News @ www.JassimRahma.com

              S Offline
              S Offline
              Shrutik Panchal
              wrote on last edited by
              #6

              Hello Jassim, It is because Indexing start from 0 not from 1. Look, Your gridContacts.RowCount will return 10, if you have 10 rows and your for loop will be executed 11 times. In grid, there are only 10 records, so last record will be blank line. So, start index from i = 1 or use i < gridContacts.RowCount But by looking at your code, you should use i < gridContacts.RowCount. Otherwise you have to change some of your code too. Thanks... Have Fun ;P :cool:

              1 Reply Last reply
              0
              • J Jassim Rahma

                Hi, I am using the following for() to loop through a datagrid to save to a database. My problem here is: when looping, I am getting all records correct except the last record which I am getting a blank cell value for it. what's wrong with my for() please?

                for (int i = 0; i <= gridContacts.RowCount; i++)
                {
                sql_command = new MySqlCommand("sp_add_new_employee_contact", sql_connection);
                sql_command.CommandType = CommandType.StoredProcedure;
                sql_command.CommandTimeout = Convert.ToInt32(string_encryptor.DecryptString(xmlClass.read_xml_value("HRMS\\HRMS", "CommandTimeOut"), "JassimRahma@731004167"));

                sql\_command.Parameters.AddWithValue("param\_employee\_id", employee\_id).MySqlDbType = MySqlDbType.Int32;
                sql\_command.Parameters.AddWithValue("param\_contact\_category", Convert.ToInt32(gridContacts.GetRowCellValue(i, "contact\_category"))).MySqlDbType = MySqlDbType.Int32;
                sql\_command.Parameters.AddWithValue("param\_contact\_details", Convert.ToString(gridContacts.GetRowCellValue(i, "contact\_details"))).MySqlDbType = MySqlDbType.VarChar;
                sql\_command.Parameters.AddWithValue("param\_contact\_description", Convert.ToString(gridContacts.GetRowCellValue(i, "contact\_description"))).MySqlDbType = MySqlDbType.VarChar;
                
                MessageBox.Show(Convert.ToString(gridContacts.GetRowCellValue(i, "contact\_details")));
                
                int result\_rows = sql\_command.ExecuteNonQuery();
                

                }

                Thanks, Jassim

                Technology News @ www.JassimRahma.com

                S Offline
                S Offline
                Swinkaran
                wrote on last edited by
                #7

                There are two ways of using this. To make the correction on your code, it should be,

                for (int i = 0; i < gridContacts.RowCount; i++)
                {
                }
                // You dont need the <= as the index starts from 0

                Alternative way is to try with For each, which i prefer the best suit for Datagrid,

                foreach (DataGridRow drv in gridContacts.Items) // or try(gridContacts.Rows) either /or one
                {
                }

                1 Reply Last reply
                0
                • J Jassim Rahma

                  Hi, I am using the following for() to loop through a datagrid to save to a database. My problem here is: when looping, I am getting all records correct except the last record which I am getting a blank cell value for it. what's wrong with my for() please?

                  for (int i = 0; i <= gridContacts.RowCount; i++)
                  {
                  sql_command = new MySqlCommand("sp_add_new_employee_contact", sql_connection);
                  sql_command.CommandType = CommandType.StoredProcedure;
                  sql_command.CommandTimeout = Convert.ToInt32(string_encryptor.DecryptString(xmlClass.read_xml_value("HRMS\\HRMS", "CommandTimeOut"), "JassimRahma@731004167"));

                  sql\_command.Parameters.AddWithValue("param\_employee\_id", employee\_id).MySqlDbType = MySqlDbType.Int32;
                  sql\_command.Parameters.AddWithValue("param\_contact\_category", Convert.ToInt32(gridContacts.GetRowCellValue(i, "contact\_category"))).MySqlDbType = MySqlDbType.Int32;
                  sql\_command.Parameters.AddWithValue("param\_contact\_details", Convert.ToString(gridContacts.GetRowCellValue(i, "contact\_details"))).MySqlDbType = MySqlDbType.VarChar;
                  sql\_command.Parameters.AddWithValue("param\_contact\_description", Convert.ToString(gridContacts.GetRowCellValue(i, "contact\_description"))).MySqlDbType = MySqlDbType.VarChar;
                  
                  MessageBox.Show(Convert.ToString(gridContacts.GetRowCellValue(i, "contact\_details")));
                  
                  int result\_rows = sql\_command.ExecuteNonQuery();
                  

                  }

                  Thanks, Jassim

                  Technology News @ www.JassimRahma.com

                  A Offline
                  A Offline
                  AshishvermaMCA
                  wrote on last edited by
                  #8

                  Correction in For Loop- for (int i = 0; i < gridContacts.RowCount; i++)

                  1 Reply Last reply
                  0
                  • J Jassim Rahma

                    Hi, I am using the following for() to loop through a datagrid to save to a database. My problem here is: when looping, I am getting all records correct except the last record which I am getting a blank cell value for it. what's wrong with my for() please?

                    for (int i = 0; i <= gridContacts.RowCount; i++)
                    {
                    sql_command = new MySqlCommand("sp_add_new_employee_contact", sql_connection);
                    sql_command.CommandType = CommandType.StoredProcedure;
                    sql_command.CommandTimeout = Convert.ToInt32(string_encryptor.DecryptString(xmlClass.read_xml_value("HRMS\\HRMS", "CommandTimeOut"), "JassimRahma@731004167"));

                    sql\_command.Parameters.AddWithValue("param\_employee\_id", employee\_id).MySqlDbType = MySqlDbType.Int32;
                    sql\_command.Parameters.AddWithValue("param\_contact\_category", Convert.ToInt32(gridContacts.GetRowCellValue(i, "contact\_category"))).MySqlDbType = MySqlDbType.Int32;
                    sql\_command.Parameters.AddWithValue("param\_contact\_details", Convert.ToString(gridContacts.GetRowCellValue(i, "contact\_details"))).MySqlDbType = MySqlDbType.VarChar;
                    sql\_command.Parameters.AddWithValue("param\_contact\_description", Convert.ToString(gridContacts.GetRowCellValue(i, "contact\_description"))).MySqlDbType = MySqlDbType.VarChar;
                    
                    MessageBox.Show(Convert.ToString(gridContacts.GetRowCellValue(i, "contact\_details")));
                    
                    int result\_rows = sql\_command.ExecuteNonQuery();
                    

                    }

                    Thanks, Jassim

                    Technology News @ www.JassimRahma.com

                    V Offline
                    V Offline
                    V 0
                    wrote on last edited by
                    #9

                    beside the error you get you're making the mistake of mixing DAL code with GUI code, which is generally not done, and most certainly not in production code. I can recommend you two articles :-) 1. Programming Vs. Software Development[^] 2. Building a Framework - Part I (DAL)[^] Hope this helps.

                    V.
                    (MQOTD rules and previous solutions)

                    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