简体   繁体   中英

2 users registration in one form C# winforms

I want to do a game with player1 Vs. player2 and need to register them to DB first. I have one form with panel for each user's details and one submit button, so when the two users fill their details they submit them in the same form. and I have one Users table in DB, so all registered users should be there. but when i add it to the DB table, i get exception that the @username already exist (and the same for all parameters in table).

is it possible to register two users in one form to one table ?

this is the code for the submit button. it works for one user..

private void submitBtn_Click(object sender, EventArgs e)
{
    if (registerValidation())
    {
        string conStr = ConfigurationManager.ConnectionStrings["BackgammonGame"].ConnectionString;
        SqlConnection con = new SqlConnection(conStr);
        SqlCommand cmd = new SqlCommand();
        cmd.Connection = con;
        con.Open();

        cmd.CommandText = ("INSERT INTO Users (userName, firstName, lastName, address, addNum, city, phone, email) VALUES (@userName, @firstName, @lastName, @address, @addNum, @city, @phone, @email)");

        cmd.Parameters.Add("@userName", SqlDbType.NVarChar).Value = userNmTxt.Text;
        cmd.Parameters.Add("@firstName", SqlDbType.NVarChar).Value = firstNmTxt.Text;
        cmd.Parameters.Add("@lastName", SqlDbType.NVarChar).Value = lastNmTxt.Text;
        cmd.Parameters.Add("@address", SqlDbType.NVarChar).Value = streetTxt.Text;
        cmd.Parameters.Add("@addNum", SqlDbType.Int).Value = (string.IsNullOrWhiteSpace(stNumTxt.Text)) ? 0 : Int32.Parse(stNumTxt.Text);
        cmd.Parameters.Add("@city", SqlDbType.NVarChar).Value = cityTxt.Text;
        cmd.Parameters.Add("@phone", SqlDbType.Int).Value = (string.IsNullOrWhiteSpace(phoneNumTxt.Text)) ? 0 : Int32.Parse(phoneNumTxt.Text);
        cmd.Parameters.Add("@email", SqlDbType.NVarChar).Value = mailTxt.Text;

        cmd.ExecuteNonQuery();
        MessageBox.Show("User successfully created!");
        con.Close();
        Close();
        Form1.ActiveForm.Close();
    }
}

I suspect that the problem you are having is that your button click event is just using the same textboxes which relate to the first player and then reusing these for the second player (which obviously still points to player one).

Your best idea would be to either make a method that will take in some parameters or create a user control for your panels that you could use to add to db.. You will still need checking for users that exist though.

Solution One

private void submitBtn_Click(object sender, EventArgs e)
{
    AddPlayerToDb("name","add"....);
    AddPlayerToDb("name2","add2"....);

//Inside method
cmd.Parameters.Add("@userName", SqlDbType.NVarChar).Value = param1;

Solution two

private void submitBtn_Click(object sender, EventArgs e)
{
    userControl1.AddPlayerToDb(connection);
    userControl2.AddPlayerToDb(connection);

Note I'm not familiar with SQL too much so I don't know if its valid to pass a connection through or if you need a new connection

I think you should check if the user exists in the datatable before saving a new one. Than you can update the user data, overwrite it or ignor the new data.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM