参数@description没有默认值异常 [英] Parameter @description has no default value exception

查看:302
本文介绍了参数@description没有默认值异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

iam尝试更新datagridview中的单元格,但它给我参数@Description没有默认值异常,如果我点击进入单元格,它给我索引或主键在插入查询中不能包含Null值AccountNumber是我的主键但是现在所有表都是空白的,插入查询我写在dataGridView1_RowLeave事件上,而更新查询写在dataGridView1_CellValueChanged事件上(



我尝试过:



更新代码:





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

}

推荐答案

您正在使用其他 - if -statements,用于初始化datagridview中的参数值。这没有任何意义,因为它意味着只会设置其中一个值。您需要将所有这些更改为独立if语句。
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.


这篇关于参数@description没有默认值异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆