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";
}
}
}