Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6202153
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T04:40:58+00:00 2026-05-24T04:40:58+00:00

I want to return an array of Strings in the form abc#xyz#ghi#tru (where #

  • 0

I want to return an array of Strings in the form “abc#xyz#ghi#tru” (where # is delimiter) from my web service method . However i m not able to do it . Here is my current web service code :

using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.Xml.Linq;
using System.Data.SqlClient;

namespace WebService10
{
    /// <summary>
    /// Summary description for Service1
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [ToolboxItem(false)]
    // To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment
 the following line. 
    // [System.Web.Script.Services.ScriptService]
    public class Service1 : System.Web.Services.WebService
    {
        String[] result=new String[40];
        String[] result2 = new String[40];

        [WebMethod]
        public String[] getData()
        {
            SqlConnection myConnection = new SqlConnection(@"Data Source=.\SQLEXPRESS;Initial Catalog=student;User ID=sa;Password=123");
            try
            {
                myConnection.Open();

                SqlCommand myCommand = new SqlCommand();
                myCommand.Connection = myConnection;
                myCommand.CommandText = "select count(*) from names where name =@name";

                SqlDataReader myReader = myCommand.ExecuteReader();

                //while
                for(int i=0;i<40;i++)
                {
                    if (myReader.Read())
                    {
                         result[i]= myReader["name"].ToString();
                         result2[i] = result[i] + "#";
                    }
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
            finally
            {
                myConnection.Close();
            }

            return result2;
        }
    }
}

Can anyone tell me what’s wrong with my code ?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T04:40:59+00:00Added an answer on May 24, 2026 at 4:40 am

    Try this:

    I changed the query (BTW: the query still doesn’t make much sense but I do not know what you really want), the resulting type and the loop.

    You forgot to pass the parameter to the query too.

    Also: change the exception handling; writing to the console on the server side is not a good idea.

    public class Service1 : System.Web.Services.WebService
    {
    
        [WebMethod]
        public String getData(string nameFilter)
        {
            String result = "";
    
            SqlConnection myConnection = new SqlConnection(@"Data Source=.\SQLEXPRESS;Initial Catalog=student;User ID=sa;Password=123");
            try
            {
                myConnection.Open();
    
                SqlCommand myCommand = new SqlCommand();
                myCommand.Connection = myConnection;
                myCommand.CommandText = "select name from names where name =@name";
                myCommand.Parameters.AddWithValue("@name", nameFilter);
                SqlDataReader myReader = myCommand.ExecuteReader();
    
                while(myReader.Read())
                {
                    if(result.Length > 0)
                    {
                        result += "#";
                    }
                    result += myReader["name"].ToString();
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
            finally
            {
                myConnection.Close();
            }
    
            return result;
        }
    }
    

    EDIT

    I’d prefer a different approach:

    public class Service1 : System.Web.Services.WebService
    {
    
        [WebMethod]
        public String[] getData(string nameFilter)
        {
            var names = new List<string>();
            SqlConnection myConnection = new SqlConnection(@"Data Source=.\SQLEXPRESS;Initial Catalog=student;User ID=sa;Password=123");
            try
            {
                myConnection.Open();
    
                SqlCommand myCommand = new SqlCommand();
                myCommand.Connection = myConnection;
                myCommand.CommandText = "select name from names where name = @name";
                myCommand.Parameters.AddWithValue("@name", nameFilter);
                SqlDataReader myReader = myCommand.ExecuteReader();
    
                while(myReader.Read())
                {
                    names.Add(myReader["name"].ToString());
                }
            }
            catch (Exception ex)
            {
                //Console.WriteLine(ex.Message);
            }
            finally
            {
                myConnection.Close();
            }
    
            return names.ToArray();
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm using Array.sample to return a random element from an array. I want to
I have a Web Service written in Java. I want to send some strings
Hey! I want to create an array of fields. however my code return an
I want to return just 1 item in the filtered array my code is
Good Day, I'm creating a VBScript function to return an array. But I want
I want to return a private field from a remoted object but i get
I want to return a set of values from function till the point they
I want to return some errors to my jquery method. What is happening is
I have a ASP.NET web service decorated with System.Web.Script.Services.ScriptService() so it can return json
I want to pass a custom made vector object containing data from background service

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.