Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Index was outside the bounds of the array.

Author  Topic 

JmaC
Starting Member

18 Posts

Posted - 2010-09-14 : 02:47:53

i create a registration form using asp.net & sql server..
when i click save button it show error:-"Index was outside the bounds of the array."

heres is my code:-

private void ExecuteInsert(string AccNo, string CompanyName, string Desc2, string RegisterNo, string Address1, string Address2, string Address3, string Address4, string PostCode, string DeliverAddr1, string DeliverAddr2, string DeliverAddr3, string DeliverAddr4, string DeliverPostCode, string Attention, string Phone1, string Phone2, string Fax1, string Fax2, string AreaCode, string SalesAgent, string DebtorType, string NatureOfBusiness, string WebURL, string EmailAddress, string DisplayTerm, string UDF_Shipvia, string UDF_freigh, string UDF_SourceIn, string UDF_ProductEnq, string CurrencyCode)
{
SqlConnection conn = new SqlConnection(GetConnectionString());

string sql ="INSERT INTO Debtor (AccNo, CompanyName, Desc2, RegisterNo, Address1, Address2, Address3, Address4, PostCode, CreditLimit, CurrencyCode, DeliverAddr1, DeliverAddr2, DeliverAddr3, DeliverAddr4, DeliverPostCode, Attention, Phone1, Phone2, Fax1, Fax2, AreaCode, SalesAgent, DebtorType, NatureOfBusiness, WebURL, EmailAddress, DisplayTerm, UDF_Shipvia, UDF_freigh, UDF_ProductEnq, UDF_SourceIn) VALUES " + " (@AccNo, @CompanyName, @Desc2, @RegisterNo, @Address1, @Address2, @Address3, @Address4, @PostCode, @DeliverAddr1, @DeliverAddr2, @DeliverAddr3, @DeliverAddr4, @DeliverPostCode, @Attention, @Phone1, @Phone2, @Fax1, @Fax2, @AreaCode, @SalesAgent, @DebtorType, @NatureOfBusiness, @WebURL, @EmailAddress, @DisplayTerm, @UDF_Shipvia, @UDF_freigh, @UDF_ProductEnq, @UDF_SourceIn, @CreditLimit, @CurrencyCode)";

try
{
conn.Open();

SqlCommand cmd = new SqlCommand(sql, conn);
SqlParameter[] param = new SqlParameter[30];


param[0] = new SqlParameter("@AccNo", SqlDbType.NVarChar, 12);
param[1] = new SqlParameter("@Companyname", SqlDbType.NVarChar, 80);
param[2] = new SqlParameter("@Desc2", SqlDbType.NVarChar, 80);
param[3] = new SqlParameter("@RegisterNo", SqlDbType.NVarChar, 25);
param[4] = new SqlParameter("@Address1", SqlDbType.NVarChar, 40);
param[5] = new SqlParameter("@Address2", SqlDbType.NVarChar, 40);
param[6] = new SqlParameter("@Address3", SqlDbType.NVarChar, 40);
param[7] = new SqlParameter("@Address4", SqlDbType.NVarChar, 40);
param = new SqlParameter("@PostCode", SqlDbType.NVarChar, 10);
param[9] = new SqlParameter("@DeliverAddr1", SqlDbType.NVarChar, 40);
param[10] = new SqlParameter("@DeliverAddr2", SqlDbType.NVarChar, 40);
param[11] = new SqlParameter("@DeliverAddr3", SqlDbType.NVarChar, 40);
param[12] = new SqlParameter("@DeliverAddr4", SqlDbType.NVarChar, 40);
param[13] = new SqlParameter("@DeliverPostCode", SqlDbType.NVarChar, 10);
param[14] = new SqlParameter("@Attention", SqlDbType.NVarChar, 40);
param[15] = new SqlParameter("@Phone1", SqlDbType.VarChar, 25);
param[16] = new SqlParameter("@Phone2", SqlDbType.VarChar, 25);
param[17] = new SqlParameter("@Fax1", SqlDbType.VarChar, 25);
param[18] = new SqlParameter("@Fax2", SqlDbType.VarChar, 25);
param[19] = new SqlParameter("@AreaCode", SqlDbType.NVarChar, 12);
param[20] = new SqlParameter("@SalesAgent", SqlDbType.NVarChar, 12);
param[21] = new SqlParameter("@DebtorType", SqlDbType.NVarChar, 20);
param[22] = new SqlParameter("@NatureOfBusiness", SqlDbType.NVarChar, 40);
param[23] = new SqlParameter("@WebURL", SqlDbType.NVarChar, 80);
param[24] = new SqlParameter("@EmailAddress", SqlDbType.NVarChar, 80);
param[25] = new SqlParameter("@DisplayTerm", SqlDbType.NVarChar, 30);
param[26] = new SqlParameter("@UDF_Shipvia", SqlDbType.NVarChar, 50);
param[27] = new SqlParameter("@UDF_freigh", SqlDbType.NVarChar, 50);
param[28] = new SqlParameter("@UDF_SourceIn", SqlDbType.NVarChar, 50);
param[29] = new SqlParameter("@UDF_ProductEnq", SqlDbType.NVarChar, 50);
param[30] = new SqlParameter("@CurrencyCode", SqlDbType.NVarChar);


param[0].Value = AccNo;
param[1].Value = CompanyName;
param[2].Value = Desc2;
param[3].Value = RegisterNo;
param[4].Value = Address1;
param[5].Value = Address2;
param[6].Value = Address3;
param[7].Value = Address4;
param.Value = PostCode;
param[9].Value = DeliverAddr1;
param[10].Value = DeliverAddr2;
param[11].Value = DeliverAddr3;
param[12].Value = DeliverAddr4;
param[13].Value = DeliverPostCode;
param[14].Value = Attention;
param[15].Value = Phone1;
param[16].Value = Phone2;
param[17].Value = Fax1;
param[18].Value = Fax2;
param[19].Value = NatureOfBusiness;
param[20].Value = WebURL;
param[21].Value = EmailAddress;
param[22].Value = UDF_ProductEnq;
param[23].Value = AreaCode;
param[24].Value = SalesAgent;
param[25].Value = DebtorType;
param[26].Value = DisplayTerm;
param[27].Value = UDF_Shipvia;
param[28].Value = UDF_SourceIn;
param[29].Value = UDF_freigh;
param[30].Value = CurrencyCode;



for (int i = 0; i < param.Length; i++)
{
cmd.Parameters.Add(param[i]);
}
cmd.CommandType = CommandType.Text;
cmd.ExecuteNonQuery();
}
catch (System.Data.SqlClient.SqlException ex)
{
string msg = "Insert error:";
msg += ex.Message;
throw new Exception(msg);
}
finally
{
conn.Close();
}

the error message pop out at "AccNo"
This is the (AccNo=300000213)
datatype with database and in form are same..

Thanks & Regards
JmaC

Kristen
Test

22859 Posts

Posted - 2010-09-14 : 03:47:31
I presume you need

SqlParameter[] param = new SqlParameter[31];

as an array of 30 items, based-zero, is 0-29 not 0-30
Go to Top of Page

JmaC
Starting Member

18 Posts

Posted - 2010-09-14 : 21:23:10
yeah i got it kristen..thank you..

Thanks & Regards
JmaC
Go to Top of Page
   

- Advertisement -