How to synchronize DataTables with database
<p>I'm creating a small wpf project which involves users registration. In this project I'm using dataAdapter to load data from database to datatables.</p>

<pre><code>con = new SqlConnection(connectionString);

string query = "select * from Users;";
cmd = new SqlCommand(query, con);
da = new SqlDataAdapter();

da.SelectCommand = cmd;

users = new DataTable();
con.Open();
da.Fill(users);
</code></pre>

<p>Database have 6 columns Id which is auto increment, username, password, name and two int type columns with default value to Zero. Here is database schema.</p>

<pre><code>CREATE TABLE [dbo].[Users] (
    [Id]        INT           IDENTITY (1, 1) NOT NULL,
    [username]   NVARCHAR (20) NOT NULL,
    [password]  NVARCHAR (20) NOT NULL,
    [name]      NVARCHAR (50) NULL,
    [attempted] INT           DEFAULT ((0)) NULL,
    [correct]   INT           DEFAULT ((0)) NULL,
    PRIMARY KEY CLUSTERED ([Id] ASC),
    UNIQUE NONCLUSTERED ([username] ASC)
);
</code></pre>

<p>Whenever I insert new user into datatable I only provide username, password and name remaining field should be filled automatically but datatable only save provided fields and remaining are left blank and when I call </p>

<pre><code>da.update(users);
</code></pre>

<p>it update data in database and remaining fields are filled automatically butt datatable still contain those fields as empty columns.</p>

<p><a href="https://i.stack.imgur.com/ohkqp.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/ohkqp.png" alt="Image without user Id"></a></p>

<p>But I need user Id to update or delete user. Problem is how can I sync datatable with database for updated fields so I could use them for further operations. Thanks in Advance for your assistance.</p>

<p><strong>Update:</strong>
Insert query and parameters are given below</p>

<pre><code>query = "insert into Users (username, password, name)values(@u, @p, @n)";
SqlCommand insertcmd = new SqlCommand(query, con);
insertcmd.Parameters.Add(new SqlParameter("u", SqlDbType.NVarChar, 20, "username"));
insertcmd.Parameters.Add(new SqlParameter("p", SqlDbType.NVarChar, 20, "Password"));
insertcmd.Parameters.Add(new SqlParameter("n", SqlDbType.NVarChar, 50, "name"));

da.InsertCommand = insertcmd;
</code></pre>