繁体   English   中英

ASP.NET 2.0 C#插入数据库不起作用

[英]ASP.NET 2.0 C# Insert to database not working

我的Web应用程序中有一个asp.net向导控件,我在后面的代码中调用存储过程以将数据插入数据库。 在SSMS中执行proc可以正常工作,我之前已经使此块工作了一次并进行了更改(不幸的是,我不记得我进行了哪些更改)。 我的问题是,当单击下一个按钮时,不会引发任何错误,并且也不会将任何数据写入数据库。 我已经通过将异常添加到cnx2 try块中进行了测试,并抛出了异常,因此我知道代码正在执行到我想要的位置,但仍未插入。 任何帮助表示赞赏,谢谢。 如果有我可以补充的任何信息,请帮助我

     protected void onNextButtonClick(object sender, EventArgs e)
    {
        if (Wizard1.ActiveStepIndex.Equals(1))
        {
            page1Submit();
        }
        else if (Wizard1.ActiveStepIndex.Equals(2))
        {
            page2Submit();
        }
        else if (Wizard1.ActiveStepIndex.Equals(3))
        {
            page3Submit();
        }
        else if (Wizard1.ActiveStepIndex.Equals(8))
        {
            page8submit();
        }
    }
    protected void page1Submit()
    {
        string hispanic;
        if (cbIsHispanic.Checked)
        {
            hispanic = "1";
        }
        else
        {
            hispanic = "0";
        }
        bool newReport = true;
        SqlConnection cnx = new SqlConnection(server);
        SqlCommand cmd = new SqlCommand("[ReportCheckExists]", cnx);
        cmd.CommandType = CommandType.StoredProcedure;
        cmd.Parameters.AddWithValue("@reportNumber", dReportNumber.Text.ToString());
        try
        {
            cnx.Open();
            int rowCount = Convert.ToInt32(cmd.ExecuteScalar());
            cnx.Close();
            if (rowCount > 0)
            {
                newReport = false;
            }

        }
        catch (Exception ex)
        {
            throw new Exception("Error executing MyProcedureName.", ex);
        }
        if (newReport)
        {
            SqlConnection cnx2 = new SqlConnection(server);
            SqlCommand cmd2 = new SqlCommand("[NewReport]", cnx2);
            cmd2.CommandType = CommandType.StoredProcedure;
            cmd2.Parameters.AddWithValue("@reportNumber", dReportNumber.Text.ToString());
            try
            {
                cnx.Open();
                cmd.ExecuteNonQuery();
                cnx.Close();

            }
            catch (Exception ex)
            {
                throw new Exception("Error executing MyProcedureName.", ex);
            }  
        }
        else
        {
            string strJavaScript = "<script language = javascript type=text/Javascript> alert('That report number already exists. If you need to modify a report select it from the main menu or enter the report number again.')</script>";
            this.Page.RegisterClientScriptBlock("Key4", strJavaScript);
            Wizard1.ActiveStepIndex = 0;

        }
    }

可能是因为您正在执行cmd命令,而不是第二个try catch块中的cmd2

暂无
暂无

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

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