Exception using Querytables in Excel Automation

Posted by sam on Stack Overflow See other posts from Stack Overflow or by sam
Published on 2010-06-18T14:59:01Z Indexed on 2010/06/18 15:03 UTC
Read the original article Hit count: 974

Filed under:
|
|

Hi,

I'm using Automation to populate a range in Excel using a querytable.. However, when I try to add a querytable to the qorksheet I get an exception. I checked the connection string and its working fine.. Can some one please help me with this??

        public void writeproc1()
    {
        try
        {
            Worksheet ws = (Worksheet)wb.Worksheets.Add(Missing.Value, Missing.Value, Missing.Value, Missing.Value);
            Range rng = ws.get_Range("A1", "E14");

            QueryTable qt = ws.QueryTables.Add("Data Source=(local)\\SQLEXPRESS;initial catalog=temp;Integrated Security=SSPI;", rng, "Select * From Table_1");
            qt.RefreshStyle = XlCellInsertionMode.xlInsertEntireRows;
            qt.Refresh(false);
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex.ToString());
            Console.ReadKey();
        }
    }

Exception Thrown

System.Runtime.InteropServices.COMException (0x800A03EC): Exception from HRESULT: 0x800A03EC at System.RuntimeType.ForwardCallToInvokeMember(String memberName, BindingFlags flags, Object target, Int32[] aWrapperTypes, MessageData& msgData) at Microsoft.Office.Interop.Excel.QueryTables.Add(Object Connection, Range Destination, Object Sql) at tmp.Program.writeproc1() in ...Projects\tmp\tmp\Program.cs:line 25

© Stack Overflow or respective owner

Related posts about c#

Related posts about excel