c#asp.net程序执行过程中遇到致命错误

问题描述:

这里我试图从表单插入值到database.when我填写表单并提交,我得到一个错误窗口说在程序执行过程中遇到致命错误。c#asp.net程序执行过程中遇到致命错误

这是我的代码。

using System; 
using System.Collections.Generic; 
using System.Configuration; 
using System.Data; 
using System.Linq; 
using System.Drawing; 
using System.Web; 
using System.Web.UI; 
using System.Web.UI.WebControls; 
using MySql.Data.MySqlClient; 


public partial class dashboard : System.Web.UI.Page 
{ 
    MySqlConnection conn = new MySqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString); 
    protected void Page_Load(object sender, EventArgs e) 
    { 

    } 

    protected void submit_Click(object sender, EventArgs e) 
    { 
     var b = RadioButtonList1.SelectedItem.ToString(); 
     try 
     { 
      conn.Open(); 
      MySqlCommand cmdo = new MySqlCommand("Insert into application (applicant_fullname,applicant_age,applicant_dob,applicant_gender,applicant_residential_address) values (@Name,@password,@Email)", conn); 
      cmdo.Parameters.Clear(); 
      cmdo.Parameters.AddWithValue("@fullname", fullname.Text); 
      cmdo.Parameters.AddWithValue("@age", age.Text); 
      cmdo.Parameters.AddWithValue("@dob", dateofbirth.Text); 
      cmdo.Parameters.AddWithValue("@gender",b); 
      cmdo.Parameters.AddWithValue("@resident", resident.Text); 
      cmdo.ExecuteNonQuery(); 
      cmdo.Parameters.Clear(); 
      cmdo.Dispose(); 
      ShowMessage("Registered successfully......!"); 
     } 
     catch (MySqlException ex) 
     { 
      ShowMessage(ex.Message); 
     } 

     finally 
     { 
      conn.Close(); 
     } 

    } 

    void ShowMessage(string msg) 
    { 
     ClientScript.RegisterStartupScript(Page.GetType(), "validation", "<script language='javascript'>alert('" + msg + "');</script>"); 
    } 

} 

ASP代码:

<form id="form1" runat="server"> 
     <table class="applytable"> 
     <tr><td>Full name</td><td><asp:TextBox ID="fullname" runat="server" Height="25px" Width="155px"></asp:TextBox></td></tr> 
     <tr><td>Age</td><td><asp:TextBox ID="age" runat="server" Height="25px" Width="155px"></asp:TextBox></td></tr> 
     <tr><td>Date Of Birth</td><td><asp:TextBox ID="dateofbirth" runat="server" Height="25px" Width="155px"></asp:TextBox></td></tr> 
     <tr><td>Gender</td><td><asp:RadioButtonList ID="RadioButtonList1" runat="server"> 
      <asp:ListItem Value="Male"></asp:ListItem> 
      <asp:ListItem Value="Female"></asp:ListItem> 
     </asp:RadioButtonList></td></tr> 
     <tr><td>Residential Address</td><td><asp:TextBox id="resident" TextMode="multiline" Columns="50" Rows="5" 
     runat="server" Height="58px" Width="194px" /></td></tr> 
     <tr><td>Permanant Address</td><td><asp:TextBox id="permenant" TextMode="multiline" Columns="50" Rows="5" 
      runat="server" Height="68px" Width="192px" /></td></tr> 
     <tr><td>Parent/Guardian name</td><td><asp:TextBox ID="parentname" runat="server" Height="25px" Width="155px"></asp:TextBox></td></tr> 
     <tr><td>E-mail Address</td><td><asp:TextBox ID="emailid" runat="server" Height="25px" Width="155px"></asp:TextBox></td></tr> 
     <tr><td>Phone Number</td><td><asp:TextBox ID="phoneno" runat="server" Height="25px" Width="155px"></asp:TextBox></td></tr> 
     <tr><td></td><td><asp:Button ID="submitpage1" runat="server" 
       onclick="submit_Click" Text="Submit" /></td></tr>   
    </form> 

我怎么能解决这个问题?我在这里做错了什么?

我可以清楚地看到您的插入查询是错误的。您的列和值不匹配。

所以我建议先在mysql编辑器中运行该查询来检查插入查询的作品。

此外,您正在参数中添加未在您的查询中提到的值。