'How do I save null values that I didn't specify in the database?

welcome In my program I import data from excel file and the method worked for me, but I have a simple problem, I choose the data I want to recover from the dropdown list and then I import it, but maybe the user doesn't want to select all the data, so I want if he doesn't choose some data, it is saved as blank . Example: enter image description here

I want if I leave these values blank and don't select a column for them from the excel sheet, they will be saved as blank. It's already saved blank but this message pops up for me. enter image description here

I know the cure for this message, but when I solve it, I can't save empty values, so this message appears enter image description here

This is my code:

for (int i = 0; i < DgvSearch.Rows.Count; i++)
                {
                    if (Con.State == ConnectionState.Closed)
                    {
                        Con.Open();
                    }
                    tbl3 = db3.readData("select max (Sup_ID) from Suppliers", "");
                    Cmd3 = new SqlCommand("insert into Suppliers (Sup_ID,Sup_Name,Compny_Name,Sup_Adderss,Sup_Phone,Notes) Values (@Sup_ID,@Sup_Name,@Compny_Name,@Sup_Adderss,@Sup_Phone,@Notes)", Con);

                    if ((Convert.ToInt32(tbl3.Rows[0][0]) + 1).ToString() == null)
                    {
                        Cmd3.Parameters.Add(new SqlParameter("@Sup_ID", SqlDbType.Int)).Value = DBNull.Value;
                    }
                    else
                    {
                        Cmd3.Parameters.Add(new SqlParameter("@Sup_ID", SqlDbType.Int)).Value = (Convert.ToInt32(tbl3.Rows[0][0]) + 1).ToString();

                    }
                    if (cbx2.SelectedIndex <= -1)
                    {
                        Cmd3.Parameters.Add(new SqlParameter("@Sup_Name", SqlDbType.VarChar)).Value = DBNull.Value ;
                    }
                    else
                    {
                         Cmd3.Parameters.Add(new SqlParameter("@Sup_Name", SqlDbType.VarChar)).Value = DgvSearch.Rows[i].Cells[cbx2.SelectedIndex].Value;
                    }
                    if (cbx3.SelectedIndex <= -1)
                    {
                        Cmd3.Parameters.Add(new SqlParameter("@Compny_Name", SqlDbType.VarChar)).Value = DBNull.Value;
                    }
                    else
                    {
                        Cmd3.Parameters.Add(new SqlParameter("@Compny_Name", SqlDbType.VarChar)).Value = DgvSearch.Rows[i].Cells[cbx3.SelectedIndex].Value;

                    }
                    if (cbx4.SelectedIndex <= -1)
                    {
                        Cmd3.Parameters.Add(new SqlParameter("@Sup_Adderss", SqlDbType.VarChar)).Value = DBNull.Value;
                    }
                    else
                    {
                        Cmd3.Parameters.Add(new SqlParameter("@Sup_Adderss", SqlDbType.VarChar)).Value = DgvSearch.Rows[i].Cells[cbx4.SelectedIndex].Value;

                    }
                    if (cbx5.SelectedIndex <= -1)
                    {
                        Cmd3.Parameters.Add(new SqlParameter("@Sup_Phone", SqlDbType.VarChar)).Value = DBNull.Value;
                    }
                    else
                    {
                        Cmd3.Parameters.Add(new SqlParameter("@Sup_Phone", SqlDbType.VarChar)).Value = DgvSearch.Rows[i].Cells[cbx5.SelectedIndex].Value;

                    }
                    if (cbx6.SelectedIndex <= -1)
                    {
                        Cmd3.Parameters.Add(new SqlParameter("@Notes", SqlDbType.VarChar)).Value = DBNull.Value;
                    }
                    else
                    {
                        Cmd3.Parameters.Add(new SqlParameter("@Notes", SqlDbType.VarChar)).Value = DgvSearch.Rows[i].Cells[cbx6.SelectedIndex].Value;
                    }
                    
                    Cmd3.ExecuteNonQuery();
                    Con.Close();
                }
                XtraMessageBox.Show("تم إستيراد بيانات الموردين بنجاح ", "حفظ", MessageBoxButtons.OK, MessageBoxIcon.Information);
                SelectAll_Table3();
            }

The previous code is saved with null values, but it shows the first message When I modify it like this: Does not save empty values

 if (DgvSearch.Rows[i].Cells[cbx5.SelectedIndex].Value == null)
                    {
                        Cmd3.Parameters.Add(new SqlParameter { ParameterName = "@Notes", Value = DBNull.Value });
                    }
                    else
                    {
                        Cmd3.Parameters.Add(new SqlParameter { ParameterName = "@Notes", Value = DgvSearch.Rows[i].Cells[cbx5.SelectedIndex].Value });
                    }

Is there any solution? Thank you very much



Solution 1:[1]

Like Richard describes

if "tbl3.Rows[0][0]" is NULL

Convert.ToInt32(null) // 0
Convert.ToInt32(null) + 1 // 1
(Convert.ToInt32(null) + 1).ToString() // "1"

if "tbl3.Rows[0][0]" is "10"

Convert.ToInt32("10") // 10
Convert.ToInt32("10") + 1 // 11
(Convert.ToInt32("10") + 1).ToString() // "11"

if "tbl3.Rows[0][0]" is "abc"

Convert.ToInt32("abc") // system format exception

either do: if (tbl3.Rows[0][0] == null) {assign null} else {assign parsed value} and do exception handling if input is not numeric

or try to parse it and handle non-numeric input as null

if (Int32.TryParse(tbl3.Rows[0][0], out var parsedValue)) {assign parsed value} else {assign null}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Jane