这里我试图将值从表单插入到数据库中。当我填写表单并点击提交时,我收到一个错误窗口,说明在程序执行期间遇到致命错误。
这是我的代码。
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>
我该如何解决这个问题?我在这做错了什么?
答案 0 :(得分:1)
我可以清楚地看到您的插入查询错误。您的列和值不匹配。
所以我建议首先在mysql编辑器中运行该查询以检查插入查询的工作原理。
此外,您在参数中添加了未在查询中提及的值。