0

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.

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.

Stack Trace:

[InvalidOperationException: Stack empty.]
System.ThrowHelper.ThrowInvalidOperationException(ExceptionResource resource) +52 System.Collections.Generic.Stack`1.Pop() +6856361
Microsoft.VisualStudio.Web.PageInspector.Runtime.WebForms.SelectionMappingRenderTraceListener.EndRendering(TextWriter writer, Object renderedObject) +85
System.Web.UI.RenderTraceListenerList.EndRendering(TextWriter writer, Object renderedObject) +66
System.Web.UI.Control.RenderControlInternal(HtmlTextWriter writer, ControlAdapter adapter) +169
System.Web.UI.Control.RenderControl(HtmlTextWriter writer, ControlAdapter adapter) +287
System.Web.UI.Control.RenderControl(HtmlTextWriter writer) +27
System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +5625

Form Code:

<asp:Content ID="BodyContent" ContentPlaceHolderID="MainContent" runat="server">

  <h1>An ASP.NET Form</h1>
  <br /><br />
  Please submit your information using the form below if you would like to be enrolled in our monthly newsletter.
  <br /><br />

    <form id="form1" runat="server">
        <div class="text-right">
    Name: 
    <asp:TextBox id="pName" runat="server" Width="200px" />
    <asp:RequiredFieldValidator 
        id="reqName" 
        ControlToValidate="pName"
        Style="color:Red"   
        ErrorMessage="Please enter your name!"
        runat="server"/><br>

    E-mail Address: 
    <asp:TextBox id="pEmail" runat="server" Width="200px" />
    <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" /><br>
    <asp:Button runat="server" text="Submit" OnClick="Unnamed1_Click" ID="Button1"/><br>
        </div>

   </form>

    <asp:Label ID="Label1" runat="server" ForeColor="#FF6666"></asp:Label>

    <br />
    <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:ConnectionString %>" SelectCommand="SELECT * FROM [Table]"></asp:SqlDataSource>

<br />

</asp:Content>

C# code for database entry:

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();
    }
}

Table created in Microsoft Visual Studio has two items pName & pEmail.

NineBerry
  • 26,306
  • 3
  • 62
  • 93
penmas
  • 957
  • 3
  • 13
  • 21

0 Answers0