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

本文关键字:遇到 致命错误 执行程序 c#asp net | 更新日期: 2023-09-27 18:20:09

在这里,我正试图将值从表单插入数据库。当我填写表单并点击提交时,我会收到一个错误窗口,显示在程序执行过程中遇到的致命错误。

这是我的密码。

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>

我该如何解决这个问题?我在这里做错了什么?

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

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

因此,我建议先在mysql编辑器中运行该查询,以检查insert查询是否有效。

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