Exception using Querytables in Excel Automation
- by sam
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