How to insert dynamic GridView selected rows to database.
using System;
using System.Data;
using System.Data.SqlClient;
using System.Web.UI.WebControls;
using System.Configuration;
namespace ReceiptEntry
{
public partial class ReceiptEntry : System.Web.UI.Page
{
private readonly string connectionString = ConfigurationManager.ConnectionStrings["ReceiptEntryDB"].ConnectionString;
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
// Bind the CheckBoxList with fee headers from the database
if (chkFeesHeader.Items.Count == 0)
{
BindFeeHeaders();
}
}
}
protected void btnSave_Click(object sender, EventArgs e)
{
int rollNo;
if (!int.TryParse(txtRollNo.Text, out rollNo))
{
return;
}
string name = txtName.Text;
string department = txtDepartment.Text;
int batchYear;
if (!int.TryParse(txtBatchYear.Text, out batchYear))
{
return;
}
InsertStudentData(rollNo, name, department, batchYear);
foreach (ListItem item in chkFeesHeader.Items)
{
if (item.Selected)
{
string feeHeader = item.Text;
int feeAmount = GetFeeAmountFromDatabase(feeHeader);
InsertFeeData(rollNo, feeHeader, feeAmount);
}
}
{
GridView1.DataBind();
}
}
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
int feeID = Convert.ToInt32(GridView1.DataKeys[e.RowIndex].Value);
TextBox txtPaidAmount = (TextBox)GridView1.Rows[e.RowIndex].FindControl("txtFees");
int paidAmount = Convert.ToInt32(txtPaidAmount.Text);
UpdatePaidAmount(feeID, paidAmount);
GridView1.DataBind();
}
private void BindFeeHeaders()
{
using (SqlConnection connection = new SqlConnection(connectionString))
{
connection.Open();
using (SqlCommand cmd = new SqlCommand("SELECT FeeHeader FROM tbl_FeeHeaders", connection))
{
using (SqlDataReader reader = cmd.ExecuteReader())
{
while (reader.Read())
{
ListItem item = new ListItem(reader["FeeHeader"].ToString());
chkFeesHeader.Items.Add(item);
}
}
}
}
}
private void InsertStudentData(int rollNo, string name, string department, int batchYear)
{
using (SqlConnection connection = new SqlConnection(connectionString))
{
connection.Open();
using (SqlCommand cmd = new SqlCommand("sp_InsertStudent", connection))
{
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@RollNo", rollNo);
cmd.Parameters.AddWithValue("@Name", name);
cmd.Parameters.AddWithValue("@Department", department);
cmd.Parameters.AddWithValue("@BatchYear", batchYear);
cmd.ExecuteNonQuery();
}
}
}
private void InsertFeeData(int rollNo, string feeHeader, int feeAmount)
{
using (SqlConnection connection = new SqlConnection(connectionString))
{
connection.Open();
using (SqlCommand cmd = new SqlCommand("sp_InsertFee", connection))
{
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@RollNo", rollNo);
cmd.Parameters.AddWithValue("@FeeHeader", feeHeader);
cmd.Parameters.AddWithValue("@FeeAmount", feeAmount);
cmd.ExecuteNonQuery();
}
}
}
private void UpdatePaidAmount(int feeID, int paidAmount)
{
using (SqlConnection connection = new SqlConnection(connectionString))
{
connection.Open();
using (SqlCommand cmd = new SqlCommand("sp_UpdateFee", connection))
{
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@FeeID", feeID);
cmd.Parameters.AddWithValue("@PaidAmount", paidAmount);
cmd.ExecuteNonQuery();
}
}
}
private int GetFeeAmountFromDatabase(string feeHeader)
{
int feeAmount = 0; // Initialize with 0
using (SqlConnection connection = new SqlConnection(connectionString))
{
connection.Open();
using (SqlCommand cmd = new SqlCommand("SELECT FeeAmount FROM tbl_FeeHeaders WHERE FeeHeader = @FeeHeader", connection))
{
cmd.Parameters.AddWithValue("@FeeHeader", feeHeader);
using (SqlDataReader reader = cmd.ExecuteReader())
{
if (reader.Read())
{
feeAmount = Convert.ToInt32(reader["FeeAmount"]);
}
}
}
}
return feeAmount;
}
}
}
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="ReceiptEntry.aspx.cs" Inherits="ReceiptEntry.ReceiptEntry" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Receipt Entry</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<!-- Student Information -->
<label>Roll No:</label>
<asp:TextBox ID="txtRollNo" runat="server"></asp:TextBox><br />
<label>Name:</label>
<asp:TextBox ID="txtName" runat="server"></asp:TextBox><br />
<label>Department:</label>
<asp:TextBox ID="txtDepartment" runat="server"></asp:TextBox><br />
<label>Batch Year:</label>
<asp:TextBox ID="txtBatchYear" runat="server"></asp:TextBox><br />
<!-- Fee Headers -->
<label>Fees Header:</label>
<asp:CheckBoxList ID="chkFeesHeader" runat="server" RepeatDirection="Horizontal">
<asp:ListItem>Admission</asp:ListItem>
<asp:ListItem>Tuition</asp:ListItem>
<asp:ListItem>Others</asp:ListItem>
</asp:CheckBoxList><br />
<!-- Save Button -->
<asp:Button ID="btnSave" runat="server" Text="Save" OnClick="btnSave_Click" /><br />
<!-- GridView to display fee data -->
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false" DataKeyNames="FeeID"
OnRowUpdating="GridView1_RowUpdating">
<Columns>
<asp:BoundField DataField="SNO" HeaderText="S NO" />
<asp:BoundField DataField="Select" HeaderText="Select" />
<asp:BoundField DataField="FeeHeader" HeaderText="Fees Header" />
<asp:TemplateField HeaderText="Fees">
<ItemTemplate>
<asp:TextBox ID="txtFees" runat="server" Text='<%# Bind("FeeAmount") %>'></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Paid">
<ItemTemplate>
<asp:Label ID="lblPaid" runat="server" Text='<%# Bind("PaidAmount") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Balance">
<ItemTemplate>
<asp:Label ID="lblBalance" runat="server" Text='<%# Eval("Balance") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="To Be Paid">
<ItemTemplate>
<asp:TextBox ID="txtToBePaid" runat="server" Text='<%# Bind("ToBePaid") %>'></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</div>
</form>
</body>
</html>
CREATE TABLE tbl_Students (
RollNo INT PRIMARY KEY,
Name NVARCHAR(255),
Department NVARCHAR(255),
BatchYear INT
);
CREATE TABLE tbl_Fees (
FeeID INT PRIMARY KEY IDENTITY(1,1),
RollNo INT,
FeeHeader NVARCHAR(255),
FeeAmount INT,
PaidAmount INT,
CONSTRAINT FK_Student FOREIGN KEY (RollNo) REFERENCES tbl_Students(RollNo)
);
-----------------------------------
CREATE PROCEDURE sp_InsertFee
@RollNo INT,
@FeeHeader NVARCHAR(255),
@FeeAmount INT,
@PaidAmount INT
AS
BEGIN
INSERT INTO tbl_Fees (RollNo, FeeHeader, FeeAmount, PaidAmount)
VALUES (@RollNo, @FeeHeader, @FeeAmount, @PaidAmount)
END;
----------------
CREATE PROCEDURE sp_UpdateFee
@FeeID INT,
@PaidAmount INT,
@FeeAmount INT
AS
BEGIN
UPDATE tbl_Fees
SET PaidAmount = @PaidAmount,
FeeAmount = @FeeAmount
WHERE FeeID = @FeeID;
END;
CREATE PROCEDURE sp_InsertStudent
@RollNo INT,
@Name NVARCHAR(255),
@Department NVARCHAR(255),
@BatchYear INT
AS
BEGIN
INSERT INTO tbl_Students (RollNo, Name, Department, BatchYear)
VALUES (@RollNo, @Name, @Department, @BatchYear);
END;
---------------------
CREATE TABLE tbl_FeeHeaders
(
FeeHeaderID INT PRIMARY KEY IDENTITY(1,1),
FeeHeader NVARCHAR(255) NOT NULL,
FeeAmount INT NOT NULL
);
INSERT INTO tbl_FeeHeaders (FeeHeader, FeeAmount)
VALUES
('Admission', 1000),
('Tuition', 2000),
('Others', 500);
select * from tbl_Fees
insert into tbl_Fees (RollNo,FeeHeader,FeeAmount,PaidAmount) values (555,'Admission',5000,3000);