如何解决此错误“错误索引(从零开始)必须大于或等于零且小于参数列表的大小”。
问题描述:
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