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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T00:11:21+00:00 2026-06-03T00:11:21+00:00

I have a web form where I need to add, update, delete and read

  • 0

I have a web form where I need to add, update, delete and read using a unique ID. So far I have managed to add, update and delete functions with little trouble.

However now I am having trouble getting my read function to work (understand I have a webform that has four text fields; ID, FIRSTNAME, SURNAME AND ADDRESS). Basically when an ID that has been previously created (using add button) is entered into the text field and the read button clicked it should update the other 3 text fields with the stored entries depending on the ID entered.

Here is my behind code (cs.) on the web form

protected void cmdRead_Click(object sender, EventArgs e)
{
   // Create a reference to the Web service
   DbWebService.WebService1 proxy = new DbWebService.WebService1();

   // Create a person details object to send to the Web service.
   string ADDRESS;
   string SURNAME;
   string FIRSTNAME;
   string ID;

   ADDRESS = txtAddress.Text;
   SURNAME = txtSurname.Text;
   FIRSTNAME = txtFirstname.Text;
   ID = txtID.Text;

   // Attempt to store in the Web service
   bool rsp = proxy.ReadPerson(int.Parse(ID), FIRSTNAME, SURNAME, ADDRESS);

   // Inform the user
   if (rsp)
   {
       lblOutcome.Text = "Successfully read data.";

       txtFirstname.Text = FIRSTNAME;
       txtSurname.Text = SURNAME;
       txtAddress.Text = ADDRESS;
   }
   else
   {
       lblOutcome.Text = "Failed to read data! Select a previously created ID!";
   }
}

and here is my web function on the web service (which is where the SQL Server Express database is)

[WebMethod]
public bool ReadPerson(int ID, string FIRSTNAME, string SURNAME, string ADDRESS)
{
   // In case of failure failure first
   bool rtn = false;

   // Connect to the Database
   SqlConnection connection = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDbFilename='|DataDirectory|\Database.mdf';Integrated Security=True;User Instance=True");

   // Open the connection
   connection.Open();

   // Prepare an SQL Command
   SqlCommand command = new SqlCommand(String.Format("SELECT FIRSTNAME, SURNAME, ADDRESS FROM PersonalDetails WHERE ID = '{0}'", ID), connection);

   // Execute the SQL command and get a data reader.
   SqlDataReader reader = command.ExecuteReader();

   // Instruct the reader to read the first record.
   if (reader.Read())
   {
      // A record exists, thus the return value is updated
      FIRSTNAME = (string)reader["FIRSTNAME"];
      SURNAME = (string)reader["SURNAME"];
      ADDRESS = (string)reader["ADDRESS"];

      rtn = true;
   }

   // Close the connection
   connection.Close();

   // Return the result.
   return (rtn);

}

Now the problem is when I click read I get a success message (using a label as you can see in the behind code) but the fields don’t update, I assume this is because of the (rtn = true;) statement. Therefore I thought something like this might work:

rtn = (bool)reader["ADDRESS"];

However with this I get a specified cast is not valid, so I figure maybe the bool doesn’t work in this context, I think it might work if I use string instead but how do I convert, I think rtn needs a value in regards to the reader right??

Basically I am just looking for a solution to which will update the text fields in the web form.

  • 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-03T00:11:22+00:00Added an answer on June 3, 2026 at 12:11 am

    There are several problems with your code. The most obvious is that your code cannot ever return the data from the database. You are sending FIRSTNAME etc. to the web service – you are not returning them from the web service.

    There is no reason to have a bool return from the service to tell you whether or not it succeeded. Let the service throw an exception if it failed. Instead, you should return the fields from the database as the return of the service.

    In the service:

    public class Person
    {
        public string FIRSTNAME {get;set;}
        public string SURNAME {get;set;}
        public string ADDRESS {get;set;}
    }
    
    [WebMethod]        
    public Person ReadPerson(int ID)        
    {        
        // ...
        if (reader.Read()) 
        { 
            // A record exists, thus return the value
            Person p = new Person();
            p.FIRSTNAME = (string)reader["FIRSTNAME"]; 
            p.SURNAME = (string)reader["SURNAME"]; 
            p.ADDRESS = (string)reader["ADDRESS"]; 
    
            rtn = p;
       } 
    
        connection.Close();          
    
        return rtn; 
    }           
    

    Also, you should not be using a WebMethod or an ASMX web service unless you have no choice. ASMX is a legacy technology which is kept around only for backwards compatability. It should not be used for new development. You should use WCF instead.


    The other issues with your code are resolved below:

    [WebMethod]
    public Person ReadPerson(int id)
    {
        using (
            var connection =
                new SqlConnection(
                    @"Data Source=.\SQLEXPRESS;
            AttachDbFilename='|DataDirectory|\Database.mdf';
            Integrated Security=True;
            User Instance=True")
            )
        {
            connection.Open();
    
            using (
                var command =
                    new SqlCommand(@"
    SELECT FIRSTNAME, SURNAME, ADDRESS 
    FROM PersonalDetails 
    WHERE ID = @id",
                        connection))
            {
                var idParameter =
                    command.Parameters.Add(
                        "@id", SqlDbType.Int);
                idParameter.Value = id;
    
                using (var reader = command.ExecuteReader())
                {
                    if (!reader.Read())
                    {
                        return null;
                    }
    
                    return new Person
                                {
                                    Firstname =
                                        (string)
                                        reader["FIRSTNAME"],
                                    Surname =
                                        (string)
                                        reader["SURNAME"],
                                    Address =
                                        (string)
                                        reader["ADDRESS"]
                                };
                }
            }
        }
    }
    

    The main issue is that the SqlConnection, SqlCommand, and SqlDataReader all need to be instantiated inside of using blocks. This ensures that the objects are disposed of (closed) whether or not an exception is thrown.

    Next, you should not get into the habit of building queries through string manipulation; not even using String.Format. That leaves you open to “SQL Injection” attacks. Using parameters resolves that problem. See “Commands and Parameters ” in MSDN.

    One last minor issue: I recommend that you get out of the habit of placing comments on obvious statements. For instance, it’s not necessary to comment that Open opens the connection to the database, or that return returns a value.

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

Sidebar

Related Questions

I have a web form where I need to have an additional field displayed
I'm making a web page using CGI scripting which has a form users need
I have web form with a panel which has form elements in side it.
I am running a thread in C#. I have web form. I declared public
I have a web form where I have a textbox in which the user
I have a web form that collects information and submits it to a cgi
I have a web form that manipulates records in a MySQL database. I have
I have a web form that has 2 radio buttons, depending on which one
I have a web form that uses if statements to create a string of
I currently have a web form aspx page that calls RegisterClientScriptBlock. This sends down

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.