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 7800087
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T00:31:12+00:00 2026-06-02T00:31:12+00:00

Complete newbie in VS and C#, I am trying to setup a web service,

  • 0

Complete newbie in VS and C#, I am trying to setup a web service, starting by something very simple. I managed to write a class that will (hopefully) return an item’s name, given the item’s code. Now I want to implement this so that it can be called from the web, but I do not know how to retrieve the parameter. The idea is to have http://myurl.com?ProdRef=XYZ, and return the name of product XYZ.
Please don’t shoot, this is my first try with C# and VS !

Edit: working code (for whom it may help)

Class code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Data.SqlClient;

/// <summary>
/// Summary description for Class1
/// </summary>
class Product    
    {
        public string ProdRef {get; set; }
        public string ProdName{get; set; }

        public static string GetLabel(string ProdRef)
        {
            //
            // create a new SqlConnection object with the appropriate connection string 
            SqlConnection sqlConn = new SqlConnection("Data Source=ssss;Initial Catalog=ccccc;Persist Security Info=True;User ID=uuu;Password=pppp;Network Library=dbmssocn");
            // open the connection 
            sqlConn.Open();
            // create the command object 
            SqlCommand sqlComm = new SqlCommand("select libelle from dbo.vwArticlesPerm WHERE Ref = '" + ProdRef + "'", sqlConn);
            string strResult = (string)sqlComm.ExecuteScalar();
            // close the connection
            sqlConn.Close();
            return strResult;
        }
    }

Web service code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;

/// <summary>
/// Summary description for WebService
/// </summary>
[WebService(Namespace = "http://xxxxx.lu/clientwebserv")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
// 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 WebService : System.Web.Services.WebService {

    public WebService () {

        //Uncomment the following line if using designed components 
        //InitializeComponent(); 
    }

    [WebMethod]
    public string GetName(string prName) {
        return Product.GetLabel(prName);
    }

}
  • 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-06-02T00:31:14+00:00Added an answer on June 2, 2026 at 12:31 am

    I think you should define GetName in this way

    [WebMethod]
    public string GetName(string name) 
    {
        return Product.GetLLabel(name);
    }
    

    When a client app loads your WSDL, xml returned shows that GetName function wants a string in input and gives back a string. That’s all.

    Next: don’t build queries manually joining strings, int, dates, use Parameters.

    SqlCommand sqlComm = new SqlCommand(
        "select libelle from dbo.vwArticlesPerm WHERE Ref = @par", sqlConn);
    cmd.Parameters.AddWithValue("@par", ProdRef);
    

    If you want to use GetLabel, you must add public to its declaration or you won’t be able to see it outside its class!

    public static string GetLabel(string ProdRef)
    {
        string strResult = null;
        using (SqlConnection sqlConn = new SqlConnection("server=xxx;uid=yyy;pwd=zzz;database=myerp;"))
        {
            sqlConn.Open();
            using (SqlCommand sqlComm = new SqlCommand("select libelle from dbo.vwArticlesPerm WHERE Ref = '" + ProdRef + "'", sqlConn))
                strResult = (string)sqlComm.ExecuteScalar();
            sqlConn.Close();
            return strResult;
        }
    }
    

    Another advise I give you is to use using(...) when instantiating classes that implements IDisposable interface: when execution exits from using block classes are disposed, so you can avoid memory waste and some headache 🙂

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I will begin by saying that I'm a complete newbie as far as AppleScript
I'm a complete newbie in OCaml and trying to create a simple console program.
Hi im a complete newbie and this may seem very simple to any experienced
I'm a complete newbie to JavaScript trying to understand simple things.... Please look at
Complete C# newbie here, so cut me some slack. I have this application that
I'm a complete newbie to Windows and COM programming, trying to use com4j in
I'm sorry, I'm a complete newbie to CSS and I'm trying to create a
I'm trying to build a website that will allow user to upload video files
I'm a complete WinDbg newbie and I've been trying to debug a WindowsXP problem
Complete rails newbie trying to get started. I have two classes, Ingredient, and Unit.

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.