locked
TableAdapter connection string errors RRS feed

  • Question

  • User-1184680138 posted

    I am using a TableAdapter to open an SQL table.  It was working correctly, until I switched to a different table (in a different database), which needed a username and password as part of a connection string.  Adding the connection information to the Web.config file worked.  But I don't want to leave it there for security reasons.  The connection string is stored in the global assembly cache, and I am able to retrieve it and verify it is correct.  But when I try to say "myTableAdapter.Connection.ConnectionString = connectionStr;", it starts throwing nullReferenceException errors in both debugging and when published.  The debugging version takes me to here in the Designer.cs file: [global::System.Diagnostics.DebuggerNonUserCodeAttribute()]
            internal global::System.Data.SqlClient.SqlConnection Connection

    and complains about the line "this.InitConnection()". 

       The published version displays the following in the browser: 

    Object reference not set to an instance of an object.

    Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

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

     

     Any ideas on how to fix this?

    I hope I posted this in the right part of the forum... 

    Monday, July 14, 2008 3:00 PM

Answers

  • User-1184680138 posted

    I found a solution.  When I say "myTableAdapter.Connection.ConnectionString", apparently there isn't actually a "myTableAdapter.Connection" in the first place (even though Visual Studio pretends there is).  So, it tries to create one.  But when it does, it checks the Web.config file for the connection string by default.  The string isn't there, so it fails, leading to the null reference.  Instead, use this code:

     

    myTableAdapter.Connection = new System.Data.SqlClient.SqlConnection(connectionStr);
     This will create the connection, with the appropriate connection string.  No errors!
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, July 15, 2008 5:52 PM

All replies

  • User-1184680138 posted

    I found a solution.  When I say "myTableAdapter.Connection.ConnectionString", apparently there isn't actually a "myTableAdapter.Connection" in the first place (even though Visual Studio pretends there is).  So, it tries to create one.  But when it does, it checks the Web.config file for the connection string by default.  The string isn't there, so it fails, leading to the null reference.  Instead, use this code:

     

    myTableAdapter.Connection = new System.Data.SqlClient.SqlConnection(connectionStr);
     This will create the connection, with the appropriate connection string.  No errors!
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, July 15, 2008 5:52 PM
  • User-1184680138 posted

    Also, note that when I said "Designer.cs file" in the first post, I actually meant the DataSet.Designer.cs file.

    Tuesday, July 15, 2008 5:55 PM