title: How to Use a Web Service to Update TextBox controls in ASP.NET?
body: <p>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. </p>
  
  <p>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. </p>
  
  <p>Here is my behind code (cs.) on the web form</p>
  
  <pre><code>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!";
     }
  }
  </code></pre>
  
  <p>and here is my web function on the web service (which is where the SQL Server Express database is)</p>
  
  <pre><code>[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);
  </code></pre>
  
  <p>} </p>
  
  <p>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 (<code>rtn = true;</code>) statement. Therefore I thought something like this might work: </p>
  
  <pre><code>rtn = (bool)reader["ADDRESS"];
  </code></pre>
  
  <p>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?? </p>
  
  <p>Basically I am just looking for a solution to which will update the text fields in the web form. </p>