Category Archives: asp.net

Master Page DataBind() will clear data on your page – remember!

I was working on a problem with a drop down list getting un-initialized and had run across this page:

c# – .Net Webform losing data – Stack Overflow.

This simple post is about a coder who was getting their ddl control reset every time a postback occurred. They had not yet learned about putting the initialization inside an if statement that only runs when the page is first loaded.

I had done this, and had not changed the code on this page for days and yet it stopped working.

Then I realized — I had been fighting with the links on the site.master.cs page that were causing the link to come up blank. I learned that when you have a reference in your head element on the master page like so:

<script src='<%# Helper.ResolveMyUrl(“/Scripts/libs/jquery-1.7.1.min.js”) %>’ type=”text/javascript”></script>

You need this:

Page.DataBind(); // (yes, but not quite…)

in your Page_Load so that the function call will be resolved. I added that line and my links all started working — and my drop down list started clearing on a postback. But there were enough hours between these discoveries, that I hadn’t made the connection.

What I really needed was:

if (! Page.IsPostBack)  // (better, but not yet done…)
Page.DataBind();

since the DataBind affects all of the data bound controls on all of the pages that use it as a master page.

But wait, there’s more!

I thought this had cleared up my issue, but then I found that another control that I initialized lost its value. It turns out that this DataBind on the entire page is not a good idea. Instead you need to be specific about the stuff on the master page. So I did this:

if (! Page.IsPostBack)
{
  Page.Header.DataBind(); // initializes the script references
  LoginView1.DataBind(); // initializes a link inside LoginView1
}

Ok, I think that does it. I feel better about a more qualified / constrained approach too.

With this qualification, I got the initialization on the links I needed, and didn’t interfere with the drop down lists that were initialized once and then needed to remain populated. I didn’t find this anywhere, and so I am hoping that it will save someone the couple hours I spent.

Happy coding!

WebMatrix Deployment

I have been noodling with MVC3 / cshtml / Razor, and I put together a web site and tried to deploy it. I was using the WebSecurity that was nicely integrated into the default web site that I started with, and was delighted about how easy it was to implement. With a few hours of work, I had a site that was performing registration, adding users to my database, emailing for confirmation and I didn’t write any of that code! Sweet. Let’s deploy. Then, ugh, an error.

Server Error in ‘/’ Application.


Configuration Error

Description: An error occurred during the processing of a configuration file required to service this request. Please review the specific error details below and modify your configuration file appropriately.

Parser Error Message: The connection name ‘LocalSqlServer’ was not found in the applications configuration or the connection string is empty.

Source Error:

Line 238: <membership> Line 239: <providers> Line 240: <add name=”AspNetSqlMembershipProvider” type=”System.Web.Security.SqlMembershipProvider, System.Web, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a” connectionStringName=”LocalSqlServer” enablePasswordRetrieval=”false” enablePasswordReset=”true” requiresQuestionAndAnswer=”true” applicationName=”/” requiresUniqueEmail=”false” passwordFormat=”Hashed” maxInvalidPasswordAttempts=”5″ minRequiredPasswordLength=”7″ minRequiredNonalphanumericCharacters=”1″ passwordAttemptWindow=”10″ passwordStrengthRegularExpression=”” />  Line 241: <add name=”MySQLMembershipProvider” type=”MySql.Web.Security.MySQLMembershipProvider, MySql.Web, Version=6.3.6.0, Culture=neutral, PublicKeyToken=c5687fc88969c44d” connectionStringName=”LocalMySqlServer” enablePasswordRetrieval=”false” enablePasswordReset=”true” requiresQuestionAndAnswer=”true” applicationName=”/” requiresUniqueEmail=”false” passwordFormat=”Clear” maxInvalidPasswordAttempts=”5″ minRequiredPasswordLength=”7″ minRequiredNonalphanumericCharacters=”1″ passwordAttemptWindow=”10″ passwordStrengthRegularExpression=”” /> Line 242: </providers>


Source File: C:\Windows\Microsoft.NET\Framework\v4.0.30319\Config\machine.config    Line: 240


Version Information: Microsoft .NET Framework Version:4.0.30319; ASP.NET Version:4.0.30319.272

I started immediately looking for an entry possibly added by WebMatrix. But there was none referencing AspNetSqlMembershipProvider, or LocalSqlServer. Then I noticed that this reference was in the machine.config of the server. Well, this is not my server, so I contacted support to ask them about it. They promptly and courteously told me that they did not solve programming questions. OK, on my own – no big surprise there. But I thought it might be a normal “gotcha” that lots of people run into. Well, it is… Here is what needs to happen:

First the reference is partially right. You DO need to have this reference in place, however the machine config does not know where your site has put the Membership info. So you have to replace it. Use this:

<connectionStrings>
<clear />
<add name=”myDB” providerName=”System.Data.SqlClient”
connectionString=”Data Source=someserver.com;Initial Catalog=myDB;User ID=myDB_user;Password=’xxxxx’;Integrated Security=False;” />
<remove name=”LocalSqlServer” />
<add name=”LocalSqlServer” providerName=”System.Data.SqlClient”
connectionString=”Data Source=someserver.com;Initial Catalog=myDB;User ID=myDB_user;Password=’xxxxx’;Integrated Security=False;” />
</connectionStrings>

Of course replace the database details with your own.

Once this was in place, I ran it again and got the following error:

Server Error in ‘/’ Application.


The Role Manager feature has not been enabled.

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.Configuration.Provider.ProviderException: The Role Manager feature has not been enabled.

Source Error:

Line 3: var providerName = “System.Data.SqlClient”; Line 4: //var db = Database.OpenConnectionString(connectionString, providerName); Line 5: WebMatrix.WebData.WebSecurity.InitializeDatabaseConnection (connectionString, providerName, Line 6: “UserProfile”, “UserId”, “Email”, true); Line 7:


Source File: e:\web\myWeb\_AppStart.cshtml    Line: 

It is now looking at my (mind you WORKING LOCALLY 🙂 ) init and saying it is failing. To be honest, since this is new tech for me, I wasn’t sure if I should be enabling the role manager, or if I had enabled something that was adding a new / different security setting that would cause some conflict. With a bit more research (Googling WebSecurity and Role Manager), I saw that the WebSecurity uses the Role Manager in “SimpleMembership” mode. OK, so this is needed and probably ok.

But I don’t need the AspNetSqlMembershipProvider… So I removed it (with the <clear/> below):

Inside the System.Web section of my web.config:

<membership defaultProvider=”SimpleMembershipProvider”>
<providers >
<clear/>
<add name=”SimpleMembershipProvider” type=”WebMatrix.WebData.SimpleMembershipProvider,WebMatrix.WebData”/>
</providers>
</membership>

Then I got the error:

Server Error in ‘/’ Application.


Default Membership Provider must be specified.

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.Configuration.Provider.ProviderException: Default Membership Provider must be specified.

With a little more digging, I found that this entry in the appSettings section will take care of that issue.

<add key=”enableSimpleMembership” value=”true” />

I hope this helps someone make a few less round trips searching!

Peace…

Helpful Links for this issue:

http://stackoverflow.com/questions/3874279/the-role-manager-feature-has-not-been-enabled

http://forums.asp.net/t/1695156.aspx/1?WebSecurity+in+MVC

https://forums.iis.net/p/1188577/2018786.aspx

WCF – ____ is already defined.

This drove me crazy for a several hours. Turned out the solution was simple. Again MS has limitations that don’t allow combinations of input / output. YOu have either use wrappers or no wrappers. It seems like I have seen implementations where this limit did not exist, but putting wrappers (reauest / response) around my new entry points fixed the issue.

More info at stack overflow:

wcf – Add Service Reference “___ is already defined”a – Stack Overflow

 

Configuration Button Disabled on new IIS site

I created a new web site in IIS and then installed a web site and went to the properties page. I needed to edit the config so I went to the ASP.Net tab and found that the Edit Configuration button was disabled. I searched the web using: “new IIS” web.config “button is disabled” “Edit configuration” and found nothing. It is a simple issue: The Asp.Net version must be set to 2.x not 1.x in the properties page. There now the issue is searchable, and I will know what to do next time!