0

我的 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;

        }
    }
4

1 回答 1

4

It's probably because you are executing the cmd command, and not cmd2 in your second try catch block

于 2012-05-09T17:07:01.227 回答