You are on page 1of 5

using System;

using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Web.UI.WebControls;
using System.Linq;
using System.Web.UI;

namespace Student_Registration
{
public partial class Std_Reg : System.Web.UI.Page

{
//string cs =
ConfigurationManager.ConnectionStrings["DBCS"].ConnectionString;
//System.Data.SqlClient.SqlConnection con = new SqlConnection(@"Data
Source=DESKTOP-QSOMF5J;Initial Catalog=StudentRegistration;Integrated
Security=True");
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
Gvbind();
loadGrid();
}
}
private void loadGrid()
{

}
protected void btnsave_Click(object sender, EventArgs e)
{
using (DataClassesDataContext dbstd = new DataClassesDataContext())
{
StudentInfo std = new StudentInfo
{
Address = txtAddress.Text,
Cellno = txtCellno.Text,
Country = drpCountry.SelectedValue,
DOB = Convert.ToDateTime(txtDOB.Text),
Program = txtProgram.Text,
StudentName = txtStudentName.Text,
FatherName = txtFatherName.Text,
Gender = radGender.SelectedValue
};
dbstd.StudentInfos.InsertOnSubmit(std);
dbstd.SubmitChanges();
}

// // SqlCommand cmd = new SqlCommand(@"INSERT INTO [dbo].


[StudentInfo]
// // ([StudentName]
// // ,[FatherName]
// // ,[DOB]
// // ,[Address]
// // ,[Cellno]
// // ,[Program]
// // ,[Country]
// // ,[Gender])
// //VALUES
// // ('" + txtStudentName.Text + "','" + txtFatherName.Text +
"','" + txtDOB.Text + "','" + txtAddress.Text + "','" + txtCellno.Text + "','" +
txtProgram.Text + "','" + drpCountry.SelectedValue + "','" +
radGender.SelectedValue + "')", con);
// // con.Open();
// // cmd.ExecuteNonQuery();
// // con.Close();
// this.Gvbind();
// Response.Redirect(Request.Url.AbsoluteUri);
}
protected void Button1_Click(object sender, EventArgs e)
{
Button btn = sender as Button;
string btnName = btn.Text;
using (DataClassesDataContext dbstd = new DataClassesDataContext())
{
if (!string.IsNullOrWhiteSpace(hfId.Value))
{
StudentInfo std = (from c in dbstd.StudentInfos where c.Id ==
Convert.ToInt16(hfId.Value) select c).FirstOrDefault();
std.Address = txtAddress.Text;
std.Cellno = txtCellno.Text;
std.Country = drpCountry.SelectedValue;
std.DOB = Convert.ToDateTime(txtDOB.Text);
std.FatherName = txtFatherName.Text;
std.StudentName = txtStudentName.Text;
std.Program = txtProgram.Text;
std.Gender = radGender.SelectedValue;
dbstd.SubmitChanges();
hfId.Value = "";
Response.Write("Data Updated Successfully");
Gvbind();
}
else
{
StudentInfo std = new StudentInfo
{
Address = txtAddress.Text,
Cellno = txtCellno.Text,
Country = drpCountry.SelectedValue,
DOB = Convert.ToDateTime(txtDOB.Text),
Program = txtProgram.Text,
StudentName = txtStudentName.Text,
FatherName = txtFatherName.Text,
Gender = radGender.SelectedValue
};
dbstd.StudentInfos.InsertOnSubmit(std);
dbstd.SubmitChanges();
Response.Write("Data Saved Successfully");
Gvbind();
}
}
//System.Data.SqlClient.SqlConnection con = new SqlConnection(@"Data
Source=DESKTOP-QSOMF5J;Initial Catalog=StudentRegistration;Integrated
Security=True");
//SqlCommand cmd = new SqlCommand(@"UPDATE [dbo].[StudentInfo]
//SET[StudentName] = '" + txtStudentName.Text + "',[FatherName] = '" +
txtFatherName.Text + "',[DOB] = '" + txtDOB.Text + "',[Address] = '" +
txtAddress.Text + "',[Cellno] = '" + txtCellno.Text + "',[Program] = '" +
txtProgram.Text + "',[Country] = '" + drpCountry.SelectedValue + "',[Gender] = '" +
radGender.SelectedValue + "' WHERE [StudentName]='" + txtStudentName.Text + "' ",
con);
//con.Open();
//cmd.ExecuteNonQuery();

Response.Redirect(Request.Url.AbsoluteUri);
}
protected void Gvbind()
{
using (DataClassesDataContext dbstd = new DataClassesDataContext())
{
Gridview1.DataSource = from StudentInfo in dbstd.StudentInfos
select StudentInfo;
Gridview1.DataBind();
}
}
protected void Gridview1_PageIndexChanging(object sender,
GridViewPageEventArgs e)
{
FillGrid();
Gridview1.PageIndex = e.NewPageIndex;
Gvbind();
}
private void FillGrid()
{

}
protected void Gridview1_RowEditing(object sender, GridViewEditEventArgs e)
{
btnsave.Text = "Update";
e.Cancel = true;
hfId.Value =
((Label)Gridview1.Rows[e.NewEditIndex].FindControl("Label15")).Text;
txtStudentName.Text =
((Label)Gridview1.Rows[e.NewEditIndex].FindControl("Label2")).Text;
txtFatherName.Text =
((Label)Gridview1.Rows[e.NewEditIndex].FindControl("Label3")).Text;
txtDOB.Text =
Convert.ToDateTime( ((Label)Gridview1.Rows[e.NewEditIndex].FindControl("Label4")).T
ext).ToString("yyyy-MM-dd");
txtProgram.Text =
((Label)Gridview1.Rows[e.NewEditIndex].FindControl("Label7")).Text;
txtCellno.Text =
((Label)Gridview1.Rows[e.NewEditIndex].FindControl("Label5")).Text;
txtAddress.Text =
((Label)Gridview1.Rows[e.NewEditIndex].FindControl("Label6")).Text;
drpCountry.Text =
((Label)Gridview1.Rows[e.NewEditIndex].FindControl("Label8")).Text;
var valuetoBindGenderCtrl =
((Label)Gridview1.Rows[e.NewEditIndex].FindControl("Label1")).Text.Trim();
radGender.SelectedValue = valuetoBindGenderCtrl;
Gvbind();

}
protected void Gridview1_RowDeleting(object sender, GridViewDeleteEventArgs
e)
{
int Id = Convert.ToInt32(Gridview1.DataKeys[e.RowIndex].Values[0]);
using (DataClassesDataContext dbstd = new DataClassesDataContext())
{
StudentInfo std = (from c in dbstd.StudentInfos where c.Id == Id
select c).FirstOrDefault();
dbstd.StudentInfos.DeleteOnSubmit(std);
dbstd.SubmitChanges();

}
Gvbind();
}

protected void Gridview1_RowCancelingEdit(object sender,


GridViewCancelEditEventArgs e)
{
Gridview1.EditIndex = -1;
Gvbind();
}

protected void Gridview1_RowUpdating(object sender, GridViewUpdateEventArgs


e)
{
GridViewRow row = Gridview1.Rows[e.RowIndex];
int Id = Convert.ToInt32(Gridview1.DataKeys[e.RowIndex].Values[0]);
string StudentName = (row.FindControl("txtStudentName") as
TextBox).Text;
string FatherName = (row.FindControl("txtFatherName") as TextBox).Text;
string DOB = (row.FindControl("txtDOB") as TextBox).Text;
string Address = (row.FindControl("txtAddress") as TextBox).Text;
string Cellno = (row.FindControl("txtCellno") as TextBox).Text;
string Program = (row.FindControl("txtProgram") as TextBox).Text;
string Country = (row.FindControl("drpCountry") as
DropDownList).SelectedValue;
string Gender = (row.FindControl("radGender") as
RadioButtonList).SelectedValue;12:04 AM 1/22/2022
using (DataClassesDataContext dbstd = new DataClassesDataContext())
{
StudentInfo std = (from c in dbstd.StudentInfos where c.Id == Id
select c).FirstOrDefault();
std.Address = txtAddress.Text;
std.Cellno = txtCellno.Text;
std.Country = drpCountry.SelectedValue;
std.DOB = Convert.ToDateTime(txtDOB.Text);
std.FatherName = txtFatherName.Text;
std.StudentName = txtStudentName.Text;
std.Program = txtProgram.Text;
std.Gender = radGender.SelectedValue;
dbstd.SubmitChanges();

}
Gridview1.EditIndex = -1;
Gvbind();
}
protected void Gridview1_RowCommand(object sender, GridViewCommandEventArgs
e)
{
if (e.CommandName == "Edit")
{

}
}
}

You might also like