VS2013 An exception occurred while initializing the database

I’m doing a code first Entity database

{“An exception occurred while initializing the database. See the InnerException for details.”}

It was working fine, but I decided to delete the physical database to see it recreate it, but it just fails every time.

I get the error

Cannot attach the file ‘C:\ASP\OdeToFood\OdeToFood\App_Data\OdeToFoodDb.mdf’ as database ‘OdeToFoodDb’.”}

Its code first so I don’t understand why its not recreating the database.



So deleting the file from Visual Studio was a dumb idea.

SQL still has it registered.

Fire up MS Sql Management Tool, connect to the server in my case


You will see the database, still exists, delete it, you will get an error. The file doesn’t exist. Refresh and its gone from SQL. Now run your code again and all is good.

So delete the DB from the management tool not visual studio

Perform Specific Button click event when user press Enter key in Textbox

Put your form inside an asp.net panel control and set its DefaultButton attribute with your button ID. See the code below:


<asp:Panel ID="Panel1" runat="server" DefaultButton="Button1">
        <asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Conditional">
            <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
            <asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Send" />

VS 2013 : Project name with number in brackets

In file C:\Users\Username\Documents\IISExpress\config\applicationhost.config, under section



there was all websites (projects) I have ever opened. I deleted these items and now, on New Website there is no number in brackets. NOTE: I think it is not wise to delete the entire file but only the part that applies to sites/projects.



Reff: http://forums.asp.net/t/1953275.aspx?Project+name+with+number+in+brackets

Visual Studio 2013 Change the Default Location for Projects

For corporate office user my document is always redirected to a file server where creating exe file is restricted.

1. Create a folder in c drive called: vs 2013

2. Inside that folder create 2 folders



3. Inside Templates, create another 2 folders:



4. Open visual studio and follow the steps

  • On the Tools menu, select Options.
  • From the Projects and Solutions folder, select General.
  • In the Visual Studio projects location text box, enter a location for files and projects.




And you should be all set.


Remove HTML tags in String in C#

If the string is like that

string s = “"<p>hello <b>there</b></p>"


Use the regular expression:


using System.Text.RegularExpressions;

static string StripHTML (string inputString)
   return Regex.Replace 
     (inputString, "<.*?>", string.Empty);

But if the input string is like this:

String s = "&lt;/b.......&gt;"

You need to decode it first using http utils as follows:

Add a sub functiona and pass the input string to that to decode the string and use the regular expression avobe.

public static string DecodeHtmlEntities(string text)
        return HttpUtility.HtmlDecode(text);

RadioButton group example: how to use RadioButton array / group in asp.net

<%@ Page Language=”C#” %>
<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Transitional//EN” “http://www.w3.org/TR/xhtml1-transitional.dtd”&gt;

<script runat=”server”>
protected void RadioButton_CheckedChanged(object sender,System.EventArgs e) {
if (RadioButton1.Checked == true)
Response.Write(“You Selected: Asp.Net”);
else {
Response.Write(“You Selected: ColdFusion”);

<html xmlns=”http://www.w3.org/1999/xhtml”&gt;
<head runat=”server”>
<title>RadioButton example: how to use RadioButton control in asp.net</title>

<form id=”form1″ runat=”server”>
<asp:RadioButton ID=”RadioButton1″ runat=”server” Text=”Asp.Net” GroupName=”Software” AutoPostBack=”true” OnCheckedChanged=”RadioButton_CheckedChanged” />
<asp:RadioButton ID=”RadioButton2″ runat=”server” Text=”ColdFusion” GroupName=”Software” AutoPostBack=”true” OnCheckedChanged=”RadioButton_CheckedChanged” />






Reference: http://asp-net-example.blogspot.com/2008/10/radiobutton-example-how-to-use.html

By simplemsexchange Posted in ASP.NET

asp.net menu selected style

This seems to be a bug with the .NET menu. There is some information regarding this here. What you might want to do then is remove the staticSelectedStyle attribute and simply add this to your css:

.menu a.static.selected
    background-color: #680840 !important;
    color: #FFF !important;
    text-decoration: none !important;

You might also have to add some code to your master’s page load to determine which should be the selected item like this:

protected void Page_Load(object sender, EventArgs e)
    string path = Request.AppRelativeCurrentExecutionFilePath;
    foreach (MenuItem item in NavigationMenu.Items)
    item.Selected = item.NavigateUrl.Equals(path, StringComparison.InvariantCultureIgnoreCase);

Reff: http://stackoverflow.com/questions/7148872/asp-net-problems-with-static-selected-style-for-a-selected-page-on-the-menu 
By simplemsexchange Posted in ASP.NET