Click here to Skip to main content
15,888,288 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
iam trying to update cell in datagridview but its giving me Parameter @Description has no default value exception and if i click enter into cell its giving me Index or primary key cannot contain a Null value in insert query AccountNumber is my primary key but right now all table is blank, insert query i written on dataGridView1_RowLeave event while update query written on dataGridView1_CellValueChanged event(

What I have tried:

Update code:


C#
private void dataGridView1_CellValueChanged(object sender, DataGridViewCellEventArgs e)
        {
           if (dataGridView1.IsCurrentRowDirty)
            {
                int accountnumber = 0;
                string Account = "";
                DateTime? Date = null;
                string Description = "";
                string Post_ref = "";
                int Debit = 0;
                int Credit = 0;
                int Balance = 0;


                if (!object.ReferenceEquals(dataGridView1.CurrentRow.Cells[0].Value, DBNull.Value))
                {

                    accountnumber = Convert.ToInt32(dataGridView1.Rows[e.RowIndex].Cells[0].Value.ToString());
                }
                else
                    if (!object.ReferenceEquals(dataGridView1.CurrentRow.Cells[1].Value, DBNull.Value))
                    {
                        Account = dataGridView1.Rows[e.RowIndex].Cells[1].Value.ToString();
                    }
                    else if (!object.ReferenceEquals(dataGridView1.CurrentRow.Cells[2].Value, DBNull.Value))
                    {
                        Date = Convert.ToDateTime(dataGridView1.Rows[e.RowIndex].Cells[2].Value);
                    }
                    else if (!object.ReferenceEquals(dataGridView1.CurrentRow.Cells[3].Value, null))
                    {

                        Description = dataGridView1.Rows[e.RowIndex].Cells[3].Value.ToString();
                    }
                    else if (!object.ReferenceEquals(dataGridView1.CurrentRow.Cells[4].Value, null))
                    {
                        Post_ref = dataGridView1.Rows[e.RowIndex].Cells[4].Value.ToString();
                    }
                    else if (!object.ReferenceEquals(dataGridView1.CurrentRow.Cells[5].Value, DBNull.Value))
                    {
                        Debit = Convert.ToInt32(dataGridView1.Rows[e.RowIndex].Cells[5].Value.ToString());
                    }
                    else if (!object.ReferenceEquals(dataGridView1.CurrentRow.Cells[6].Value, DBNull.Value))
                    {
                        Credit = Convert.ToInt32(dataGridView1.Rows[e.RowIndex].Cells[6].Value.ToString());
                    }
                    else if (!object.ReferenceEquals(dataGridView1.CurrentRow.Cells[7].Value, DBNull.Value))
                    {
                        Balance = Convert.ToInt32(dataGridView1.Rows[e.RowIndex].Cells[7].Value.ToString());
                    }

                string cmd1 = ("update Ledger set [Account]=@Account,[Date]=@Date,[Description]=@Description,[Post_Ref]=@Post_ref,[Debit]=@Debit,[Credit]=@Credit,[Balance]=@Balance where [AccountNumber]=@accountnumber");
                OleDbCommand cmd = new OleDbCommand(cmd1, con);
                con.Open();
                cmd.CommandType = CommandType.Text;


                cmd.Parameters.AddWithValue("@AccountNumber", accountnumber);
                cmd.Parameters.AddWithValue("@Account", Account);
                cmd.Parameters.AddWithValue("@Date", Date);
                cmd.Parameters.AddWithValue("@Description", Description);
                cmd.Parameters.AddWithValue("@Post_Ref", Post_ref);
                cmd.Parameters.AddWithValue("@Debit", Debit);
                cmd.Parameters.AddWithValue("@Credit", Credit);
                cmd.Parameters.AddWithValue("@Balance", Balance);
                cmd.ExecuteNonQuery();
                con.Close();
                Load_data();
                dataGridView1.DataSource = null;
                dataGridView1.Refresh();
            }

}
Posted
v2
Comments
RickZeeland 26-Mar-16 4:08am    
I think it might be because you are not using DBNull.Value but null for Description.
Atul Rokade 26-Mar-16 4:19am    
still same error Parameter @Description has no default value. i changed
if (!object.ReferenceEquals(dataGridView1.CurrentRow.Cells[3].Value, DBNull.Value))
{

Description = dataGridView1.Rows[e.RowIndex].Cells[3].Value.ToString();
RickZeeland 26-Mar-16 4:24am    
Then maybe you have to look at the default value for the field in the database, don't know Access naming but in SQL Server it's called "default".
Atul Rokade 26-Mar-16 4:31am    
i check Description column in table there Default value is nothing not even all fields
RickZeeland 26-Mar-16 4:33am    
I also think you should check if the accountnumber already exists before executing an UPDATE command.

1 solution

You're using else-if-statements for initializing the parameter-values from the datagridview. That makes no sense because it means that only one of those values will be set. You need to change all those to "independent" if-statements.
 
Share this 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