hey all again
in trying to add to make sure if my datagridview has some data in it or not before adding it to the database
and here what i came up till now

 int i = POSDGV.Rows.Count;
                 for (int j = 0; j < i; j++)
            {
                string strCol2 = null;
                string strCol3 = null;
                if (POSDGV.Rows[j].cells[1].Value == null)
                   MessageBox.show("Error");
                   return;
                else
                    strCol2 = POSDGV.Rows[j].Cells[1].Value.ToString();
                if (POSDGV.Rows[j].Cells[2].Value == null)
                    strCol3 = "----";
                else
                    strCol3 = POSDGV.Rows[j].Cells[2].Value.ToString();
            }

how ever it dosen't seem to work as it dosent see the null value

and also i would like a an efficent way to add datafrom datagrid to a database
i have this code but i'm not really sure that is a good practic and here is :

{
             // the next should save data from datagrid view in database
                  OleDbCommand DGVCmd;
                  string DGVSTR;
                   for (int i = 0; i < PhoneDgv.Rows.Count-1; i++)
                  {
                      DGVSTR = @"INSERT INTO PHONES(CODE,PHONENO,PHONETYPE) VALUES ('" + PhoneDgv.Rows[i].Cells["CodeCol"].Value + "','" + PhoneDgv.Rows[i].Cells["Cell_Col"].Value + "', '"+ PhoneDgv.Rows[i].Cells["PhCode"].Value + "')";
                try
                {
                    using (OleDbConnection con = new OleDbConnection(StrConn))
                    {
                        using (DGVCmd = new OleDbCommand(DGVSTR, con))
                        {
                            con.Open();
                            DGVCmd.ExecuteNonQuery();
                        }
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
              }
           }

please help

if (POSDGV.Rows[j].Cells[2].Value == null)

It really depends on how you're storing null values. Likely the check will fail because the cell value is actually DBNull.Value or simply an empty string:

if (POSDGV.Rows[j].Cells[2].Value == DBNull.Value || string.IsNullOrEmpty(POSDGV.Rows[j].Cells[2].Value.ToString()))

Edited 2 Years Ago by deceptikon

This article has been dead for over six months. Start a new discussion instead.