I'm working on an ASP.NET webforms app which is for patient registration 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 HTML markup:
<%@ 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>
This is the 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-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");
}
}
}
Here is the 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 me solve this problem.
Thanks!
You should use server side controls.
Hence this markup:
<div class="parent">
<div>
First Name:
<asp:TextBox runat="server"
ID="fname" class="margin" Style="text-indent: 20px; margin-left: 17px;">
</asp:TextBox>
Last Name:
<asp:TextBox runat="server"
ID="lname" class="margin" Style="text-indent: 20px; margin-left: 19px;">
</asp:TextBox>
</div>
<div>
Address:
<asp:TextBox runat="server"
id="add" class="margin" style="text-indent: 20px; margin-left: 36px;" >
</asp:TextBox>
Contact No.:
<asp:TextBox runat="server"
id="ph" class="margin" style="text-indent: 20px; margin-left: 11px;">
</asp:TextBox>
</div>
<div>
Date:
<asp:TextBox runat="server"
id="date" TextMode="Date"
class="margin" style="margin-left: 58px;" >
</asp:TextBox>
Time:
<asp:TextBox runat="server"
id="time" TextMode="Time"
class="margin" style="text-indent: 20px; margin-left: 57px;">
</asp:TextBox>
</div>
<div>
Age:
<asp:TextBox runat="server"
id="age" class="margin" style="text-indent: 20px; margin-left: 62px;" >
</asp:TextBox>
Status:
<asp:DropDownList runat="server"
id="st" style="margin-left: 50px;">
<asp:ListItem>Stable</asp:ListItem>
<asp:ListItem>Critical</asp:ListItem>
</asp:DropDownList>
</div>
<div>
<asp:Button ID="btn" runat="server"
Text="Submit"
style="margin-left: 250px; height: 34px; width: 111px; margin-top: 10px;"
OnClick="btn_Click"
/>
</div>
And now the code behind becomes this:
protected void btn_Click(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.Add("@FirstName", SqlDbType.NVarChar).Value = fname.Text;
cmd.Parameters.Add("@LastName", SqlDbType.NVarChar).Value = lname.Text;
cmd.Parameters.Add("@Address", SqlDbType.NVarChar).Value = add.Text;
cmd.Parameters.Add("@ContactNo", SqlDbType.NVarChar).Value = ph.Text;
cmd.Parameters.Add("@Date", SqlDbType.Date).Value = date.Text;
cmd.Parameters.Add("@Time", SqlDbType.Time).Value = time.Text;
cmd.Parameters.Add("@Age", SqlDbType.Int).Value = age.Text;
cmd.Parameters.Add("@Status", SqlDbType.NVarChar).Value = st.Text;
con.Open();
cmd.ExecuteNonQuery();
}
}
}