AddFromTemplate in VS2005 (typed DataSet)

I'm making an add-in, and one task is to generate a typed DataSet from an SQL2000-table, and then add the DataSet to a Project in the solution.

Everything worked fine in VS2003, but now in VS2005 (beta 2 as well as June CTP) I get the error below when calling AddFromTemplate:

Microsoft.VSDesigner.Common.InternalException crossed a native/managed boundary
  Message="Unexpected error."
  Source="Microsoft.VSDesigner"
  StackTrace:
       at Microsoft.VSDesigner.CodeGenerator.TypedDataSourceGenerator.DataSourceGeneratorWrapper.GenerateCode(String inputFileName, String inputFileContent)
       at Microsoft.VSDesigner.CodeGenerator.BaseCodeGenerator.Generate(String wszInputFilePath, String bstrInputFileContents, String wszDefaultNamespace, IntPtr& pbstrOutputFileContents, Int32& pbstrOutputFileContentSize, IVsGeneratorProgress pGenerateProgress)

The DataSet is generated this way:
1. Create an untyped ds and fill it from SQL Server (select top 0 * frommyTable)
2. WriteXMLSchema to disk
3. Run xsd.exe on the xml and make an xsd-file
4. Add the xsd to the project using AddFromTemplate
5. The dataset is added, but the exception is thrown

Any clues
Regards
Roger Jakobsson
EDP Consult AB



Answer this question

AddFromTemplate in VS2005 (typed DataSet)

  • mfeigin

    < xml version="1.0" encoding="utf-8" >
    <xs:schema id="TestRJ" xmlns="" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
      <xs:element name="TestRJ" msdata:IsDataSet="true" msdata:UseCurrentLocale="true">
        <xs:complexType>
          <xs:choice minOccurs="0" maxOccurs="unbounded">
            <xs:element name="tbFuTaxa">
              <xs:complexType>
                <xs:sequence>
                  <xs:element name="intRecnum" msdata:ReadOnly="true" msdata:AutoIncrement="true" type="xs:int" />
                  <xs:element name="strTaxekod">
                    <xs:simpleType>
                      <xs:restriction base="xs:string">
                        <xs:maxLength value="10" />
                      </xs:restriction>
                    </xs:simpleType>
                  </xs:element>
                  <xs:element name="strTaxebenamning">
                    <xs:simpleType>
                      <xs:restriction base="xs:string">
                        <xs:maxLength value="60" />
                      </xs:restriction>
                    </xs:simpleType>
                  </xs:element>
                  <xs:element name="strFakturagrupp" minOccurs="0">
                    <xs:simpleType>
                      <xs:restriction base="xs:string">
                        <xs:maxLength value="6" />
                      </xs:restriction>
                    </xs:simpleType>
                  </xs:element>
                  <xs:element name="strProdukt" minOccurs="0">
                    <xs:simpleType>
                      <xs:restriction base="xs:string">
                        <xs:maxLength value="6" />
                      </xs:restriction>
                    </xs:simpleType>
                  </xs:element>
                  <xs:element name="strDelprodukt" minOccurs="0">
                    <xs:simpleType>
                      <xs:restriction base="xs:string">
                        <xs:maxLength value="6" />
                      </xs:restriction>
                    </xs:simpleType>
                  </xs:element>
                  <xs:element name="strTextkod" minOccurs="0">
                    <xs:simpleType>
                      <xs:restriction base="xs:string">
                        <xs:maxLength value="10" />
                      </xs:restriction>
                    </xs:simpleType>
                  </xs:element>
                  <xs:element name="strSeparatanlnr" minOccurs="0">
                    <xs:simpleType>
                      <xs:restriction base="xs:string">
                        <xs:maxLength value="15" />
                      </xs:restriction>
                    </xs:simpleType>
                  </xs:element>
                  <xs:element name="datUpphorddatum" type="xs:dateTime" minOccurs="0" />
                  <xs:element name="strDelAvArFrom" minOccurs="0">
                    <xs:simpleType>
                      <xs:restriction base="xs:string">
                        <xs:maxLength value="4" />
                      </xs:restriction>
                    </xs:simpleType>
                  </xs:element>
                  <xs:element name="strDelAvArTom" minOccurs="0">
                    <xs:simpleType>
                      <xs:restriction base="xs:string">
                        <xs:maxLength value="4" />
                      </xs:restriction>
                    </xs:simpleType>
                  </xs:element>
                  <xs:element name="bolUtforDebOrdFakt" type="xs:boolean" />
                  <xs:element name="intTaxaSpecificeringstyp" type="xs:unsignedByte" />
                  <xs:element name="bolPriserInklMoms" type="xs:boolean" />
                  <xs:element name="decKarlstorlek" type="xs:decimal" minOccurs="0" />
                  <xs:element name="intTomPerAr" type="xs:int" minOccurs="0" />
                  <xs:element name="bolKompost" type="xs:boolean" />
                  <xs:element name="bolSommar" type="xs:boolean" />
                  <xs:element name="strAvfallsslag" minOccurs="0">
                    <xs:simpleType>
                      <xs:restriction base="xs:string">
                        <xs:maxLength value="15" />
                      </xs:restriction>
                    </xs:simpleType>
                  </xs:element>
                  <xs:element name="strEWCKod" minOccurs="0">
                    <xs:simpleType>
                      <xs:restriction base="xs:string">
                        <xs:maxLength value="10" />
                      </xs:restriction>
                    </xs:simpleType>
                  </xs:element>
                  <xs:element name="decMangdPerTom" type="xs:decimal" minOccurs="0" />
                  <xs:element name="strEnhetkort" minOccurs="0">
                    <xs:simpleType>
                      <xs:restriction base="xs:string">
                        <xs:maxLength value="3" />
                      </xs:restriction>
                    </xs:simpleType>
                  </xs:element>
                  <xs:element name="datSkapad" type="xs:dateTime" />
                  <xs:element name="strSkapadAv">
                    <xs:simpleType>
                      <xs:restriction base="xs:string">
                        <xs:maxLength value="6" />
                      </xs:restriction>
                    </xs:simpleType>
                  </xs:element>
                </xs:sequence>
              </xs:complexType>
            </xs:element>
          </xs:choice>
        </xs:complexType>
        <xs:unique name="Constraint1" msdata:PrimaryKey="true">
          <xs:selector xpath=".//tbFuTaxa" />
          <xs:field xpath="strTaxekod" />
        </xs:unique>
      </xs:element>
    </xs:schema>

  • norpe

    Hi Roger,

    I think a better way to add a typed dataset to a project is to call Project.ProjectItems.AddFromFile instead of Solution.AddFromTemplate.
    That said, there may have been a bug in the beta2 drop that caused the exception you're seeing.
    If you use AddFromFile on the RTM drop, do you still see the error

    HTH

    Lorenzo Minore
    VB .Net Development

  • Vinod72

    Can you post the xsd-file you generated

    Thanks,

    Sirkku

  • lexp

    The following code works. Note that AddFromTemplate fails if the new project file name already exists in the destination.


    HTH,
    Sirkku


    Public Sub OnConnection(ByVal application As Object, _

     ByVal connectMode As ext_ConnectMode, ByVal addInInst As Object, _

     ByRef custom As Array) Implements IDTExtensibility2.OnConnection

        _applicationObject = CType(application, DTE2)

        _addInInstance = CType(addInInst, AddIn)

        SolutionExample(_applicationObject)

    End Sub

    < xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" /> 

    Sub SolutionExample(ByVal dte As DTE2)

        ' This function creates a solution and adds a Visual C# Console

        ' project to it.

        Try

            Dim soln As Solution2 = CType(DTE.Solution, Solution2)

            Dim csTemplatePath As String

            ' This path must exist on your computer.

            ' Replace <file path> below with an actual path.

            Dim csPrjPath As String = "<file path>"

            MsgBox("starting")

            ' Get the project template path for a C# console project.

            csTemplatePath = soln.GetProjectTemplate _

            ("ConsoleApplication.zip", "CSharp")

            ' Create a new C# Console project using

            ' the template obtained above.

            soln.AddFromTemplate(csTemplatePath, csPrjPath, _

            "New CSharp Console Project", False)

            MsgBox("done")

        Catch ex As System.Exception

            MsgBox(ex.ToString)

        End Try

    End Sub

     

    [C#]

    public void OnConnection(object application,

     Extensibility.ext_ConnectMode connectMode, object addInInst,

     ref System.Array custom)

    {

        _applicationObject = (DTE2)application;

        _addInInstance = (AddIn)addInInst;

        // Pass the applicationObject member variable to the code example.

        SolutionExample((DTE2)_applicationObject);

    }

     

    public void SolutionExample(DTE2 dte)

    {

        // This function creates a solution and adds a Visual C# Console

        // project to it.

        try{

            Solution2 soln = (Solution2)_applicationObject.Solution;

            String csTemplatePath;

            // The file path must exist on your computer.

            // Replace <file path> below with an actual path.

            String csPrjPath = "<file path>";

        "<file path>MessageBox.Show("Starting...");

    "<file path>"<file path>csTemplatePath =

    soln.GetProjectTemplate("ConsoleApplication.zip", "CSharp");

            // Create a new C# Console project using the template obtained

            // above.

            soln.AddFromTemplate(csTemplatePath, csPrjPath,

     "New CSharp Console Project", false);

            MessageBox.Show("Done!");

        }

        catch(SystemException ex)

        {

            MessageBox.Show("ERROR: " + ex);

        }

    }


  • foolish frost

    I will look into this and get back to you if I find an answer.
  • Flame Thrower

    Hi ,

    i am facing the same problem as roger faced about a year back.

    -------------------------------------------------------------------

    I use the following code to add the dataset and give its name as abc.xsd

    ProjectItem p1 = vsp.Project.ProjectItems..AddFromTemplate(@"C:\Program Files\Microsoft Visual Studio 8\Common7\IDE\ItemTemplates\CSharp\1033\DataSet.zip", "abc.xsd");

    Now when i look into what is added with the project ,it turns out to be ony an xsd file.When i click on it it open up in notepad with some binary characters.

    It might be because the other files abc.deisgner.cs, abc.abc.xsc and abc.xss are not added automatically.

    -----------------------------------------------------------------------------------------------------------------------------------------

    I tried to solve previous issue by first generating the xsd using untyped dataset of a particular table in database on a certain directory location(c:\dat4.xsd).

    Now i try to add this xsd to project pragrammatically using following code

    ProjectItem p2 = vsp.Project.ProjectItems.AddFromFile(@"c:\dat4.xsd");

    This indeed created the related xsc and xss file.Also i am able to open and see the table structure but the problem which is still persisting is that related .cs file is not generated.This .cs file is to be used to refer this typeddataset as an object in other classes.

    ------------------------------------------------------------------------------------------------------------------------------------------ 

    Can somebody please post the runnable code for adding a dataset programmatically in a project.

    Regards


  • Casey Chamberlain

    Yes, I'm using AddFromTemplate to add projects to the solution, and even to add forms/classes to existing projects, and that works as expected.

    It's only the dataset that gives me the exception.


  • AddFromTemplate in VS2005 (typed DataSet)