You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

ASP.NET WebForms患者注册数据无法保存至SQL Server求助

患者注册ASP.NET WebForms应用数据无法保存问题

我正在开发一款患者注册的ASP.NET WebForms应用,要求ID和注册号唯一且自动生成。HTML与C#代码运行无报错,但提交表单后数据无法保存,也无任何提示。


HTML标记

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" 
         Inherits="Formtask.WebForm1" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <style>
        .parent{
            align-items: center;
            border: 1px solid black;
            display: contents;
            justify-content: center;
            height: 150px;
            width:contain;
        }
         .margin {
             border: 1px solid black;
             margin-bottom: 2px;
         }
         .button {
              background-color: lightblue;
              border: none;
              color: black;
              padding: 15px 32px;
              text-align: center;
              text-decoration: none;
              display: inline-block;
              font-size: 16px;
         }
         .div {
              
             height: 5px;
             align-items: center;
         }
    </style>
</head>
<body>
    <form id="form1" runat="server">
    <div class="parent">
        <div>
           First Name: <input type="text" id="fname" class="margin" style="text-indent:20px;margin-left:17px;"/>&nbsp;&nbsp;&nbsp;&nbsp;
           Last Name: <input type="text" id="lname" class="margin" style="text-indent:20px; margin-left:19px;"/>
        </div>
        <div>
           Address: <input type="text" id="add" class="margin" style="text-indent:20px; margin-left:36px;"/>&nbsp;&nbsp;&nbsp;&nbsp;
           Contact No.: <input type="text" id="ph" class="margin" style="text-indent:20px; margin-left:11px;"/>
        </div>
        <div>
           Date: <input type="date" id="date" class="margin" style="text-indent:20px; margin-left:58px;"/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
           Time: <input type="time" id="time" class="margin" style="text-indent:20px; margin-left:57px;"/>
        </div>
        <div>
           Age: <input type="text" id="age" class="margin" style="text-indent:20px; margin-left:62px;"/>&nbsp;&nbsp;&nbsp;&nbsp;
           Status: <select id="st" style="margin-left:50px;"><option value="stable">Stable</option><option value="critical">Critical</option></select>

        </div>
        <div>
            <input type="submit" id="btn" value="Submit" style="margin-left:250px; height: 34px; width: 111px; margin-top: 10px;"/></div>
        </div>
    
    </form>
</body>
</html>

C#代码

using System;
using System.Configuration;
using System.Data.SqlClient;
using System.Xml.Linq;

namespace Formtask
{
    public partial class WebForm1 : System.Web.UI.Page
    {
        protected void btn(object sender, EventArgs e)
        {
            string connectionString = @"Data Source=LAPTOP-VN6JLQTP\SQLEXPRESS;Initial Catalog=webform;Integrated Security=True;Trust Server Certificate=True                                                                                                                                                                                                                                                                                                                                                                                                                               ";

            using (SqlConnection con = new SqlConnection(connectionString))
            {
                string query = "INSERT INTO datain (FirstName, LastName, Address, ContactNo, Date, Time, Age, Status) VALUES (@FirstName, @LastName, @Address, @ContactNo, @Date, @Time, @Age, @Status)";

                using (SqlCommand cmd = new SqlCommand(query, con))
                {
                    cmd.Parameters.AddWithValue("@FirstName", Request.Form["fname"]);
                    cmd.Parameters.AddWithValue("@LastName", Request.Form["lname"]);
                    cmd.Parameters.AddWithValue("@Address", Request.Form["add"]);
                    cmd.Parameters.AddWithValue("@ContactNo", Request.Form["ph"]);
                    cmd.Parameters.AddWithValue("@Date", Request.Form["date"]);
                    cmd.Parameters.AddWithValue("@Time", Request.Form["time"]);
                    cmd.Parameters.AddWithValue("@Age", Request.Form["age"]);
                    cmd.Parameters.AddWithValue("@Status", Request.Form["st"]);

                    con.Open();
                    cmd.ExecuteNonQuery();
                }
            }
            // Optionally, you can redirect the user to another page after submission
            // Response.Redirect("SuccessPage.aspx");
        }
    }
}

SQL查询语句

CREATE DATABASE webform;

CREATE TABLE datain 
(
    ID INT PRIMARY KEY IDENTITY(1,1),
    PatientRegistrationNo NVARCHAR(50) 
         DEFAULT CONCAT('REG', REPLACE(CONVERT(NVARCHAR(36), NEWID()), '-', '')),
    FirstName NVARCHAR(50),
    LastName NVARCHAR(50),
    Address NVARCHAR(100),
    ContactNo NVARCHAR(20),
    Date DATE,
    Time TIME,
    Age INT,
    Status NVARCHAR(50)
);

SELECT * FROM datain;

问题根源及修复方案

1. 提交按钮未绑定后台事件

ASP.NET WebForms中,普通HTML提交按钮不会自动触发后台方法,需改为服务器控件并绑定事件:
修改提交按钮代码,添加runat="server"和OnClick="btn"属性:

<input type="submit" id="btn" runat="server" OnClick="btn" value="Submit" style="margin-left:250px; height: 34px; width: 111px; margin-top: 10px;"/>

2. 表单控件缺少name属性

Request.Form通过控件的name属性获取值,当前所有输入控件只有id,需补充name属性(与id保持一致):

First Name: <input type="text" id="fname" name="fname" class="margin" style="text-indent:20px;margin-left:17px;"/>
Last Name: <input type="text" id="lname" name="lname" class="margin" style="text-indent:20px; margin-left:19px;"/>
Address: <input type="text" id="add" name="add" class="margin" style="text-indent:20px; margin-left:36px;"/>
Contact No.: <input type="text" id="ph" name="ph" class="margin" style="text-indent:20px; margin-left:11px;"/>
Date: <input type="date" id="date" name="date" class="margin" style="text-indent:20px; margin-left:58px;"/>
Time: <input type="time" id="time" name="time" class="margin" style="text-indent:20px; margin-left:57px;"/>
Age: <input type="text" id="age" name="age" class="margin" style="text-indent:20px; margin-left:62px;"/>
Status: <select id="st" name="st" style="margin-left:50px;"><option value="stable">Stable</option><option value="critical">Critical</option></select>

3. 添加异常捕获与数据类型校验

原代码无异常处理,出错无提示,同时需确保参数类型与数据库字段匹配:
修改C#代码如下:

protected void btn(object sender, EventArgs e)
{
    string connectionString = @"Data Source=LAPTOP-VN6JLQTP\SQLEXPRESS;Initial Catalog=webform;Integrated Security=True;Trust Server Certificate=True";

    try
    {
        using (SqlConnection con = new SqlConnection(connectionString))
        {
            string query = "INSERT INTO datain (FirstName, LastName, Address, ContactNo, Date, Time, Age, Status) VALUES (@FirstName, @LastName, @Address, @ContactNo, @Date, @Time, @Age, @Status)";

            using (SqlCommand cmd = new SqlCommand(query, con))
            {
                cmd.Parameters.AddWithValue("@FirstName", Request.Form["fname"]);
                cmd.Parameters.AddWithValue("@LastName", Request.Form["lname"]);
                cmd.Parameters.AddWithValue("@Address", Request.Form["add"]);
                cmd.Parameters.AddWithValue("@ContactNo", Request.Form["ph"]);
                cmd.Parameters.AddWithValue("@Date", DateTime.Parse(Request.Form["date"]));
                cmd.Parameters.AddWithValue("@Time", TimeSpan.Parse(Request.Form["time"]));
                cmd.Parameters.AddWithValue("@Age", int.Parse(Request.Form["age"]));
                cmd.Parameters.AddWithValue("@Status", Request.Form["st"]);

                con.Open();
                int rowsAffected = cmd.ExecuteNonQuery();
                if(rowsAffected > 0)
                {
                    Response.Write("<script>alert('数据保存成功!');</script>");
                }
                else
                {
                    Response.Write("<script>alert('数据保存失败,请重试!');</script>");
                }
            }
        }
    }
    catch(Exception ex)
    {
        Response.Write($"<script>alert('出错了:{ex.Message}');</script>");
    }
}

4. 验证数据库连接配置

确认连接字符串中的SQL Server实例名称(LAPTOP-VN6JLQTP\SQLEXPRESS)与实际环境一致,且webform数据库已正确创建。


内容的提问来源于stack exchange,提问作者AB DJ jan

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.23 19:42:01