0

I've been searching all day to solve my problem, but I can't find a clear answer, or I just don't understand them

I use Visual Studio to create a Website for searching Events. My problem is that I can't save the information a user inputs in my create user form, in my Database.

My button looks like this:

 <button type="submit" value="Submit" onclick="Submit">Opret Bruger</button>

It calls a method called "Submit", it looks like this (with an opening to my database:

public static MySqlConnection GetConnection(string host, string user, string pwd, string db)
    {
        string conStr = string.Format("server={0};uid={1};pwd={2};database={3}", host, user, pwd, db);
        var con = new MySqlConnection();
        con.ConnectionString = conStr;
        con.Open();
        return con;
    }

    public static MySqlConnection GetDefaultConnection()
    {
        return GetConnection("localhost", "root", "", "EventCrush");
    }

    public static void Submit(string Fornavn, string Efternavn, string Email, int Telfonnummer, string Brugernavn, byte Avatar, string Kodeord, DateTime Fødselsdag)
    {
        var con = GetDefaultConnection();
        MySqlCommand cmd = new MySqlCommand();
        cmd.Connection = con;
        cmd.CommandText = "insert into private_user values (@Username, @PW, @Birthdate, @FirstName, @LastName, @Eamail, @PhoneNumber)";
        cmd.Prepare();
        cmd.Parameters.AddWithValue("@Username", Brugernavn);
        cmd.Parameters.AddWithValue("@PW", Kodeord);
        cmd.Parameters.AddWithValue("@Birthdate", Fødselsdag);
        cmd.Parameters.AddWithValue("@FirstName", Fornavn);
        cmd.Parameters.AddWithValue("@LastName", Efternavn);
        cmd.Parameters.AddWithValue("@Email", Email);
        cmd.Parameters.AddWithValue("@PhoneNumber", Telfonnummer);
        cmd.ExecuteNonQuery();
        if (con != null) { con.Close(); }
    }

I can't seem to get it to save anything in my Database, can anyone help me detect my problem?

3
  • Are you getting any error? Commented Apr 30, 2015 at 16:00
  • In Visual Studio, can you insert a breakpoint on the line var con = GetDefaultConnection(); then when you debug it, you can step through the code line by line and see what happens. Commented Apr 30, 2015 at 16:01
  • No errors, it just dos not save the data to the database Commented Apr 30, 2015 at 16:55

2 Answers 2

1

This is not how MVC works. Seems like you're trying to use this as if it was a Web Form. In MVC there's a very clear distinction between server-side and client-side. Namely, your Submit function only exists server-side, where as your HTML button that's attempting to call it only exists client-side. By the time it's rendered, the Submit method is no longer around.

You need to do an actual form post to some action that will save this to the database, or use AJAX to post to an action without causing a page reload. Either way, you need an actual action to act as an endpoint for your form post.

Sign up to request clarification or add additional context in comments.

1 Comment

<script> $.ajax({ type: "POST", url: "WHAT TO PUT HERE?", data: $('#PrivateUser').serialize(), datatype: "html", success: function (data) { $('#result').html(data); } }); </script> Would something like this do the trick?
0
<script>
            $.ajax({
                type: "POST",
                url: '@Url.Action("Submit", "CreateController")',
                data: $('#PrivateUser').serialize(),
                datatype: "html",
                success: $("Submit1").click(function (data) {
                    $('#result').html(data)
                })
                });
        </script>

Will this do the trick???

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.