0

I have an asp.net wizard control in my web application I am calling a stored procedure from code behind to insert data into database. Executing the proc in SSMS works fine and I had gotten this block to work once before and made changes (i unfortunately cannot remember which changes i made). My problem is that when the next button is clicked no errors are thrown and also no data is written to the database. I have tested by adding exception into the cnx2 try block and the exceptions were thrown so I know the code is executing to the place I want it to but it is still not inserting. Any help is appreciated Thank you. And please if there is any information i can add that may help let me know

     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;

        }
    }
3
  • 1
    "and the exceptions were thrown". If its throwing exception on cmd.ExectueNonQuery(), then surely it is not going to insert anything. What is the exception you are getting Commented May 9, 2012 at 17:05
  • 1
    Absolutely not related, but replace everything from string hispanic to bool newReport with string hispanic = cbIsHispanic.Checked ? "1" : "0";. Easy to read and saves you a lot of lines. Commented May 9, 2012 at 17:09
  • 1
    You should add using statements or finally blocks to avoid unclosed connections if an exception occurs. Commented May 9, 2012 at 17:10

1 Answer 1

4

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

Sign up to request clarification or add additional context in comments.

2 Comments

after a few minutes sitting with my boss and then me bludgeoning myself with a hammer for being stupid found it thanks for the answer.
lol, np. Do take into consideration to use using statements on your connections and readers btw, they are very useful, and they look pretty

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.