Search code examples
c#access-violationhp-quality-center

C# Inserting a new Requirement in HP Quality Center - AccessViolationException


Trying to create a prototype application that will post a new Requirement to HPQC 11.

I've managed to get a solid connection but when I attempt to add the blank requirement I get an AccessViolationException.

TDConnectionClass td = HPQC_Connect(); //Open a connection
ReqFactory myReqFactory = (ReqFactory)td.ReqFactory; //Start up the Requirments Factory.
Req myReq = (Req)myReqFactory.AddItem(DBNull.Value); //Create a new blank requirement (AccessViolationException)
myReq.Name = "New Requirement"; //Populate Name
myReq.TypeId = "1"; // Populate Type: 0=Business, 1=Folder, 2=Functional, 3=Group, 4=Testing
myReq.ParentId = 0; // Populate Parent ID
myReq.Post(); // Submit

Any ideas? I'm fairly new to C# and coding in general, so it's probably best to assume I know nothing.


Solution

  • After some significant working through the isse the following code works correctly:

    private void HPQC_Req_Create_Click()
        {
            TDConnection td = null;
            try
            {
                td = new TDConnection();
                td.InitConnectionEx("server");
                td.Login(HPQCUIDTextbox.Text.ToString(), HPQCPassTextbox.Text.ToString());
                Console.WriteLine(HPQCPassTextbox.Text.ToString());
                td.Connect("DEFAULT", "Test_Automation_Playground");
    
                bool check = td.LoggedIn;
                if (check == true)
                {
                    Console.WriteLine("Connected.");
                    HPQCStatus.Text = "Connected.";
                }
    
                ReqFactory myReqFactory = (ReqFactory)td.ReqFactory;
                Req myReq = (Req)myReqFactory.AddItem(-1); //Error Here
                myReq.Name = "New Requirement 1";
                myReq.TypeId = "1"; // 0=Business, 1=Folder, 2=Functional, 3=group, 4=testing 
                myReq.ParentId = 0;
                myReq.Post();
    
                Console.WriteLine("Requirement Created.");
                HPQCStatus.Text = "Requirement Created.";
    
                try
                {
                    td.Logout();
                    td.Disconnect();
                    td = null;
                }
                catch
                { }
            }
            catch (Exception ex)
            {
                Console.WriteLine("[Error] " + ex);
                try
                {
                    td.Logout();
                    td.Disconnect();
                    td = null;
                }
                catch
                { }
            }
    

    This code requires that the Server be patched to QC 11 Patch 9 (Build 11.0.0.7274) in order to work. Previous versions cause errors, most notably the error in the question.