简体   繁体   English

在数据库中显示为空的行-ASP.NET C#

[英]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. 我已经检查了表单(TextBoxUN,TextBoxEmail ..)中的字段的ID,它们都匹配。

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. 调用Parameters.AddWithValue时,请从参数名称的末尾删除逗号,并查看是否有帮助。 So instead of 所以代替

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

write

AddWithValue("@Uname",...)

And the same for other parameters. 与其他参数相同。

Instead of com.Parameters.AddWithValue("@Uname,", TextBoxUN.Text); 而不是com.Parameters.AddWithValue("@Uname,", TextBoxUN.Text); remove the , after the parameter name and write com.Parameters.AddWithValue("@Uname", TextBoxUN.Text); 除去,参数名称和后写com.Parameters.AddWithValue("@Uname", TextBoxUN.Text); without comma. 没有逗号。 Do this for all parameters. 对所有参数执行此操作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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