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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T14:03:14+00:00 2026-05-26T14:03:14+00:00

I am working on a code in C#.NET to create a web service that

  • 0

I am working on a code in C#.NET to create a web service that will take in a particular input. It will then connect to the database , where depending on the input provided, the entire result must be fetched from my table and displayed accordingly.

However, I am not that familiar with C#.NET and so I am not able to implement my code properly. can someone please help me

Here is what I have do so far:

       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 test3
     {
          /// <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
   {
      [WebMethod]
        public String GetAttendance(String rollno)
       {
          String result="";


          try
           {
              using (SqlConnection myConnection = new SqlConnection(@"Data Source=.\SQLEXPRESS;Initial Catalog=student;User ID=sa;Password=123"))
            {

                myConnection.Open();

                using (SqlCommand myCommand = new SqlCommand())
                {

                    myCommand.Connection = myConnection;
                    myCommand.CommandText = "SELECT COUNT(*) FROM studentdata WHERE rollno = @rollno";


                    myCommand.Parameters.Add("@rollno", SqlDbType.VarChar).Value = rollno;

                    SqlDataReader myReader = myCommand.ExecuteReader();
                    while (myReader.Read())
                    {
                       result = myReader.ToString();

                    }

                }
            }
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex.Message);
            return "an error occured";
        }

        return result;
     }

   } 

}

If I run this code, I get output as “System.Data.SqlClient.SqlDataReader” which is not what i want

  • 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-26T14:03:15+00:00Added an answer on May 26, 2026 at 2:03 pm

    Don’t use a data reader for this you only have one result which is a row count so use ExecuteScalar() and use an int as type for result:

    int result = Convert.ToInt32(myCommand.ExecuteScalar());
    

    (Alternatively you can get the string value with your current query using result = myReader.GetInt32(0).ToString(); – don’t do this though)

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

Sidebar

Related Questions

I'm working on a web service that will create orders in SAP using .NET.
I am working on an ASP.NET page that we, in code impersonate the requesting
I'm working in a web application using VB.NET. There is also VisualBasic code mixed
Very often when working on an ASP.NET web site, the options View Code and
The scenario I am facing is that I have an ASP.NET web service (.NET
I'm working on an ASP.NET webforms app that will serve as a simple intake
I create an asp.net 4.0 web application which has a web service for uploading
I'm working on a .NET app that calls 3rd party web services over the
I'm working with code I'm converting to Pgsql working with .NET. I want to
I'm working on a project where we mix .NET code and native C++ code

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.