I’m work on a webform which is for patient registration form where ID and registration no should be unique and auto generated. my HTML and C# is code is running and no error is displaying but the real problem in saving data when i fill the form it shows no error whether the data is saved or not.
Here is the code of 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;"/>
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;"/>
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;"/>
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;"/>
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>``
`
here is C# code:
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-VN6JLQTPSQLEXPRESS;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");
}
}
}
Here is SQL query:
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;
Please help to solve this problem.
Thanks!
i pasted all the problem in above section i want to save data in sql database and showing that data in grid view so i need help on it.
AB DJ jan is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.