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:
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();
}
}