简体   繁体   中英

Rows showing up as null in database - asp.net c#

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());
        }
    }

从参数中删除“,”字符

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.

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.

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