我正在尝试更新DataGridView
中的单元格,但是它给了我一个带有消息的异常:
参数
@Description
没有默认值
如果我点击进入单元格,它会给我:
索引或主键在插入查询中不能包含Null值。
AccountNumber
是我的主键,但是现在整个表都是空白的,插入查询我写在dataGridView1_RowLeave
事件上,而更新查询写在dataGridView1_CellValueChanged
事件上
插入代码:
if (dataGridView1.IsCurrentRowDirty)
{
string connectionString = null;
connectionString = ConfigurationManager.ConnectionStrings["AccessConnectionString"].ConnectionString;
con.ConnectionString = connectionString;
string cmd1 = "insert into Ledger([AccountNumber],[Account],[Date],[Description],[Post_Ref],[Debit],[Credit],[Balance])values(?,?,?,?,?,?,?,?)";
OleDbCommand cmd = new OleDbCommand(cmd1, con);
con.Open();
cmd.CommandType = CommandType.Text;
int accountNumber;
bool accountHasValue = int.TryParse(
dataGridView1.Rows[e.RowIndex].Cells["AccountNumber"].Value.ToString(),
out accountNumber);
if (accountHasValue)
{
cmd.Parameters.AddWithValue("@AccountNumber", accountNumber);
}
else
{
cmd.Parameters.AddWithValue("@AccountNumber", DBNull.Value);
}
string accounts = dataGridView1.Rows[e.RowIndex].Cells["Account"].Value.ToString();
cmd.Parameters.AddWithValue("@Account", accounts);
DateTime datetime;
bool dateTimeHasValue = DateTime.TryParse(dataGridView1.Rows[e.RowIndex].Cells["Date"].Value.ToString(), out datetime);
if (dateTimeHasValue)
{
cmd.Parameters.AddWithValue("@Date", datetime);
}
else
{
cmd.Parameters.AddWithValue("@Date", DBNull.Value);
}
string Description = dataGridView1.Rows[e.RowIndex].Cells["Description"].Value.ToString();
cmd.Parameters.AddWithValue("@Description", Description);
string Post_Ref = dataGridView1.Rows[e.RowIndex].Cells["Post_Ref"].Value.ToString();
cmd.Parameters.AddWithValue("@Post_Ref", Post_Ref);
int debit;
bool debitHasValue = Int32.TryParse(
dataGridView1.Rows[e.RowIndex].Cells["Debit"].Value.ToString(),
out debit);
if (debitHasValue)
{
cmd.Parameters.AddWithValue("@Debit", debit);
}
else
{
cmd.Parameters.AddWithValue("@Debit", DBNull.Value);
}
int Credits;
bool CreditsHasValue = Int32.TryParse(
dataGridView1.Rows[e.RowIndex].Cells["Credit"].Value.ToString(),
out Credits);
if (CreditsHasValue)
{
cmd.Parameters.AddWithValue("@Credit", Credits);
}
else
{
cmd.Parameters.AddWithValue("@Credit", DBNull.Value);
}
int Balances;
bool BalancesHasValue = Int32.TryParse(
dataGridView1.Rows[e.RowIndex].Cells["Balance"].Value.ToString(),
out Balances);
if (BalancesHasValue)
{
cmd.Parameters.AddWithValue("@Balance", Balances);
}
else
{
cmd.Parameters.AddWithValue("@Balance", DBNull.Value);
}
cmd.ExecuteNonQuery();
dataGridView1.DataSource = null;
dataGridView1.Refresh();
con.Close();
}
更新代码:
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();
}
}