更新gridview中的错误 [英] error in update gridview

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

问题描述

嗨 我想更新网格视图中的一个单元格时收到错误消息
错误是:

索引(从零开始)必须大于或等于零且小于参数列表的大小.
说明:执行当前Web请求期间发生未处理的异常.请查看堆栈跟踪,以获取有关错误及其在代码中起源的更多信息.

异常详细信息:System.FormatException:索引(从零开始)必须大于或等于零且小于参数列表的大小.

源错误:


第72行:SqlCommand cmd =新的SqlCommand();
第73行:cmd.Connection = con;
第74行:cmd.CommandText = string.Format("UPDATE ActionType SET AName = N" {0}"WHERE ACode = N" {1}",con);

为什么?
请帮帮我吗?

解决方案

您的字符串格式中有{0}和{1},但没有给它们指定值.

像下面一样,您必须为每个商品提供参数.

 字符串 test = 字符串 .Format(  A = {0},B = {1}",AValue,BValue);



您将连接对象放在错误的位置,请检查以下示例代码:

字符串更新Sql = "  +   SET LastName = @LastName" +  ;
SqlCommand UpdateCmd =新的SqlCommand(updateSql,thisConnection);

// 2 .地图参数

UpdateCmd.Parameters.添加(" ,SqlDbType. NVarChar  10 "span> 名字");

UpdateCmd.Parameters.添加(" ,SqlDbType. NVarChar  20 "span> 姓氏");

UpdateCmd.Parameters [" ].Value =  " ;
UpdateCmd.Parameters [" ].Value =  " ;

UpdateCmd.ExecuteNonQuery(); 



使用sql参数比使用字符串格式更好,可以避免sql注入攻击


hi i recieve an error when I want update one of cells in grid view
error is:

Index (zero based) must be greater than or equal to zero and less than the size of the argument list.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.FormatException: Index (zero based) must be greater than or equal to zero and less than the size of the argument list.

Source Error:


Line 72: SqlCommand cmd = new SqlCommand();
Line 73: cmd.Connection = con;
Line 74: cmd.CommandText = string.Format("UPDATE ActionType SET AName=N''{0}'' WHERE ACode=N''{1}''", con);

why?
please help me?

解决方案

you have {0} and {1} in your string format but you not have given values for those.

like below you have to give parameters for each item you have.

string test = string.Format("A={0}, B ={1}", AValue, BValue);



you have given connection object in wrong place, check below sample code:

string updateSql = "UPDATE Employees " + "SET LastName = @LastName " + "WHERE FirstName = @FirstName";
SqlCommand UpdateCmd = new SqlCommand(updateSql, thisConnection);

// 2. Map Parameters

UpdateCmd.Parameters.Add("@FirstName", SqlDbType.NVarChar, 10, "FirstName");

UpdateCmd.Parameters.Add("@LastName", SqlDbType.NVarChar, 20, "LastName");

UpdateCmd.Parameters["@FirstName"].Value = "Wade";
UpdateCmd.Parameters["@LastName"].Value = "Harvey";

UpdateCmd.ExecuteNonQuery();



better to use sql parameters than using string format, you can avoid sql injection attacks


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

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