Question

On the first step of the asp:Wizard I have a login using DirectoryServices to authenticate. But then I want to take the UserID, Date, and the SCOPE_IDENTITY() and insert it into a table. Here is what I have tried. When I hit next the information is not inserted but the AD function is checked properly. I am not sure what i am doing wrong

protected void OnActiveStepChanged(object sender, EventArgs e)
    {
        //check for the employee in AD
        string Domain = "mydomain.local";
        string EmployeeID = txtEmpID.Text;
        string Password = txtPassword.Text;
        string ADStatus = null;

        // If the ActiveStep is changing to Step2, check to see whether the 
        // user authenticated the AD Login Process.  If it is, skip to the Step2 step.
        if (Wizard1.ActiveStepIndex == Wizard1.WizardSteps.IndexOf(this.WizardStep2))
        {
            if (AuthenticateActiveDirectory(Domain, EmployeeID, Password) == true)
            {
                //If success ...
                ADStatus = "Success";
                Session["SessionADStatus"] = ADStatus;


                string strDepartment = ddlDepartment.SelectedValue;
                SqlConnection conn1 = new SqlConnection(ConfigurationManager.ConnectionStrings["myconnection"].ConnectionString);
                SqlCommand cmd1 = new SqlCommand("INSERT INTO [pharm_OrderID](UserID, RequestType, CreateDate) values (@UserID, @RequestType, @CreateDate);", conn1);
                cmd1.CommandType = CommandType.Text;
                conn1.Open();

                string strUserID = txtEmpID.Text;
                cmd1.Parameters.Add("@UserID", SqlDbType.NVarChar, 50);
                cmd1.Parameters["@UserID"].Value = strUserID;

                string strRequestType = ddlReturnType.SelectedValue;
                cmd1.Parameters.Add("@ReturnType", SqlDbType.NVarChar, 50);
                cmd1.Parameters["@ReturnType"].Value = strRequestType;

                string strCreateDate = lblOrderAttemptTime.Text;
                cmd1.Parameters.Add("@CreateDate", SqlDbType.NVarChar, 50);
                cmd1.Parameters["@CreateDate"].Value = strCreateDate;

                conn1.Dispose();
                cmd1.Dispose();
                Wizard1.ActiveStepIndex = Wizard1.WizardSteps.IndexOf(this.WizardStep2);

            }
            else
            {
                ADStatus = "Failure";
                Session["SessionADStatus"] = ADStatus;
                lblADError.Visible = true;
                lblADError.Text = "Unable to authenticate Employee ID or Password.";
                Wizard1.ActiveStepIndex = Wizard1.WizardSteps.IndexOf(this.WizardStep1);
            }


        }

    }
Was it helpful?

Solution

I am not an expert of AD, but a command needs to be executed to produce any result.

Try to add

 cmd1.ExecuteNonQuery(); 

before disposing the connection and the command

using(SqlConnection conn1 = new SqlConnection(........))
using(SqlCommand cmd1 = new SqlCommand("INSERT INTO [pharm_OrderID]" + 
                                      "(UserID, RequestType, CreateDate) " + 
                                      "values (@UserID, @RequestType, @CreateDate);", conn1))
{
    conn1.Open();
    string strUserID = txtEmpID.Text;
    cmd1.Parameters.Add("@UserID", SqlDbType.NVarChar, 50);
    cmd1.Parameters["@UserID"].Value = strUserID;

    string strRequestType = ddlReturnType.SelectedValue;
    cmd1.Parameters.Add("@ReturnType", SqlDbType.NVarChar, 50);
    cmd1.Parameters["@ReturnType"].Value = strRequestType;

    string strCreateDate = lblOrderAttemptTime.Text;
    cmd1.Parameters.Add("@CreateDate", SqlDbType.NVarChar, 50);
    cmd1.Parameters["@CreateDate"].Value = strCreateDate;

    cmd1.ExecuteNonQuery();
}
Wizard1.ActiveStepIndex = Wizard1.WizardSteps.IndexOf(this.WizardStep2);
...

Added also the using statement to close the connection and to dispose both command and connection.
You should always use this pattern to correctly close the connection also in case of exceptions thrown inside the using block

OTHER TIPS

3 Things

  1. I agree with steve's post. You should have using statements when you can over objects that need to be disposed of. That way, you don't really need to remember to do it. Its handled for you.
  2. the sqlcommand object has an open connection of its own. Maybe using it will help resolve the issue?
  3. You really should be using the transaction object as well. Transactions are basically fail safes when you enter multiple pieces of data. For example. Say your wizard in this case has 3 steps, and step 2 fails. You probably wouldn't want step 1 to be left over. With a transaction, you can rollback your changes on error.

For section 2:

cmd1.Connection.Open();

instead of

conn1.Open();

For Section 3:

    SqlTransaction transaction;

    // Start a local transaction.
    transaction = conn1.BeginTransaction("TheTransaction");
    cmd1.Transaction = transaction;

Then just before you dispose of your command

transaction.Commit();

In an error block (when you write a TryCatch, or if you detect some sort of error), you use

transaction.Rollback();

As a note, you DO set the success status at the beginning of your sql insert. You should really be putting that after the command is successful...

Edit: In your code. You reference requesttype in your insert

new SqlCommand("INSERT INTO [pharm_OrderID](...RequestType...) values (...@RequestType...

Then later on, you use something called returntype

string strRequestType = ddlReturnType.SelectedValue;
cmd1.Parameters.Add("@ReturnType", SqlDbType.NVarChar, 50);
cmd1.Parameters["@ReturnType"].Value = strRequestType;

Which one is the correct one for the database. This is most likely your issue

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top