p2p.wrox.com Forums

p2p.wrox.com Forums (http://p2p.wrox.com/)
-   BOOK: Professional ASP.NET 3.5 Security, Membership, and Role Management ISBN: 978-0-470-37930-1 (http://p2p.wrox.com/book-professional-asp-net-3-5-security-membership-role-management-isbn-978-0-470-37930-1-429/)
-   -   An error occurred while attempting to initialize a System.Data.SqlClient.SqlConnectio (http://p2p.wrox.com/book-professional-asp-net-3-5-security-membership-role-management-isbn-978-0-470-37930-1/94279-error-occurred-while-attempting-initialize-system-data-sqlclient-sqlconnectio.html)

Priyanka Nellore March 19th, 2015 11:36 AM

An error occurred while attempting to initialize a System.Data.SqlClient.SqlConnectio
 
I'm trying to record User Activity for my Entity Framework application and when I run my code I'm getting the error "An error occurred while attempting to initialize a System.Data.SqlClient.SqlConnection object. The value that was provided for the connection string may be wrong, or it may contain an invalid syntax. Parameter name: connectionString". The application does not have Username s separately our department has MUD ID's which are hardcoded with the IP address and those MUD ID's automatically get recognized and we get the access to the application its not USERNAME Password giving type. could anybody suggest any solution for this and I'm using Enterprise Library for my project just for your information. Please help

this my UserActivityPage.aspx.cs code

namespace BasePage
{
public partial class UserActivityPage : BasePage
{


protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
base.LogActivity("Visiting the UserActivityPage...", true);
}
}
}

public class BasePage : System.Web.UI.Page
{
protected void LogActivity(string activity, bool recordPageUrl)
{
if (Request.IsAuthenticated)
{
// Get information about the currently logged on user
//BL3Helper.GetSession(BL3Session.LOGGEDUSERNAME);

MembershipUser currentUser = Membership.GetUser(false); // Exception at GetUser function - System.ArgumentException
//MembershipUser currentUser = null;

//currentUser = "bk293393";
if (currentUser != null)
{
Guid userId = (Guid)currentUser.ProviderUserKey;
// Log the activity in the database
using (SqlConnection myConnection = new SqlConnection(ConfigurationManager.ConnectionStrin gs["MembershipConnectionString"]
.ConnectionString)) //Exception at Bolded ConnectionString - System.Configuration.ConfigurationErrorsException
{
SqlCommand myCommand = new SqlCommand();
myCommand.CommandText = "[dbo].[SP_LogUserActivity]";
myCommand.CommandType = CommandType.StoredProcedure;
myCommand.Connection = myConnection;
myCommand.Parameters.AddWithValue("@USER_CD", BL3Session.LOGGEDUSERNAME);
myCommand.Parameters.AddWithValue("@Activity", activity);
if (recordPageUrl)
myCommand.Parameters.AddWithValue("@PageUrl", Request.RawUrl);
else
myCommand.Parameters.AddWithValue("@PageUrl", DBNull.Value);
myConnection.Open();
myCommand.ExecuteNonQuery();
myConnection.Close();
}
}
}
}
}

}

and in my web.config file I've the following code:

<add name="MyDB" connectionString="metadata=res://*/App_Code.BL3DBModel.csdl|res://*/App_Code.BL3DBModel.ssdl|res://*/App_Code.BL3DBModel.msl;provider=System.Data.SqlCl ient;provider connection string=&quot;Data Source=server_name;Initial Catalog=DBname;user id=sa;password=pwd;Persist Security Info=False;Integrated Security=False;MultipleActiveResultSets=True&quot; " providerName="System.Data.SqlClient" />

<buildProviders>
<add extension=".edmx" type="System.Data.Entity.Design.AspNet.EntityDesig nerBuildProvider" />
</buildProviders>
</compilation>
<membership defaultProvider="CustomizedProvider">
<providers>
<add name="CustomizedProvider"
type="System.Web.Security.SqlMembershipProvider"
connectionStringName="MyDB"
applicationName="/" />
</providers>
</membership>
</system.web>

mmorgan30 March 19th, 2015 08:22 PM

The only connection string that I see, is a connection string for an entity frame work DBConext or ObjectContext but yet, you are trying to instance a new sqlconnection and use sqlcommands and parameters. You are mixing your technologies here. The exception thrown clearly states the connectionstring referenced is not in the proper syntax. Which technology are you trying use?

Priyanka Nellore March 20th, 2015 08:27 AM

I'm using the connection string that is described in the web.config file but I tried to pass the config file's "MyDB" connection string but that also throws the same error.

These are the connection strings I'm using in my application and declared in web.config file

<connectionStrings>
<add name="BL3DBEntities" connectionString="metadata=res://*/App_Code.BL3DBModel.csdl|res://*/App_Code.BL3DBModel.ssdl|res://*/App_Code.BL3DBModel.msl;provider=System.Data.SqlCl ient;provider connection string=&quot;Data Source=server_name;Initial Catalog=DBname;user id=sa;password=pwd;Persist Security Info=False;Integrated Security=False;MultipleActiveResultSets=True&quot; "
providerName="System.Data.EntityClient" />

<add name="ErrorLogDB" connectionString="metadata=res://*/App_Code.BL3DBModel.csdl|res://*/App_Code.BL3DBModel.ssdl|res://*/App_Code.BL3DBModel.msl;provider=System.Data.SqlCl ient;provider connection string=&quot;Data Source=server_name;Initial Catalog=DBname;user id=sa;password=pwd;Persist Security Info=False;Integrated Security=False;MultipleActiveResultSets=True&quot; "
providerName="System.Data.SqlClient" />

<add name="MyDB" connectionString="metadata=res://*/App_Code.BL3DBModel.csdl|res://*/App_Code.BL3DBModel.ssdl|res://*/App_Code.BL3DBModel.msl;provider=System.Data.SqlCl ient;provider connection string=&quot;data source=server_name;initial catalog=DBname;persist security info=True;user id=sa;password=pwd;multipleactiveresultsets=True;& quot;" providerName="System.Data.SqlClient" />
</connectionStrings>

The MyDB connection string I've specially created for logging the user activity.

mmorgan30 March 20th, 2015 09:07 AM

All 3 of these connection strings are for entity frame work context. Since the are in the web.cong and typically added when you add an entity model, I am assuming you have models? Why not use the context associated with the connection string and instead of a sqlconnection and sqlcommand

Priyanka Nellore March 20th, 2015 11:23 AM

Actually I'm new to entity framework and this project is partly developed by another organization which was transferred to us and I'm trying to do add some features to it...so could you explain me in detail where these models could be and how I can use them instead of sqlconnection and sqlcommand? and this page is not associated with any other tables in the entity model (.edmx file) its just a test I'm trying to do...if its successful I have to include in the remaining pages.

mmorgan30 March 21st, 2015 04:02 PM

I believe that your models are in the app_code folder according to the connection string. Trying doing a search of the entire solution for the work Context until you find a class declarion that either extends an objectcontext or dbcontext.

mmorgan30 March 21st, 2015 04:19 PM

as an example for the ErrorLogDB, I don't know all the details of the context so this purely example of entity frame and fetching and adding some data.

Code:



public Logs[] GetLogsBetween(DateTime start, DateTime end )
{
      Logs[] rVal = null;

      using( var db = new ErrorLogDBContext() )
      {
            rVal = db.LogTable.Where( r => r.LogTime >= start && r.LogTime <= end ).ToArray();
      }

      return rVal;
}


public void Log( Log log )
{
        using( var db = new ErrorLogDBContext() )
      {
            db.LogTable.Add(log)
            db.SaveChanges();
      }
}


use between ObjectContext and DBContext are slightly different but for the most part are the same (DBContext consumes an ObjectContext). the illustration above is of a DBContext


All times are GMT -4. The time now is 10:53 PM.

Powered by vBulletin®
Copyright ©2000 - 2017, Jelsoft Enterprises Ltd.
2013 John Wiley & Sons, Inc.