0

Please let me know if my title does not make sense but I'm sitting with a quite frustrating issue.

A colleague of mine created a asp.net application where students can write exams etc. This works perfectly fine.

We have created a student management application (vb.net win forms) that serves to register the students for above mentioned exams. We don't want the students to register when they need to write, simply just log in with their details and write the available exams.

So the idea is to register them in the asp.net tables when creating them on the student management site (vb.net win forms app). So this is what I have done (this is a test application using the same code as the actual win forms):

Imports ExamRegister
Imports System.Web
Imports System.Data

Public Class Form1
Dim objAccountControler As ExamRegister.Controllers.AccountController = New ExamRegister.Controllers.AccountController()
Dim objRegisterModel As ExamRegister.Models.RegisterModel = New Models.RegisterModel
Dim objStudent As ExamRegister.Models.tStudent = New Models.tStudent

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click

    objStudent.Name = txtForename.Text
    objStudent.Surname = txtSurname.Text
    objStudent.CenterID = Convert.ToInt32(txtCenterid.Text)
    objStudent.IDNumber = txtIDNumber.Text
    objStudent.CertLangID = Convert.ToInt32(txtCertLang.Text)
    objStudent.ExamNo = txtExamNo.Text
    objStudent.ModifiedBy = 1

    objAccountControler.RegisterFromTransfer(objStudent)

    MessageBox.Show("inserted")

End Sub
End Class

This is the error we keep getting:

An unhandled exception of type 'System.Web.HttpException' occurred in System.Web.dll

Additional information: Unable to connect to SQL Server database.

NOTE

  • We are running this app on the same database as the asp.net web application (that works).
  • I have made sure that the database is available.
  • We have also done the suggestions in the following links here and here

  • The asp.net web app works fine on his side. I imported and reference the dll that was created from his app called ExamRegister

Here is the full extract of the exception:

System.Web.HttpException was unhandled
  ErrorCode=-2147467259
  HResult=-2147467259
  Message=Unable to connect to SQL Server database.
  Source=System.Web
  WebEventCode=0
  StackTrace:
       at System.Web.Management.SqlServices.GetSqlConnection(String server, String user, String password, Boolean trusted, String connectionString)
       at System.Web.Management.SqlServices.SetupApplicationServices(String server, String user, String password, Boolean trusted, String connectionString, String database, String dbFileName, SqlFeatures features, Boolean install)
       at System.Web.Management.SqlServices.Install(String database, String dbFileName, String connectionString)
       at System.Web.DataAccess.SqlConnectionHelper.CreateMdfFile(String fullFileName, String dataDir, String connectionString)
       at System.Web.DataAccess.SqlConnectionHelper.EnsureDBFile(String connectionString)
       at System.Web.DataAccess.SqlConnectionHelper.GetConnection(String connectionString, Boolean revertImpersonation)
       at System.Web.Security.SqlMembershipProvider.CreateUser(String username, String password, String email, String passwordQuestion, String passwordAnswer, Boolean isApproved, Object providerUserKey, MembershipCreateStatus& status)
       at System.Web.Security.Membership.CreateUser(String username, String password, String email, String passwordQuestion, String passwordAnswer, Boolean isApproved, Object providerUserKey, MembershipCreateStatus& status)
       at System.Web.Security.Membership.CreateUser(String username, String password, String email, String passwordQuestion, String passwordAnswer, Boolean isApproved, MembershipCreateStatus& status)
       at ExamRegister.Controllers.AccountController.RegisterFromTransfer(tStudent a)
       at WindowsApplication1.Form1.Button1_Click(Object sender, EventArgs e) in C:\Users\Johnathan\AppData\Local\Temporary Projects\WindowsApplication1\Form1.vb:line 21
       at System.Windows.Forms.Control.OnClick(EventArgs e)
       at System.Windows.Forms.Button.OnClick(EventArgs e)
       at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
       at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
       at System.Windows.Forms.Control.WndProc(Message& m)
       at System.Windows.Forms.ButtonBase.WndProc(Message& m)
       at System.Windows.Forms.Button.WndProc(Message& m)
       at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
       at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
       at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
       at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
       at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr dwComponentID, Int32 reason, Int32 pvLoopData)
       at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
       at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
       at System.Windows.Forms.Application.Run(ApplicationContext context)
       at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.OnRun()
       at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.DoApplicationModel()
       at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.Run(String[] commandLine)
       at WindowsApplication1.My.MyApplication.Main(String[] Args) in 17d14f5c-a337-4978-8281-53493378c1071.vb:line 81
       at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
       at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
       at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
       at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
       at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
       at System.Threading.ThreadHelper.ThreadStart()
  InnerException: System.Data.SqlClient.SqlException
       Class=20
       ErrorCode=-2146232060
       HResult=-2146232060
       LineNumber=0
       Message=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)
       Number=-1
       Server=""
       Source=.Net SqlClient Data Provider
       State=0
       StackTrace:
            at System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection, Action`1 wrapCloseInAction)
            at System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj, Boolean callerHasConnectionLock, Boolean asyncClose)
            at System.Data.SqlClient.TdsParser.Connect(ServerInfo serverInfo, SqlInternalConnectionTds connHandler, Boolean ignoreSniOpenTimeout, Int64 timerExpire, Boolean encrypt, Boolean trustServerCert, Boolean integratedSecurity, Boolean withFailover)
            at System.Data.SqlClient.SqlInternalConnectionTds.AttemptOneLogin(ServerInfo serverInfo, String newPassword, SecureString newSecurePassword, Boolean ignoreSniOpenTimeout, TimeoutTimer timeout, Boolean withFailover)
            at System.Data.SqlClient.SqlInternalConnectionTds.LoginNoFailover(ServerInfo serverInfo, String newPassword, SecureString newSecurePassword, Boolean redirectedUserInstance, SqlConnectionString connectionOptions, SqlCredential credential, TimeoutTimer timeout)
            at System.Data.SqlClient.SqlInternalConnectionTds.OpenLoginEnlist(TimeoutTimer timeout, SqlConnectionString connectionOptions, SqlCredential credential, String newPassword, SecureString newSecurePassword, Boolean redirectedUserInstance)
            at System.Data.SqlClient.SqlInternalConnectionTds..ctor(DbConnectionPoolIdentity identity, SqlConnectionString connectionOptions, SqlCredential credential, Object providerInfo, String newPassword, SecureString newSecurePassword, Boolean redirectedUserInstance, SqlConnectionString userConnectionOptions, SessionData reconnectSessionData)
            at System.Data.SqlClient.SqlConnectionFactory.CreateConnection(DbConnectionOptions options, DbConnectionPoolKey poolKey, Object poolGroupProviderInfo, DbConnectionPool pool, DbConnection owningConnection, DbConnectionOptions userOptions)
            at System.Data.ProviderBase.DbConnectionFactory.CreateNonPooledConnection(DbConnection owningConnection, DbConnectionPoolGroup poolGroup, DbConnectionOptions userOptions)
            at System.Data.ProviderBase.DbConnectionFactory.TryGetConnection(DbConnection owningConnection, TaskCompletionSource`1 retry, DbConnectionOptions userOptions, DbConnectionInternal oldConnection, DbConnectionInternal& connection)
            at System.Data.ProviderBase.DbConnectionInternal.TryOpenConnectionInternal(DbConnection outerConnection, DbConnectionFactory connectionFactory, TaskCompletionSource`1 retry, DbConnectionOptions userOptions)
            at System.Data.ProviderBase.DbConnectionClosed.TryOpenConnection(DbConnection outerConnection, DbConnectionFactory connectionFactory, TaskCompletionSource`1 retry, DbConnectionOptions userOptions)
            at System.Data.SqlClient.SqlConnection.TryOpenInner(TaskCompletionSource`1 retry)
            at System.Data.SqlClient.SqlConnection.TryOpen(TaskCompletionSource`1 retry)
            at System.Data.SqlClient.SqlConnection.Open()
            at System.Web.Management.SqlServices.GetSqlConnection(String server, String user, String password, Boolean trusted, String connectionString)
       InnerException: 

Is this even possible how we are trying to do this or not? And if it is, what are we doing wrong here?

Also here is the connection string in his application:

 <connectionStrings>   
   <remove name="ExamCenter" />
   <add name="ExamCenter" connectionString="Data Source=DEV;Initial Catalog=ExamCenter;User ID=[id];Password=[password]"
        providerName="System.Data.SqlClient" />

 </connectionStrings>

Thanks in advance.

Community
  • 1
  • 1
J2H656
  • 101
  • 1
  • 16
  • Is the connection string in your VB.NET application? Since you're just referencing his DLL, I believe (note 100% sure) that the DLL doesn't contain the web.config file so you'll have to add the connection string in your app.config file. – Blue Eyed Behemoth Sep 19 '16 at 13:22
  • @BlueEyedBehemoth - The connection string is not in the VB.Net Application no. So i did what you said. added the connection string there but to no avail. I opened the connection before running his function and its also the same. We then went and opened the connection in the function before it registers the asp.net members but now we get the following: `An unhandled exception of type 'System.InvalidOperationException' occurred in System.Data.dll Additional information: The ConnectionString property has not been initialized.` – J2H656 Sep 19 '16 at 14:12
  • http://stackoverflow.com/questions/9162647/the-connectionstring-property-has-not-been-initialized-error-in-vb-net See if that helps. – Blue Eyed Behemoth Sep 19 '16 at 14:30
  • @BlueEyedBehemoth - tried that now as well. Is now back at the "Unable to connect to SQL Server database." We have opened the connection now on my side, and again on his side, close it at the right places but still this error. The connection opens successfully on both our apps. But its when i call the function from his DLL that it goes all weird in the SQL Connection – J2H656 Sep 19 '16 at 14:47
  • Interesting, and I'm sure you've checked permissions? – Blue Eyed Behemoth Sep 19 '16 at 14:52
  • @BlueEyedBehemoth - I might be a bit daft, but which permissions are you talking about? – J2H656 Sep 19 '16 at 15:00
  • Ignore me, that was a silly question. I was thinking about file access. Unfortunately, I'm fresh out of ideas. – Blue Eyed Behemoth Sep 19 '16 at 15:02

0 Answers0