Click here to Skip to main content
15,901,666 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I Have a form in that one bankname combobox,acountno textbox,accounttype combobox ,onedatagridview, and one Addbutton and Save button.
After Loading the form If i select bankname from the combobox accountno and accounttype
boxes are filling and showing the related bank details in datagridview fine.Add button is used for if select bankname and then i click add button new empty row is added top of the
datagridview and then i enter the data into row and the click save button.It saving database fine.Now what i want is if i add new new empty row and click save button it will message cell value is empty i tried different ways but it is not working .Please anybody
help me on this.I am getting compile time error operator != cannot be applied operands of the int and system.dbnull error

What I have tried:

C#
<blockquote class="quote"><div class="op">Quote:</div><pre>
<blockquote class="quote"><div class="op">Quote:</div>  


       private void btnsave_Click(object sender, EventArgs e)
        {
            ChqMngrBsns.CheckBook chk = new ChqMngrBsns.CheckBook();

            // Inserting Each Record into database

            foreach (DataGridViewRow row in dgvwChqs.Rows)
            {
                if (bankid > 0)
                {
                    chk.bankid=bankid;


                    if (Convert.ToInt32(row.Cells["Slno"].Value) != DBNull.Value)
                    {

                        chk.Slno = Convert.ToInt32(row.Cells["Slno"].Value);

                    }

                    if (Convert.ToInt32(row.Cells["startno"].Value) != DBNull.Value)
                    {
                        chk.startingno = Convert.ToInt32(row.Cells["startno"].Value);
                    }

                    if (Convert.ToInt32(row.Cells["noofcheques"].Value) != DBNull.Value)
                    {
                        chk.Noofcheques = Convert.ToInt32(row.Cells["noofcheques"].Value);
                    }

                    if (Convert.ToInt32(row.Cells["endingno"].Value) != DBNull.Value)
                    {

                        chk.Endingno = Convert.ToInt32(row.Cells["endingno"].Value);
                    }

                    if (Convert.ToBoolean(row.Cells["Status"].Value == DBNull.Value))
                    {
                        chk.status = false;

                    }

                    else if (Convert.ToBoolean(row.Cells["Status"].Value != DBNull.Value))
                    {
                        chk.status = true;

                    }
                    chk.SaveData(true);

                }
            }
            //Clearing the datatable
            cmbbankname.SelectedIndex = 0;
            txtAcntno.Text = "";
            cmbAcntype.SelectedIndex = 0;
            dts.Clear();
            //LoadDataGridView();
        }</blockquote>
Posted
Updated 18-May-17 1:26am
Comments
CHill60 18-May-17 7:08am    
Don't convert to an int before comparing to DBNull.Value. It's the Cell you are checking for null not the value within the cell.
Also avoid using Convert.To... use the Parse or TryParse methods on each type.
Member 13153537 18-May-17 7:14am    
How to do that please tell me.I'm fresher.Thanks In Advance.
Member 13153537 18-May-17 7:15am    
or Give me any reference.

1 solution

Something like this should get you over the problem (warning - untested)
C#
int atry;
if (row.Cells["Slno"].Value != null)
{
    if (int.TryParse(row.Cells["Slno"].Value.ToString(), out atry)) 
    {
        chk.Slno = atry;
    }
}

if (row.Cells["startno"].Value != null)
{
    if (int.TryParse(row.Cells["startno"].Value.ToString(), out atry)) 
    {
        chk.startingno = atry;
    }
}

if (row.Cells["noofcheques"].Value != null)
{
    if (int.TryParse(row.Cells["noofcheques"].Value.ToString(), out atry)) 
    {
        chk.Noofcheques = atry;
    }
}

if (row.Cells["endingno"].Value != null)
{
    if (int.TryParse(row.Cells["endingno"].Value.ToString(), out atry)) 
    {
        chk.Endingno = atry;
    }
}


if (row.Cells["Status"].Value != null)
{
    bool abtry;
    if (bool.TryParse(row.Cells["Status"].Value.ToString(), out abtry))
    {
        chk.status = abtry;
    }
    else
    {
        chk.status = false;
    }
}
 
Share this answer
 
Comments
Member 13153537 18-May-17 7:29am    
Ok thanks for your response.If it works. I will accept the answer.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900