.NET
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Error on connection to MS-SQL inside AutoCAD 2012

3 REPLIES 3
SOLVED
Reply
Message 1 of 4
jimanderson
771 Views, 3 Replies

Error on connection to MS-SQL inside AutoCAD 2012

Hello All,

I have having no luck with getting a list of titleBlock's from a MS-SQL to post in a listbox.  The code works great outside of AutoCAD 2012 as a stand alone application. I am getting a error on the connection to MS-SQL (See in RED). Has anyone seen this before?

 

My System Info:

Win 7 64b. 24GB Ram

AutoCAD 2012

VS2010 .NET 4.0

MS-SQK 2008 R2

 //=================================

The Error Dump

//=================================

Application does not support just-in-time (JIT)

debugging. See the end of this message for details.

 

************** Exception Text **************

System.NullReferenceException: Object reference not set to an instance of an object.

   at Myclass.Form1.Form1_Load(Object sender, EventArgs e) in C:\Users\JimAnderson\Documents\Visual Studio 2010\Projects\AutoCAD 2012 plug-in 4.01\AutoCAD 2012 plug-in 4.01\Form1.cs:line 18

   at System.Windows.Forms.Form.OnLoad(EventArgs e)

   at System.Windows.Forms.Control.CreateControl(Boolean fIgnoreVisible)

 

//=================================

 

The Code

//=================================

Form1.cs

//=================================

using System;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Windows.Forms;

namespace Myclass
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            SqlConnection conn1 = new SqlConnection(ConfigurationManager.ConnectionStrings["SpeedSteel"].ConnectionString);
            using (conn1)
            {
                string Strquery1 = "SELECT * FROM dbo.TITLEBLOCKS";
                conn1.Open();
                SqlDataAdapter adapter1 = new SqlDataAdapter(Strquery1, conn1);
                System.Data.DataTable dsTITLEBLOCK = new System.Data.DataTable();
                adapter1.Fill(dsTITLEBLOCK);
                this.TitleBlockListBox.Items.Clear();
                int i = 0;
                foreach (DataRow row in dsTITLEBLOCK.Rows)
                {
                    this.TitleBlockListBox.Items.Add(dsTITLEBLOCK.Rows[i]["DIALOGLIST"].ToString());
                    i++;
                    this.TitleBlockListBox.Refresh();
                }
                conn1.Close();
            }
        }
    }
}

//=================================

ClassLibaray.cs

//=================================

using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.Runtime;

// This line is not mandatory, but improves loading performances
[assembly: CommandClass(typeof(Myclass.Myclass))]

namespace Myclass
{
    public class Myclass
    {
        [CommandMethod("HELLO")]
        public void myHelloAlertCommand()
        {
            //Write to an alert box
            Application.ShowAlertDialog("Hello Jim!");
        }
        [CommandMethod("MYFORM")]
        public void myApp()
        {
            Form1 formx = new Form1();
            formx.Show();
        }
    }
}

//=================================

App.Config

//=================================

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <connectionStrings>
    <add name="SpeedSteel"
         connectionString="Data Source=JANDERSON90\SPEEDSTEEL;Initial Catalog=SpeedSteel;Persist Security Info=True;User ID=SpeedSteel;Password=SpeedSteel"/>
  </connectionStrings>
</configuration>

//=================================

3 REPLIES 3
Message 2 of 4
norman.yuan
in reply to: jimanderson

Since it is the very first line in the Form1_Load() that gives NullreferenceException, it is very possible that the code could not find the connectionString named as "SpeedSteel" in the ConnectionString section from the ACAD.EXE.Confg file. not a app.congi file you created.

 

Because you are doing an Acad plug-in, not an standalone app, you do not create a app.config file and expect VS to create a [AppName].exe.config file after you build your code.

 

I the case of Acad .NET plug-in app, Acad itself is the .NET app, which comes with its own *.config file: acad.exe.config, sitting in AutoCAD installation folder. You open it and add the connectionStrings section (or appSettings section) to this config file.

 

HTH

Norman Yuan

Drive CAD With Code

EESignature

Message 3 of 4
jimanderson
in reply to: norman.yuan

I tryed that and get this now...

 

Dump:

************** Exception Text **************
System.Data.SqlClient.SqlException (0x80131904): A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections. (provider: SQL Network Interfaces, error: 26 - Error Locating Server/Instance Specified)
   at System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection)

Message 4 of 4
jimanderson
in reply to: jimanderson

It helps to turn on the SQL server... Smiley Happy

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk DevCon in Munich May 28-29th


Autodesk Design & Make Report

”Boost