Reputation: 83
I'm using VS2k10 to write a C# program that requires a username and a password to run. I have a database saved in an external file with the usernames and passwords. When I click register in the login form, another form opens that allows you to save your credentials to the login database.
The problem is probably in the register form, not the login one. When I successfully register a user account, the login form doesn't seem to recognize that username. It doesn't save to the file either. Checked SE on how to commit changes to the dataset, but nothing works so far.
My current code is:
DataRow foundRow = ds.Tables["userstable"].Rows.Find(username.Text);
if (foundRow != null)
{ MessageBox.Show("Username already exists!"); }
else
{
DataRow newrow = ds.Tables["userstable"].NewRow();
newrow["ID"] = username.Text;
newrow["hash"] = CalculateMD5Hash(password.Text + username.Text + "hambába");
ds.Tables["userstable"].Rows.Add(newrow);
username.Text = "";
password.Text = "";
repeatpass.Text = "";
ds.AcceptChanges();
MessageBox.Show("Registration complete!");
}
"ds" is the dataset used. The table has two columns - "ID" and "hash", which contain the username and the hash counted from the password. What's the issue, did I miss something?
Upvotes: 0
Views: 95
Reputation: 181
Where is the DataAdapter you are using to fill the DataSet? You need to do something like this.
try
{
SqlDataAdapter1.Update(Dataset1.Tables["Table1"]);
}
catch (Exception e)
{
// Error during Update, add code to locate error, reconcile
// and try to update again.
}
See this link for more information https://msdn.microsoft.com/en-us/library/xzb1zw3x.aspx
I would also recommend that you separate out the code that writes to the database from the code that affects the UI. Look here for what an MVC (Model View Controller) pattern is. http://blog.codinghorror.com/understanding-model-view-controller/ and look here for a beginner tutorial on ASP.NET MVC http://www.w3schools.com/aspnet/mvc_app.asp
Upvotes: 1
Reputation: 397
I don't know your setup but ds.AcceptChanges(); don't write changes to underlying database. If you've setup it to work like that you have to show your code but AcceptChanges alone doesn't enough. MSDN:When you call AcceptChanges on the DataSet, any DataRow objects still in edit-mode end their edits successfully. The RowState property of each DataRow also changes; Added and Modified rows become Unchanged, and Deleted rows are removed.
Upvotes: 0
Reputation: 6452
ds.AcceptChanges();
do not update, but sign to ignore the changes. for update you need use a DataAdapter with commands for update and use the Update()
method:
DataAdapter.Update(ds);
Upvotes: 1