-1

I'm really new to C#/WPF. I try to program my first app. So I added a local database (.mdf file) with a DataSet and TableAdapter. I also created a table (User).

public static int CreateNewId()
        {
            using (var todoDataSet = new ToDoDataSet())
            {
                using (var tableAdapter = new UserTableAdapter())
                {
                    var userid = (int) tableAdapter.GetLastId();
                    userid = userid + 1;
                    return userid;
                }
            }
        }

With this function I get the last Id of my table – this works fine. But if I want to insert some new data into the table it doesn't. There isn't any error message, but the new data won't show in the table.

public static void CreateUser(User user)
        {

            using (var todoDataSet = new ToDoDataSet())
            {
                using (var userTableAdapter = new UserTableAdapter())
                {
                    try
                    {
                        userTableAdapter.Insert(user.ID, user.Loginname, user.Firstname, user.Lastname, user.Email, user.Password, user.PicUri);
                        userTableAdapter.Update(todoDataSet.User);
                        MessageBox.Show("Update successful");
                    }
                    catch (System.Exception ex)
                    {
                        MessageBox.Show("Update failed");
                    }

                }
            }
        }

Does someone know what I did wrong? Or is something missing?

Thanks for your help!

EDIT: Code for calling CreateUser():

private void RegisterUser(object sender, RoutedEventArgs e)
        {
            int userId = Mysql.CreateNewId();
            Console.WriteLine(rm.FirstName);
            User loggedUser = new User(userId, rm.Loginname, rm.Email, rm.FirstName, rm.LastName, rm.Password);
            Console.WriteLine(loggedUser.ID);
            Console.WriteLine(loggedUser.Loginname);
            Mysql.CreateUser(loggedUser);
        }
netdev
  • 1
  • 3

1 Answers1

0

I think you may call a method todoDataSet.User.Add[tablename]Row DataTable Add mehtod instead of userTableAdapter.Insert(user.ID, user.Loginname, user.Firstname, user.Lastname, user.Email, user.Password, user.PicUri);

then you call userTableAdapter.Update(todoDataSet.User);,You will save data in DB.

Because the method userTableAdapter.Update is from DbDataAdapter

D-Shih
  • 44,943
  • 6
  • 31
  • 51