Question Update using Table Adapter

Govind Sankar

Active member
Joined
May 15, 2020
Messages
42
Programming Experience
Beginner
Hi,

I have a sql server database with ID, First Name, Last Name, Phone Number and Address. I have created a wpf application that has a dataset of the database and uses a datagrid view to view the table. I did this as a learning procedure. Now I am learning how to update the database using wpf application in Visual Studio. I learned how to update one value which is phone number. I use a table adapter to update the phone number using first name with a button. When u press the button it updates the database.
C#:
private void Update_Click(object sender, RoutedEventArgs e)
{
    string FirstName = FN1_Name.Text;
    string NewPhoneNo = Interaction.InputBox("Enter Phone Number", "Update", "");
   
    if (NewPhoneNo != "")
    {
        friends1TableAdapter.UpdatePhoneNumberQuery(NewPhoneNo, FirstName);
    }
}

Update is the button when clicked this happens which is get the first name and then use it to update the phone number using the table adapter which has the sql query
SQL:
Update Friends1
Set [Phone Number] = @Phone_Number
Where ([First Name] = @First_Name)

This part worked. But now what I am trying to do is update any value that is update First Name, Last Name, Phone Number or Address using ID and by just using one button and one table adapter. Is this possible or do I have to create seperate buttons and seperate table adapters for each column. Thank you.
 
Last edited by a moderator:
You should be using the ID in the WHERE clause as that is presumably the primary key so that's what it's for. You should be using the primary key in the WHERE clause and every other column that can be modified in the SET clause.

That said, if you're using a grid, you shouldn't really be saving changes one record at a time. Retrieve all the data into a DataTable, bind that to your UI or map the data to some other data structure and bind that, let the user do all the changes they want, map the changes back to the DataTable if necessary, then save the lot with a single call to the Update method of the table adapter. You should only really be using per-record methods like that when you're only editing one record at a time.
 
You should be using the ID in the WHERE clause as that is presumably the primary key so that's what it's for. You should be using the primary key in the WHERE clause and every other column that can be modified in the SET clause.

That said, if you're using a grid, you shouldn't really be saving changes one record at a time. Retrieve all the data into a DataTable, bind that to your UI or map the data to some other data structure and bind that, let the user do all the changes they want, map the changes back to the DataTable if necessary, then save the lot with a single call to the Update method of the table adapter. You should only really be using per-record methods like that when you're only editing one record at a time.

Thank You. But my question is I only want to update one value and all the other should remain same. For eg:
Id = 4, First Name = Henry, Last Name = Thomas, Phone Number = 8151437209.
But say I entered the phone number wrong. The correct Phone Number is 9276541682.

So I want to update only the phone number to the new value keeping all the other values same. Then what should I do is my question.
 

Latest posts

Back
Top Bottom