Monday 15 June 2015

c# - Efficiently updating a table's details? -


What I am trying to do is recover a patient's details from the database, then entered the text box Based on the values ​​passed, it will update the patient's details, but if the text box is empty, then I allocate the existing details in the text box which I think is not stupid.

What would you suggest that I will do the code below to make more efficient or less repetition?

Code:

  Client = New ServiceReference1.Service1Client (); Pat = client.GetPatientbyID (txtUpdatePatientID.Text); GrdView.DataSource = pat; GrdView.DataBind (); If (txtUpdateForename.Text == "") txtUpdateForename.Text = pat [0]. Subtitle; If (txtUpdateSurname.Text == "") txtUpdateSurname.Text = pat [0] .name; If (txtUpdateStreet.Text == "") txtUpdateStreet.Text = pat [0] .street; If (txtUpdateTown.Text. == "") txtUpdateTown.Text = pat [0]. Click; If (txtUpdateCity.Text == "") txtUpdateCity.Text = pat [0]. Whistle; If (txtUpdateDOB.Text == "") txtUpdateDOB.Text = pat [0] .DOB.ToString (); If (txtUpdateHouseNumber.Text == "") txtUpdateHouseNumber.Text = pat [0] .House_number; If (txtUpdateMobile.Text == "") txtUpdateMobile.Text = pat [0] .mobile; Client.UpdateAllDetails (txtUpdatePatientID.Text, txtUpdateForename.Text, txtUpdate.Surname.Text, txtUpdateStreet.Text, txtUpdateTown.Text., TxtUpdateCity.Text, convert toDateTime (txtUpdateDOB.Text), txtUpdateHouseNumber.Text, txtUpdateMobile.Text); Client.Close (); GetPatients (txtUpdatePatientID); As he looked at the comments, I do not think you can do a lot of this.    

faster; If this is your logic, then this should be done.

Can you organize if check and conditional set to see this code?

Something like this:

  Private Zero SetIfNotEmpty (text box text boxtoetset, string value) {if (textBoxToSet.Text == "") textBoxToSet.Text = value; }   

then

 replace  (if txtUpdateForename.Text == "") txtUpdateForename.Text = pat [0] .name; If (txtUpdateSurname.Text == "") txtUpdateSurname.Text = pat [0] .name; If (txtUpdateStreet.Text == "") txtUpdateStreet.Text = pat [0] .street; If (txtUpdateTown.Text. == "") txtUpdateTown.Text = pat [0]. Click; If (txtUpdateCity.Text == "") txtUpdateCity.Text = pat [0]. Whistle; If (txtUpdateDOB.Text == "") txtUpdateDOB.Text = pat [0] .DOB.ToString (); If (txtUpdateHouseNumber.Text == "") txtUpdateHouseNumber.Text = pat [0] .House_number; If (txtUpdateMobile.Text == "") txtUpdateMobile.Text = pat [0] .mobile; With   

  SetIfNotEmpty (txtUpdateForename, pat [0] .forname); SetIfNotEmpty (txtUpdateSurname.Text, pat [0] .name); SetIfNotEmpty (txtUpdateStreet.Text, pat [0] .street); SetIfNotEmpty (txtUpdateTown.Text, pat [0]. Town); SetIfNotEmpty (txtUpdateCity.Text, pat [0]. City); SetIfNotEmpty (txtUpdateDOB.Text, pat [0] .DOB.ToString ()); SetIfNotEmpty (txtUpdateHouseNumber.Text, pat [0] .House_number); SetIfNotEmpty (txtUpdateMobile.Text, pat [0]. Mobile);   

As a high level design point, if these are WPF textboxes, then have you thought of using databases?

No comments:

Post a Comment