0

我正在使用以下代码将数据插入数据库。它既不会给出错误,也不会将数据添加到数据库中。我错过了什么吗?

列分别是和univ_regno具有email数据类型nvarchar(50)

protected void btnSubmit_Click(object sender, EventArgs e)
{
    if (chkaccept.Checked)
    {
        try
        {
            con = new SqlConnection(ConfigurationManager.ConnectionStrings["SQL Connection String"].ConnectionString);
            con.Open();
            com = new SqlCommand("INSERT INTO stdtable ([univ_regno],[email]) values(@univ_regno,@email)", con);

            com.Parameters.Add("@univ_regno", txtuniv.Text);
            com.Parameters.Add("@email", txtemail.Text);

            com.ExecuteNonQuery();
            con.Close();

            /*show javascript message */
            Type cstype = this.GetType();
            ClientScriptManager cs = Page.ClientScript;
            String cstext = "alert('Record Added Successfully');";
            cs.RegisterStartupScript(cstype, "PopupScript", cstext, true);
        }
        catch (System.Exception err)
        {
           Type cstype = this.GetType();
           ClientScriptManager cs = Page.ClientScript;
           String cstext = "alert('"+  err.Message.ToString() +" ');";
           cs.RegisterStartupScript(cstype, "PopupScript", cstext, true);
        }
   }
   else
   {
       Type cstype = this.GetType();
       ClientScriptManager cs = Page.ClientScript;
       String cstext = "alert('Not checked ');";
       cs.RegisterStartupScript(cstype, "PopupScript", cstext, true);
   }
}
4

1 回答 1

1

在此处输入图像描述 com.Parameters.Add("@univ_regno", txtuniv.Text);

这会将值添加到参数中吗?它调用 Parameters.Add(string name, SqlDbType type) 重载

参考这个链接:

Parameters.Add 和 Parameters.AddWithValue 之间的区别

尝试将参数添加为

cmd.Parameters.Add("@univ_regno", SqlDbType.VarChar).Value = txtuniv.Text;
于 2012-10-22T17:57:13.687 回答