簡體   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