Friday, March 16, 2012

Parameter problem with arrays

I'm using arrays in a web application i'm building and it's been a whole day that i can't work this out.

Specifically, i am trying to call a method that i created

" public static int ExecuteNonQuery (string proc, SqlParameter [] param) "

So in the page that i'm calling it, i'm also declaring the array of SqlParameter like this:

SqlParameter[] param = new SqlParameter[4];

-->param[0] = {"@dotnet.itags.org.sname", SqlDbType.NVarChar, 30};
param[0].Value = this.TextBox3.Text;
-->param[1] = {"@dotnet.itags.org.saddr", SqlDbType.NVarChar, 60};
param[1].Value = this.TextBox4.Text;
-->param[2] = {"@dotnet.itags.org.sparentphone", SqlDbType.NVarChar, 24};
param[2].Value = this.TextBox5.Text;
--> param[3] = {"@dotnet.itags.org.sparentemail", SqlDbType.NVarChar, 40};
param[3].Value = this.TextBox6.Text;
-->param[4] = {"@dotnet.itags.org.sdob", SqlDbType.NVarChar, 10};
param[4].Value = this.TextBox7.Text;

But when i run it i get errors on the lines above pointed by the errors.
Errors like " ; expected" and others syntactical ones. What is wrong with my syntax?

Have you got any suggestions?When you create the array 'param' with the new statement new SqlParameter[4], you are only creating an array of four object pointers. You must still create an instance of SqlParamter for each array entry. So,

param[0] = new SqlParameter("@.sname", SqlDbType.NVarChar, 30);

Should do it.

0 comments:

Post a Comment