Click here to Skip to main content
15,880,967 members
Please Sign up or sign in to vote.
1.44/5 (2 votes)
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;
        }
Posted
Comments
Sergey Alexandrovich Kryukov 17-Sep-14 10:21am    
Which part of this error message is unclear?
—SA
AnoopGharu 17-Sep-14 11:09am    
Thank you for your concern, now i resolve it :)

you have only 7 parameters remove '{7}'
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 17-Sep-14 10:22am    
Good catch, a 5.
—SA
AnoopGharu 17-Sep-14 11:33am    
Sir i got the solution of my above problem but now I am having problem related to SQL-Server database and the error is about the Foreign key.The error is that "The INSERT statement conflicted with the FOREIGN KEY constraint "FK_Products_Products". The conflict occurred in database "E-commerce", table "dbo.Category", column 'ID'.The statement has been terminated.".Please help me
AnoopGharu 17-Sep-14 11:08am    
Thank you Sir, now I got it but now I am having problem related to SQL-Server database and the error is about the Foreign key.The error is that "The INSERT statement conflicted with the FOREIGN KEY constraint "FK_Products_Products". The conflict occurred in database "E-commerce", table "dbo.Category", column 'ID'.The statement has been terminated."
Seems you are trying to insert a product with non existing category id
 
Share this answer
 
Comments
AnoopGharu 18-Sep-14 1:44am    
Thank you Sir :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900