0

I am trying to make a form send info to a database. The code for my button is below. It seems to connect to the database, but all the values that are added are null. Am I missing something? I've rechecked the ids for the fields in the form (TextBoxUN,TextBoxEmail..) and they all match.

Thanks.

protected void Button1_Click(object sender, EventArgs e)
    {
        try
        {

            MySqlConnection conn = new MySqlConnection(ConfigurationManager.ConnectionStrings["apstestConnectionString"].ConnectionString);//string used in sql datasource
            conn.Open();
            string insertQuery = "insert into asptable (username,email,password,country) values (@Uname,@email,@password,@country)";
            MySqlCommand com = new MySqlCommand(insertQuery, conn);

            com.Parameters.AddWithValue("@Uname,", TextBoxUN.Text);
            com.Parameters.AddWithValue("@email,", TextBoxEmail.Text);
            com.Parameters.AddWithValue("@password,",TextBoxPass.Text);
            com.Parameters.AddWithValue("@country,",DropDownListCountry.SelectedItem.ToString());

            com.ExecuteNonQuery();

            conn.Close(); 
            Response.Redirect("Manager.aspx");

        }
        catch(Exception ex){
            Response.Write("Error:"+ex.ToString());
        }
    }
2
  • 1
    why is there a comma after every parameter? Commented Jun 20, 2015 at 22:24
  • I gave the first answer can you mark as accepted? Commented Jun 20, 2015 at 22:31

3 Answers 3

2

Remove "," chars from parameters

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

Comments

1

Remove commas from the end of parameter names when calling Parameters.AddWithValue and see if it helps. So instead of

AddWithValue("@Uname,",...)

write

AddWithValue("@Uname",...)

And the same for other parameters.

Comments

0

Instead of com.Parameters.AddWithValue("@Uname,", TextBoxUN.Text); remove the , after the parameter name and write com.Parameters.AddWithValue("@Uname", TextBoxUN.Text); without comma. Do this for all parameters.

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.