I have a Web Service(.ASMX) code that takes a String Array as a parameter and then inserts each individual value in the database
For example let’s say I have array like:
String arr[]={"john","annie"};
Now I want to put “john” and “annie” in a column in my database
I have implemented a code for it but it’s not working and the values are not inserted. Also is there any way to test my webservice application on localhost in my browser.
Here is the web service code:
public class Service1 : System.Web.Services.WebService
{
[WebMethod]
public String getnames(String[] values)
{
try
{
using (SqlConnection myConnection = new SqlConnection(@"Data Source=.\SQLEXPRESS;Initial Catalog=student;User ID=sa;Password=123"))
{
int count = 1;
int rows = 0;
myConnection.Open();
foreach (string student in arr)
{
count++;
using (SqlCommand myCommand = new SqlCommand())
{
myCommand.Connection = myConnection;
myCommand.CommandText = "insert into record values(@pCount, @pStudent)";
SqlParameter param = myCommand.CreateParameter();
param.ParameterName = "@pCount";
param.Value = count;
myCommand.Parameters.Add(param);
param = myCommand.CreateParameter();
param.ParameterName = "@pSudent";
param.Value = student;
rows = myCommand.ExecuteNonQuery();
}
}
}
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
return "an error occured";
}
return "success";
}
}
}
YOu can debug your service by making the asmx the startpage and clicking run to debug. You should be able to fill in a value into the text box if you are accepting paramaters.
I would do it different I would create a Stored proc to handle the work that looks like this The code assumes a delimited string so its not an array scenario.. but its awesome code..
Then your code would only have to do this
Good luck