gridview更新异常 [英] gridview update exception

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

问题描述



这是我的代码.我在网格视图中有4列,其中一列具有下拉列表作为输入控件,2是文本框控件,1是标签控件(主键).

单击更新操作时出现错误.

Hi,

This is my code. I have a 4 columns in a grid view, one of the column has a drop down list as the input control, 2 are text box controls and 1 is a label control (primary key).

I get the error when the update action is clicked.

Error: **No value given for one or more parameters.**


string updateSql = "UPDATE [RateCenters] " + "SET [RateCenterName] = @RateCenterName, [Province]= @Province, [QuantityThreshold] =@QuantityThreshold  " + "WHERE [RateCenterID]=@RateCenterID";


        protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            GridViewRow row = (GridViewRow)GridView1.Rows[e.RowIndex];

            DropDownList ddl = (DropDownList)row.FindControl("DropDownList2");

            TextBox rateCenterName = (TextBox)row.FindControl("txtRateCenterName");

            TextBox quantityThreshold = (TextBox)row.FindControl("txtQuantityThreshold");

            Label ratecenterid = (Label)row.FindControl("Label1");

            OleDbConnection conn = new OleDbConnection(ConfigurationManager.ConnectionStrings["DBConnection"].ToString());

            OleDbCommand cmd = null;

            try
            {
                conn.Open();

                cmd = new OleDbCommand(updateSql, conn);

                cmd.Parameters.Add("@RateCenterID", OleDbType.Integer).Value = Convert.ToInt32(ratecenterid.Text);
                cmd.Parameters.Add("@RateCenterName", OleDbType.VarChar).Value = rateCenterName.Text;
                cmd.Parameters.Add("@Province", OleDbType.VarChar).Value = ddl.SelectedValue;
                cmd.Parameters.Add("@QuantityThreshold", OleDbType.Integer).Value = Convert.ToUInt32(quantityThreshold.Text);


                cmd.Connection = conn;
                cmd.ExecuteNonQuery();

                GridView1.EditIndex = -1;
                GridView1.DataBind();
            }

            catch (OleDbException ex)
            {
                throw (ex);
            }

            finally
            {
                cmd.Dispose();
                conn.Close();
                conn.Dispose();
            }
        }

帮助我解决此问题.

问候,
Arjun

Help me to solve this issue.

regards,
Arjun

推荐答案

如果我没记错的话,.i之前也遇到过类似的问题,并且是因为将空值传递给了参数化查询.
If i remember correctly ..i faced similar issue before and was because of null value getting passed to the parameterized query.


如果您将参数赋值为null,则会给出类似您所获得的消息.请注意,数据库不知道null,要在表中传递null,您必须传递"DBNull.Value",代表不存在的数据.
如果在内部传递null,则生成的查询将尝试为该列分配默认约束值,因此,如果列上存在默认约束,则不会收到此错误.

在将值传递给参数化查询时,应进行此类检查.
If the value you are assining to the parameter is null it will give message like what you have got.Please note database doesn''t know about null, to pass null in table you have to pass "DBNull.Value", which represent non existent data.
If you pass null internally the query generated will try to assign Default Constraint Value for that column, so if there is default constraint on your column you will not get this error.

You should check like this while passing value to parameterized query.
param[0].Value=myValue==null?(object)DBNull.Value : myValue
or
param[0].Value=myValue??(object)DBNull.Value


这篇关于gridview更新异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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