title: Unable to Insert into Database in ASP.NET using C# - Stack Empty
body: <p>I am trying to insert user data into a database file using Microsoft Visual Studio. I have written a form using asp.net, added database to directory, and created c# command to insert data to database, however I am getting a stack is empty error.</p>
  
  <blockquote>
    <p>An unhandled exception was generated during the execution of the
    current web request. Information regarding the origin and location of
    the exception can be identified using the exception stack trace below.</p>
    
    <p>Stack Trace: </p>
    
    <p>[InvalidOperationException: Stack empty.]<br>
    System.ThrowHelper.ThrowInvalidOperationException(ExceptionResource
    resource) +52    System.Collections.Generic.Stack`1.Pop() +6856361<br>
    Microsoft.VisualStudio.Web.PageInspector.Runtime.WebForms.SelectionMappingRenderTraceListener.EndRendering(TextWriter
    writer, Object renderedObject) +85<br>
    System.Web.UI.RenderTraceListenerList.EndRendering(TextWriter writer,
    Object renderedObject) +66<br>
    System.Web.UI.Control.RenderControlInternal(HtmlTextWriter writer,
    ControlAdapter adapter) +169<br>
    System.Web.UI.Control.RenderControl(HtmlTextWriter writer,
    ControlAdapter adapter) +287<br>
    System.Web.UI.Control.RenderControl(HtmlTextWriter writer) +27<br>
    System.Web.UI.Page.ProcessRequestMain(Boolean
    includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)
    +5625</p>
  </blockquote>
  
  <p>Form Code:</p>
  
  <pre><code>&lt;asp:Content ID="BodyContent" ContentPlaceHolderID="MainContent" runat="server"&gt;
  
    &lt;h1&gt;An ASP.NET Form&lt;/h1&gt;
    &lt;br /&gt;&lt;br /&gt;
    Please submit your information using the form below if you would like to be enrolled in our monthly newsletter.
    &lt;br /&gt;&lt;br /&gt;
  
      &lt;form id="form1" runat="server"&gt;
          &lt;div class="text-right"&gt;
      Name: 
      &lt;asp:TextBox id="pName" runat="server" Width="200px" /&gt;
      &lt;asp:RequiredFieldValidator 
          id="reqName" 
          ControlToValidate="pName"
          Style="color:Red"   
          ErrorMessage="Please enter your name!"
          runat="server"/&gt;&lt;br&gt;
  
      E-mail Address: 
      &lt;asp:TextBox id="pEmail" runat="server" Width="200px" /&gt;
      &lt;asp:RegularExpressionValidator 
          id="ValidEmail" 
          ControlToValidate="pEmail" 
          Style="color:Red"
          ValidationExpression="^([a-zA-Z0-9_\-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([a-zA-Z0-9\-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$"
          ErrorMessage="Invalid Email Entry" 
          runat="server" /&gt;&lt;br&gt;
      &lt;asp:Button runat="server" text="Submit" OnClick="Unnamed1_Click" ID="Button1"/&gt;&lt;br&gt;
          &lt;/div&gt;
  
     &lt;/form&gt;
  
      &lt;asp:Label ID="Label1" runat="server" ForeColor="#FF6666"&gt;&lt;/asp:Label&gt;
  
      &lt;br /&gt;
      &lt;asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="&lt;%$ ConnectionStrings:ConnectionString %&gt;" SelectCommand="SELECT * FROM [Table]"&gt;&lt;/asp:SqlDataSource&gt;
  
  &lt;br /&gt;
  
  &lt;/asp:Content&gt;
  </code></pre>
  
  <p>C# code for database entry:</p>
  
  <pre><code>public partial class _Default : System.Web.UI.Page
  {
      SqlConnection con = new SqlConnection(@"Data Source = (LocalDB)\MSSQLLocalDB;AttachDbFilename=C:\Users\P\Docs\Visual Studio 2015\WebSites\WebSite2\App_Data\Database.mdf;Integrated Security = True");
  
      protected void Page_Load(object sender, EventArgs e)
      {
  
      }
      protected void Unnamed1_Click(object sender, EventArgs e)
      {
          con.Open();
          SqlCommand cmd = con.CreateCommand();
          cmd.CommandType = System.Data.CommandType.Text;
          cmd.CommandText = "insert into Table values('" + pName.Text + "','" + pEmail.Text + "')";
          cmd.ExecuteNonQuery();
  
          con.Close();
      }
  }
  </code></pre>
  
  <p>Table created in Microsoft Visual Studio has two items pName &amp; pEmail.</p>