Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/268.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在asp.net c中将数据插入SQL Server数据库#_C#_Asp.net - Fatal编程技术网

C# 在asp.net c中将数据插入SQL Server数据库#

C# 在asp.net c中将数据插入SQL Server数据库#,c#,asp.net,C#,Asp.net,我使用以下代码将数据插入数据库。它既不给出错误,也不向数据库添加数据。我遗漏了什么吗 列分别为univ_regno和email的数据类型为nvarchar(50) protected void btnSubmit_Click(object sender, EventArgs e) { if (chkaccept.Checked) { try { con = new SqlConnection(ConfigurationMan

我使用以下代码将数据插入数据库。它既不给出错误,也不向数据库添加数据。我遗漏了什么吗

列分别为
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);
   }
}
添加(“@univ_regno”,txtuniv.Text)

这是否会将值添加到参数中?它调用Parameters.Add(字符串名,SqlDbType)重载

请参阅此链接:

尝试将参数添加为

cmd.Parameters.Add("@univ_regno", SqlDbType.VarChar).Value = txtuniv.Text;

这不会影响您的特定问题,但您没有正确关闭连接。ExecuteOnQuery()返回什么结果?我使用的是con.Close();这还不够吗@Joelcoehoorni猜测您的javascript报告中存在错误。在
catch
中设置断点,以查看sql调用的实际情况…连接必须始终在finally块中关闭,或者你可以在你的数据库上创建一个拒绝服务的情况。你能告诉我有没有强制按顺序声明标量变量?因为我正在向univ_regno和email添加数据,并按顺序添加参数。我可以先发电子邮件然后再发大学注册号吗?我想这不会是个问题。你尝试过改变你添加参数的方式吗?使用断点并逐行调试它可能会有帮助