This is ASP. Net Student mark list sample project application
download here.
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;
public partial class MarkDetailsGridView : System.Web.UI.Page
{
SqlConnection conn;
protected static DataTable dr;
protected void Page_Load(object sender, EventArgs e)
{
GridView1.DataSource = Session["dr"];
string connStr = ConfigurationManager.ConnectionStrings["LocalSqlServer"].ConnectionString;
conn = new SqlConnection(connStr);
conn.Open();
//instance of a datatable
SqlCommand cmd = new SqlCommand();
SqlCommand sqlCommand = new SqlCommand("select Subject from SubjectMaster", conn);
SqlDataAdapter adp = new SqlDataAdapter(sqlCommand);
DataTable dtSubjects = new DataTable();
adp.Fill(dtSubjects);
//instance of a datarow
DataTable dtResults = new DataTable();
DataColumn dcol;
DataRow drow;
if (dtSubjects.Rows.Count > 0)
{
SqlCommand cmmd = new SqlCommand();
SqlCommand sqlCommand1 = new SqlCommand("select * from StudentMaster", conn);
SqlDataAdapter adp1 = new SqlDataAdapter(sqlCommand1);
DataTable dtStudents = new DataTable();
adp1.Fill(dtStudents);
dcol = new DataColumn("Name", typeof(string));
//adding datacolumn to datatable
dtResults.Columns.Add(dcol);
for (int j = 0; j < dtSubjects.Rows.Count; j++)
{
//creating two datacolums Column1 and Column2
dcol = new DataColumn(dtSubjects.Rows[j]["Subject"].ToString(), typeof(string));
//adding datacolumn to datatable
dtResults.Columns.Add(dcol);
}
//loop for 10 rows
for (int i = 0; i < dtStudents.Rows.Count; i++)
{
//instance of a datarow
drow = dtResults.NewRow();
//add rows to datatable
dtResults.Rows.Add(drow);
//add Column values
dtResults.Rows[i][0] = dtStudents.Rows[i][1].ToString();
//else
//{
// //add Column values
// dtResults.Rows[i][dcol] = dtSubjects.Rows[i][1].ToString();
//}
SqlCommand cmmand = new SqlCommand();
SqlCommand sqlCommandM = new SqlCommand("select Mark from MarkDetail where Student_ID='" + dtStudents.Rows[i]["Student_ID"].ToString() + "'", conn);
SqlDataAdapter adap = new SqlDataAdapter(sqlCommandM);
DataTable dtMark = new DataTable();
adap.Fill(dtMark);
int m = 0;
if (dtMark.Rows.Count > 0)
{
for (m = 0; m < dtSubjects.Rows.Count; m++)
{
if (m >= dtMark.Rows.Count)
{
dtResults.Rows[i][m + 1] = "N/A";
}
else
{
dtResults.Rows[i][m + 1] = dtMark.Rows[m]["Mark"].ToString();
}
}
}
else
{
for (m = 0; m < dtSubjects.Rows.Count; m++)
{
dtResults.Rows[i][m + 1] = "N/A";
}
}
}
}
//set gridView Datasource as dataTable dt.
GridView1.DataSource = dtResults;
//GridView1.DataSource = Session["dtResults"];
dr = dtResults;
Session["dr"] = dtResults;
//Bind Datasource to gridview
GridView1.DataBind();
}
protected void GridView1_PageIndexChanging(Object sender, GridViewPageEventArgs e)
{
GridView1.DataSource = dr;
GridView1.PageIndex = e.NewPageIndex;
GridView1.DataBind();
}
//protected void GridView1_Sorting(object sender, GridViewSortEventArgs e)
//{
// //Retrieve the table from the session object.
// DataTable dt = Session["dr"] as DataTable;
// if (dt != null)
// {
// //Sort the data.
// dt.DefaultView.Sort = e.SortExpression + " " + GetSortDirection(e.SortExpression);
// GridView1.DataSource = Session["dr"];
// GridView1.DataBind();
// }
//}
//private string GetSortDirection(string column)
//{
// // By default, set the sort direction to ascending.
// string sortDirection = "ASC";
// // Retrieve the last column that was sorted.
// string sortExpression = ViewState["SortExpression"] as string;
// if (sortExpression != null)
// {
// // Check if the same column is being sorted.
// // Otherwise, the default value can be returned.
// if (sortExpression == column)
// {
// string lastDirection = ViewState["SortDirection"] as string;
// if ((lastDirection != null) && (lastDirection == "ASC"))
// {
// sortDirection = "DESC";
// }
// }
// }
// // Save new values in ViewState.
// ViewState["SortDirection"] = sortDirection;
// ViewState["SortExpression"] = column;
// return sortDirection;
//}
//protected void GridView1_Sorting(object sender, GridViewSortEventArgs e)
//{
// GridView1.DataSource = GetData(e.SortExpression); // GetData(string sortBy) is a method that will get Data from Database/Cache/Session to display in Grid.
// GridView1.DataBind();
//}
private string ConvertSortDirectionToSql(SortDirection sortDirection)
{
string newSortDirection = String.Empty;
switch (sortDirection)
{
case SortDirection.Ascending:
newSortDirection = "ASC";
break;
case SortDirection.Descending:
newSortDirection = "DESC";
break;
}
return newSortDirection;
}
protected void GridView1_Sorting(object sender, GridViewSortEventArgs e)
{
//DataTable dataTable = gridView.DataSource as DataTable;
if (dr != null)
{
DataView dataView = new DataView(dr);
dataView.Sort = e.SortExpression + " " + ConvertSortDirectionToSql(e.SortDirection);
GridView1.DataSource = dataView;
GridView1.DataBind();
}
}
protected void MarklistBack_Click(object sender, EventArgs e)
{
Response.Redirect("MarkDetails.aspx");
}
}
<%@ Page Language="C#" MasterPageFile="~/MasterPage.master" AutoEventWireup="true"
CodeFile="MarkDetailsGridView.aspx.cs" Inherits="MarkDetailsGridView" %>
<%--<?xml version="1.0" encoding="ISO-8859-1" ?>--%>
<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" runat="Server">
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Untitled Page</title>
</head>
<body>
<%--<form id="form1" runat="server">--%>
<center>
<div style="width: 400px;">
<table border="1">
<tr>
<td style="width: 400px;">
<div>
<div>
<asp:HiddenField ID="HiddenField2" runat="server"></asp:HiddenField>
<center>
</center>
</div>
<center>
<table>
<tr>
<td>
<asp:GridView ID="GridView1" runat="server" HeaderStyle-BackColor="Crimson" HeaderStyle-ForeColor="Snow"
ForeColor="Black" EditRowStyle-BorderColor="Red" AlternatingRowStyle-BackColor="#EEEEEE"
GridLines="None" AllowPaging="true" PageSize="5" OnPageIndexChanging="GridView1_PageIndexChanging"
HeaderStyle-Width="100%" AllowSorting="True" OnSorting="GridView1_Sorting" CellPadding="5">
</asp:GridView>
</td>
</tr>
<tr>
<td align="right">
<asp:Button ID="btStudentBack" Text="Back" runat="server" OnClick="MarklistBack_Click" />
</td>
</tr>
</table>
</center>
</div>
</td>
</tr>
</table>
</div>
</center>
<%--</form>--%>
</body>
</html>
</asp:Content>
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;
using System.Text;
public partial class Mark : System.Web.UI.Page
{
SqlConnection conn;
StringBuilder strb = new StringBuilder();
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
string strconnection = ConfigurationManager.ConnectionStrings["LocalSqlServer"].ConnectionString;
SqlConnection con = new SqlConnection(strconnection);
con.Open();
SqlCommand cmd = new SqlCommand();
cmd.Connection = con;
cmd.CommandType = CommandType.Text;
cmd.CommandText = "select * from StudentMaster";
SqlDataAdapter adp = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
adp.Fill(dt);
DDLThird.DataSource = dt;
DDLThird.DataTextField = "Student_Name";
DDLThird.DataValueField = "Student_ID";
DDLThird.DataBind();
con.Close();
DDLThird.Items.Insert(0, "Select");
//DDLThird.Items.Add("0","Select");
}
}
protected void DDLThird_SelectedIndexChanged(object sender, EventArgs e)
{
String StudentID = DDLThird.SelectedItem.Text.ToString();
if (StudentID != null)
{
string connStr = ConfigurationManager.ConnectionStrings["LocalSqlServer"].ConnectionString;
conn = new SqlConnection(connStr);
conn.Open();
SqlCommand cmd6 = new SqlCommand();
cmd6.Connection = conn;
cmd6.CommandType = CommandType.Text;
cmd6.CommandText = "select * from SubjectMaster";
SqlDataAdapter adp = new SqlDataAdapter(cmd6);
DataTable dt = new DataTable();
adp.Fill(dt);
if (dt.Rows.Count > 0)
{
StringBuilder strb = new StringBuilder();
strb.Append("<br/><br/>");
strb.Append("<table border='1' cellpadding='3'>");
for (int i = 0; i < dt.Rows.Count; i++)
{
strb.Append("<tr><td>");
strb.Append(dt.Rows[i]["Subject"].ToString());
strb.Append("</td>");
strb.Append("<td>");
strb.Append("<input type=\"text\" name=\"subject\" id=\"txtSubject" + i.ToString() + "\" runat=\"server\" />");
strb.Append("<input type=\"hidden\" name=\"hidsubject\" id=\"hidSubject" + i.ToString() + "\" runat=\"server+\" value=" + dt.Rows[i]["Subject_ID"].ToString() + "/>");
strb.Append("</td></tr>");
}
//subject = subject + "," + dt.Rows[i]["Subject_ID"].ToString();
//hidSubject.Value = subject;
strb.Append("</table>");
divMark.InnerHtml = strb.ToString();
}
}
}
protected void btnAddMarks_Click(object sender, EventArgs e)
{
try
{
String StudentID = DDLThird.SelectedValue.ToString();
string marks = hidMarks.Value;
string strMarks = null;
strMarks = hidMarks.Value;
string strSubjects = hidSubject.Value;
string[] strMar = null;
string[] strSub = null;
int count = 0;
int id = 0;
char[] splitchar = { ',' };
strMar = strMarks.Split(splitchar);
strSub = strSubjects.Split(splitchar);
string connStr = ConfigurationManager.ConnectionStrings["LocalSqlServer"].ConnectionString;
conn = new SqlConnection(connStr);
string query6 = "Select @@Identity";
conn.Open();
for (count = 0; count < strMar.Length - 1; count++)
{
SqlCommand cmd8 = new SqlCommand("Insert INTO MarkDetail (Student_ID, Subject_ID , Mark) values (@Student_id, @Subject_id, @Mark)", conn);
cmd8.Parameters.AddWithValue("@Student_id", StudentID);
cmd8.Parameters.AddWithValue("@Subject_id", strSub[count]);
cmd8.Parameters.AddWithValue("@Mark", strMar[count]);
cmd8.ExecuteNonQuery();
cmd8.CommandText = query6;
id = Convert.ToInt32(cmd8.ExecuteScalar());
}
if (id != 0)
{
Response.Write("<script>alert('Successfully Saved')</script>");
}
else
{
Response.Write("<script>alert('Not saved')</script>");
}
}
catch (Exception ex)
{
Response.Write(ex.ToString() + "<br>");
}
finally
{
conn.Close();
}
Response.Redirect("MarkDetailsGridView.aspx");
}
protected void SubjectMaster_Click(object sender, EventArgs e)
{
Response.Redirect("SubjectMaster.aspx");
}
protected void GridViewNext_Click(object sender, EventArgs e)
{
Response.Redirect("MarkDetailsGridView.aspx");
}
}
<%@ Page Language="C#" MasterPageFile="~/MasterPage.master" AutoEventWireup="true"
CodeFile="MarkDetails.aspx.cs" Inherits="Mark" %>
<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" runat="Server">
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1">
<title>Mark Details</title>
</head>
<script type="text/javascript">
function GetMarkDetails()
{
var SubjectCount = document.getElementsByName("subject").length;
// alert(SubjectCount);
var value="";
var strSubjectID = "";
for(var i=0; i<SubjectCount; i++)
{
if(value + document.getElementById("txtSubject" + i).value == "")
{
alert("Please enter Your Marks");
return false;
}
value = value + document.getElementById("txtSubject" + i).value +",";
strSubjectID = strSubjectID + document.getElementById("hidSubject" + i).value.replace("/","") + ",";
// alert(strSubjectID)
}
document.getElementById("hidMarks").value=value;
document.getElementById("hidSubject").value=strSubjectID;
}
</script>
<body>
<%--<form id="form1" runat="server">--%>
<center>
<div>
<table>
<tr>
<td style="width: 400px;">
<div>
<center>
<table>
<tr>
<td>
<table border="1">
<tr style="background-color: Crimson; color: Snow;">
<th colspan="2">
Student Master
</th>
</tr>
<tr>
<td>
Student Name
</td>
<td>
<asp:DropDownList ID="DDLThird" runat="server" AutoPostBack="True" OnSelectedIndexChanged="DDLThird_SelectedIndexChanged"
DataTextField="Student_Name" DataValueField="Student_ID">
<asp:ListItem Text="Select" Value=""></asp:ListItem>
</asp:DropDownList>
</td>
</tr>
</table>
<div>
<asp:ScriptManager ID="ShowSubjectAlert" runat="server" />
</div>
</td>
</tr>
<tr align="right">
<td colspan="2">
<input type="hidden" name="hidden1" id="hidMarks" value="" runat="server" />
<input type="hidden" name="hidden2" id="hidSubject" value="" runat="server" />
</td>
</tr>
</table>
</center>
</div>
<center>
<div id="divMark" runat="server">
</div>
<table>
<tr>
<td>
<asp:Button ID="btnAddMarks" Text="Add Marks" runat="server" OnClientClick="GetMarkDetails()"
OnClick="btnAddMarks_Click" Visible="true"></asp:Button>
</td>
<td>
<asp:Button ID="btSubjectMaster" Text="Back" runat="server" OnClick="SubjectMaster_Click" /><asp:Button
ID="btGridview" Text="Next" runat="server" OnClick="GridViewNext_Click" />
</td>
</tr>
</table>
</center>
</td>
</tr>
</table>
</div>
</center>
<%-- </form>--%>
</body>
</html>
</asp:Content>
No comments:
Post a Comment