problem using for() with datagrid - blank cell value
-
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
-
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
-
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
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 -
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 KreskowiakHear! Hear!
You'll never get very far if all you do is follow instructions.
-
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
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 efficientYou'll never get very far if all you do is follow instructions.
-
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
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:
-
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
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 0Alternative 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
{
} -
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
Correction in For Loop- for (int i = 0; i < gridContacts.RowCount; i++)
-
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
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)