如何解决此错误“错误索引(从零开始)必须大于或等于零且小于参数列表的大小”。 [英] How do I resolve this error "error Index (zero based) must be greater than or equal to zero and less than the size of the argument list"

查看:142
本文介绍了如何解决此错误“错误索引(从零开始)必须大于或等于零且小于参数列表的大小”。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Hello Everybody,当我尝试在SQL-Server数据库中执行插入操作时,我接触到FormatException错误。错误是

Hello Everybody,I am come in contact a FormatException error when I try to doing the insertion operation in SQL-Server Database.The error is "

error Index (zero based) must be greater than or equal to zero and less than the size of the argument list

。所以我找不到解决方案。请帮我解决这个问题。下面是代码:



".So I didn't find the solution.Please help me in resolving my this issue.Here's below is the code:

public static bool Insert(Products product)
        {
            string connString = @"Data Source=ps201\SQLEXPRESS;Initial Catalog=E-commerce;Integrated Security=True;Pooling=False";
            SqlConnection con = new SqlConnection(connString);
            con.Open();
            SqlCommand com = new SqlCommand();
            com.Connection = con;
            com.CommandText = string.Format("insert into Products(Name,Price,ImageUrl,Detail,ProductCode,Item,CategoryID) values('{0}','{1}','{2}','{3}','{4}','{5}','{6}','{7}')",product.Name,product.Price,product.ImageUrl,product.Detail,product.ProductCode,product.Item,product.CategoryId);   //error
            int rowAffected = com.ExecuteNonQuery();
            con.Close();
            if (rowAffected > 0)
                return true;
            else
                return false;
        }

推荐答案

你只有7个参数删除'{7}'
you have only 7 parameters remove '{7}'


似乎您试图插入具有非现有类别ID的产品
Seems you are trying to insert a product with non existing category id


这篇关于如何解决此错误“错误索引(从零开始)必须大于或等于零且小于参数列表的大小”。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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