Click here to Skip to main content
16,017,502 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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?
Posted

1 solution

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.

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


you have given connection object in wrong place, check below sample code:
SQL
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
 
Share this answer
 

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