无法使用C#在ASP.NET中插入数据库 - 堆栈空

时间:2016-10-16 23:01:42

标签: c# asp.net

我正在尝试使用Microsoft Visual Studio将用户数据插入到数据库文件中。我用asp.net编写了一个表单,将数据库添加到目录,并创建了c#命令将数据插入到数据库中,但是我得到的堆栈是空的错误。

  

执行期间生成了未处理的异常   当前的网络请求。有关的来源和位置的信息   可以使用下面的异常堆栈跟踪来识别异常。

     

堆栈追踪:

     

[InvalidOperationException:Stack empty。]
  System.ThrowHelper.ThrowInvalidOperationException(ExceptionResource   资源)+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适配器)+169
  System.Web.UI.Control.RenderControl(HtmlTextWriter writer,   ControlAdapter适配器)+287
  System.Web.UI.Control.RenderControl(HtmlTextWriter writer)+27
  System.Web.UI.Page.ProcessRequestMain(布尔   includeStagesBeforeAsyncPoint,Boolean includeStagesAfterAsyncPoint)   5625

表格代码:

<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#代码:

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

在Microsoft Visual Studio中创建的表有两个项目pName&amp; pEmail。

0 个答案:

没有答案