Click here to Skip to main content
15,909,897 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I get a double entry in my database when I signup a new user. I enter the user name and password that I made up and click submit. That part works but when i check the database I see the username twice. Why? What did I do wrong?

C#
protected void Submit_Click(object sender, EventArgs e)
    {
        SqlConnection con = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["HotConnectionString"].ConnectionString);
        con.Open();

        string cmdStr = "Select INST_ID, accessLevel, EmailAddress from Table1 where EmailAddress='" + TextBoxEA.Text + "'";
        string cmdStr2 = "Select INST_ID, accessLevel, EmailAddress from Table2 where EmailAddress='" + TextBoxEA.Text + "'";
        string insCmd = "Insert into Table22 (EmailAddress, Password, INST_ID, accessLevel) values (@EmailAddress, @Password, @INST_ID, @accessLevel)";
        string insCmd2 = "Insert into Table22 (EmailAddress, Password, INST_ID, accessLevel) values (@EmailAddress, @Password, @INST_ID, @accessLevel)";

        SqlCommand insertUser = new SqlCommand(insCmd, con);
        SqlCommand insertUser2 = new SqlCommand(insCmd2, con);

        insertUser.Parameters.AddWithValue("@EmailAddress", TextBoxEA.Text);
        insertUser.Parameters.AddWithValue("@Password", TextBoxPW.Text);
        insertUser.Parameters.AddWithValue("@INST_ID", TextBoxINST_ID.Text);
        insertUser.Parameters.AddWithValue("@accessLevel", TextBoxaccessLevel.Text);

        insertUser2.Parameters.AddWithValue("@EmailAddress", TextBoxEA.Text);
        insertUser2.Parameters.AddWithValue("@Password", TextBoxPW.Text);
        insertUser2.Parameters.AddWithValue("@INST_ID", TextBoxINST_ID.Text);
        insertUser2.Parameters.AddWithValue("@accessLevel", TextBoxaccessLevel.Text);

        try
        {
            insertUser.ExecuteScalar();
            insertUser2.ExecuteScalar();
            con.Close();
            Response.Redirect("Login.aspx");
        }
        catch (Exception er)
        {
            
        }
    }
Posted
Updated 6-Jan-14 5:47am
v2

Well, why do you think? :laugh:
You are setting up two identical INSERT commands, and executing them on the same SqlConnection one after the other. So...did you expect to get different values inserted the second time?
 
Share this answer
 
Comments
Ron Beyer 6-Jan-14 11:54am    
5 for seeing the forest for the trees :)
Isn't it obvious?

You are making two insert commands that are EXACTLY the same, then you are executing them both. Why do you have it in there twice? insertUser1 and insertUser2?
 
Share this answer
 
Comments
Computer Wiz99 6-Jan-14 11:55am    
Hi Ron Beyer. Well, I used this code before and it worked. Just don't know why it is doing it like this. Let me change something and I will show you what I have. Ok.
Computer Wiz99 6-Jan-14 11:55am    
Sorry about that.
After looking at it and making some changes from my other code I used before, I got it to work. I knew I had it twice but I left out the other connection I had in my other code. Here is my updated code:

C#
protected void Submit_Click(object sender, EventArgs e)
    {
        SqlConnection con = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["HotConnectionString"].ConnectionString);
        con.Open();
 
        string cmdStr = "Select INST_ID, accessLevel, EmailAddress from Table1 where EmailAddress='" + TextBoxEA.Text + "'";
        string cmdStr2 = "Select INST_ID, accessLevel, EmailAddress from Table2 where EmailAddress='" + TextBoxEA.Text + "'";
        string insCmd = "Insert into Table22 (EmailAddress, Password, INST_ID, accessLevel) values (@EmailAddress, @Password, @INST_ID, @accessLevel)";
        
        SqlCommand insertUser = new SqlCommand(insCmd, con);
 
        insertUser.Parameters.AddWithValue("@EmailAddress", TextBoxEA.Text);
        insertUser.Parameters.AddWithValue("@Password", TextBoxPW.Text);
        insertUser.Parameters.AddWithValue("@INST_ID", TextBoxINST_ID.Text);
        insertUser.Parameters.AddWithValue("@accessLevel", TextBoxaccessLevel.Text);
 
        
 
        try
        {
            insertUser.ExecuteScalar();
            con.Close();
            Response.Redirect("Login.aspx");
        }
        catch (Exception er)
        {
            
        }
    }
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900