[vsto-c#] Mastershape with embedded db connection and record selection

Started by farwul, November 13, 2012, 12:53:17 AM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

farwul

Hello,
I'm working on a Visio Add-in for Visio 2003 and I'm very new to .Net programming.
I made, in Visio, a master shape whose custom properties are linked to specific table from a database.
The users can then drag & drop this master shape to a page and will be asked to choose a specific row (after having entered login/pwd required by the database) in order to bind the data to the custom properties.

Now, I'm working on automating it by code (C#) :
1) Read a table from a database
2) For each row, create a new page in the visio document
3) In each page, drop the master shape (the one having custom properties bound to a database)
4) Initialize the new shape by mapping its "primary key" to dataset coming from the database

I have completed the 1st, 2nd and 3rd part but I fail to see how to complete the last step.

My master shape (from myStencilKit.vss) is named "ItemDetails" and has three custom properties : id (unique identifier), property1, property2.

Here is the code I use :

Visio.Page pagObj;
Visio.Application app;
//Stencil document that contains master
Visio.Document stnObj;

//Master to drop
Visio.Master mastObj;

Visio.Shape shpObj;

string documentName = "myDocument.vsd";
string itemDetailPageName;

SQLConnection myConnection;
string sqlQuery;
sqlQuery = " Select id, property1, property2 " +
           " FROM Items ";
myConnection = new SQLConnection();
SqlCommand myCommand;
SqlDataReader myReader;
myReader = null;

app = Dispatch("Visio.Application")
 
myConnection.Open();

myCommand = new SqlCommand(sqlQuery, myConnection);
myReader = myCommand.ExecuteReader();

while (myReader.Read())
{
    toolDetailPageName = "Detail" + myReader["id"].ToString();
   
    //Add one page to the document for each item found in the datareader
    pagObj = DropMaster.addPageToDocument(app, documentName, toolDetailPageName);


    //Get the stencil from the Documents collection
    stnObj =  app.Documents.Add("myStencilKit.vss");
    pagObj = app.ActivePage
    //Set the master
    mastObj = stnObj.Masters["ItemDetails"];
   
    //Add the shape to the page.
    shpObj = pagObj.Drop(mastObj, 4.25, 5.5);
   
    //And now, how to bind data to the shpObj using its predefined datalink ?....
}

myConnection.Close();



I can't find any method on the shape object that I can access to set a specific row.


Thanks in advance for any help anyone is able to provide.

aledlund

I'd suggest checking out the Visio SDK which has examples on reading custom properties.
al

farwul

My problem is not to access/edit custom properties but rather "trigger" the DB wizard when I change the "primary key" custom property.
I can loop over all the custom properties and updating each "one per one" but I was wondering if there was a way to reproduce the "select record action" that exists when you have mapped a shape with a dataset in the Visio interface.

aledlund

For that I would suggest you check out the SDK Event Filter Demo. By using AddAdvise and Event Filters you can watch for when the shape properties are changed and which shape has been altered.
Al

farwul

Thanks for the tips.
I found how to trigger to the DB wizard to select a row. I think I am close to what I want to achieve ;-)

Here is the code I used :

                    if (shpObj.get_CellExists("User.ODBCConnection", (short) Visio.VisExistsFlags.visExistsAnywhere) != 0)
                    {
                        if (shpObj.get_SectionExists((short) Visio.VisSectionIndices.visSectionAction,(short) Visio.VisExistsFlags.visExistsAnywhere) != 0 )
                        {
                            for (int jRow = 0; jRow <= shpObj.get_RowCount((short)Visio.VisSectionIndices.visSectionAction) - 1; ++jRow)
                            {
                                if (shpObj.get_CellsSRC((short)Visio.VisSectionIndices.visSectionAction, (short) jRow, (short)Visio.VisCellIndices.visActionAction).Formula.ToString() == "RUNADDON(\"DBS\")" )
                                {
                                    shpObj.get_CellsSRC((short)Visio.VisSectionIndices.visSectionAction, (short)jRow, (short)Visio.VisCellIndices.visActionAction).Trigger();
                                }

                            }
                        }
                    }



The DB wizard is launched with addon named "DBS".
I would like to use the "=RUNADDONWARGS()" function to trigger "DBS" with parameters by code. Is there a way to find what are parameters required for a Visio Addon ?