HGomez
HGomez

Reputation: 1534

Failed to generate a user instance of SQL Server

I have a local project where I am trying to input data from an ASP:textbox to a database.

On building I get the following...

"Failed to generate a user instance of SQL Server. Only an integrated connection can generate a user instance. The connection will be closed."

I'm a little puzzled here, I have checked the database and it is active with the credentials i am trying to connect with.

Here is the code behind C#

    namespace OSQARv0._1
{
    public partial class new_questionnaire : System.Web.UI.Page
    {
        SqlDataAdapter da = new SqlDataAdapter();
        SqlConnection sqlcon = new SqlConnection(@"user id=*myuserid*;"+"password=*mypassword*;"+"Data Source=mssql.dev-works.co.uk;User Instance=True;"+"Database=devworks_osqar"+"Trusted_Connection=true;");

        protected void Page_Load(object sender, EventArgs e)
        {

        }
        protected void Create_Click(object sender, EventArgs e)
        {
            DataBinder ds = new DataBinder();
            sqlcon.Open();
            SqlCommand sqlcmd = new SqlCommand("INSERT INTO QUESTIONNAIRES (QuestionnaireName) VALUES ('"+qnrName.Text+"')");
            sqlcmd.Parameters.AddWithValue("@Name", qnrName.Text);

            sqlcmd.ExecuteNonQuery();

            sqlcon.Close();
        }
    }
}

Any help would be much appreciated.

Edited code behind (read commment below) C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.Sql;
using System.Data.SqlClient;

namespace OSQARv0._1
{
    public partial class new_questionnaire : System.Web.UI.Page
    { 
        protected void Page_Load(object sender, EventArgs e)
        {

        }
        private string myConnectionString;
        private SqlConnection myConn;
        public new_questionnaire()
        {
            myConn = new SqlConnection();
            myConnectionString += "Data Source=mssql.database.co.uk; Initial Catalog=devworks_osqar;User ID=myusername;Password=mypassword";
        }

        protected void Create_Click(object sender, EventArgs e)
        {
            //DataBinder ds = new DataBinder();
            SqlCommand sqlcmd = new SqlCommand("INSERT INTO QUESTIONNAIRES (QuestionnaireName) VALUES ('"+qnrName.Text+"')");
            sqlcmd.CommandType = CommandType.StoredProcedure;
            sqlcmd.Parameters.AddWithValue("@Name", qnrName.Text);
            insert(sqlcmd);

        }
        private void insert(SqlCommand myCommand)
        {
            myConn.Open();
            myCommand.ExecuteNonQuery();
            myConn.Close();
        }
    }
}

Upvotes: 1

Views: 5203

Answers (1)

rick schott
rick schott

Reputation: 20617

Fix error "Failed to generate a user instance of SQL Server due to a failure in starting the process for the user instance."

Content from link pasted and altered below in case reference site is removed in the future:

Step 1.

Enabling User Instances on your SQL Server installation First we are gonna make sure we have enabled User Instances for SQL Server installation.

Go to Query Window in SQL Server Management Studio and type this:

exec sp_configure 'user instances enabled', 1.
Go
Reconfigure

Run this query and then restart the SQL Server.

Step 2.

Deleting old files Now we need to delete any old User Instances. Go to your C drive and find and completely DELETE this path (and all files inside):

C:\Documents and Settings\ YOUR_USERNAME \Local Settings\Application Data\Microsoft\Microsoft SQL Server Data\SQLEXPRESS

(Dont forget to replace the bold text in the path with your current username (if you are not sure just go to C:\Documents and Settings\ path to figure it out).

After deleting this dir you can go to Visual Studio, create ASP.NET WebSite and click on your App_Data folder and choose Add New Item and then choose SQL Server Database and it should work!!!

Upvotes: 3

Related Questions