Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Web;
- using System.Web.UI;
- using System.Web.UI.WebControls;
- using System.Data.SqlClient;
- using System.Data;
- namespace NET_Mockup
- {
- public partial class Module_Details : System.Web.UI.Page
- {
- string connstring = @"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=C:\Users\crxss\source\repos\NET Mockup\NET Mockup\App_Data\metrodb.mdf;Integrated Security=True";
- protected void Page_Load(object sender, EventArgs e)
- {
- if (!IsPostBack)
- {
- Populategride();
- }
- }
- void Populategride()
- {
- DataTable dtbl = new DataTable();
- using (SqlConnection sqcon = new SqlConnection(connstring))
- {
- sqcon.Open();
- SqlDataAdapter sqda = new SqlDataAdapter("Select * FROM module", sqcon);
- sqda.Fill(dtbl);
- }
- if (dtbl.Rows.Count > 0)
- {
- GridView1.DataSource = dtbl;
- GridView1.DataBind();
- }
- else
- {
- dtbl.Rows.Add(dtbl.NewRow());
- GridView1.DataSource = dtbl;
- GridView1.DataBind();
- GridView1.Rows[0].Cells.Clear();
- GridView1.Rows[0].Cells.Add(new TableCell());
- GridView1.Rows[0].Cells[0].ColumnSpan = dtbl.Columns.Count;
- GridView1.Rows[0].Cells[0].Text = "No Data Found!";
- GridView1.Rows[0].Cells[0].HorizontalAlign = HorizontalAlign.Center;
- }
- }
- protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
- {
- try
- {
- if (e.CommandName.Equals("AddNew"))
- {
- using (SqlConnection sqcon = new SqlConnection(connstring))
- {
- sqcon.Open();
- string query = "Insert into module(m_ID,m_name,learning_hours,lecturer) values(@m_ID,@m_name,@learning_hours,@lecturer)";
- SqlCommand sqcom = new SqlCommand(query, sqcon);
- sqcom.Parameters.AddWithValue("@m_ID", (GridView1.FooterRow.FindControl("text_m_ID_footer") as TextBox).Text.Trim());
- sqcom.Parameters.AddWithValue("@m_name", (GridView1.FooterRow.FindControl("text_m_name_footer") as TextBox).Text.Trim());
- sqcom.Parameters.AddWithValue("@learning_hours", (GridView1.FooterRow.FindControl("text_learning_hours_footer") as TextBox).Text.Trim());
- sqcom.Parameters.AddWithValue("@lecturer", (GridView1.FooterRow.FindControl("text_lecturer_footer") as TextBox).Text.Trim());
- sqcom.ExecuteNonQuery();
- Populategride();
- success.Text = "NEW RECORD INSERTED SUCCESSFULLY :-)";
- error.Text = "";
- }
- }
- }
- catch (Exception ex)
- {
- success.Text = "";
- error.Text = ex.Message;
- }
- }
- protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
- {
- try
- {
- using (SqlConnection sqcon = new SqlConnection(connstring))
- {
- sqcon.Open();
- string query = "update module set m_name=@m_name, learning_hours=@learning_hours, lecturer=@lecturer where m_ID=@m_ID";
- SqlCommand sqcom = new SqlCommand(query, sqcon);
- sqcom.Parameters.AddWithValue("@m_ID", (GridView1.Rows[e.RowIndex].FindControl("m_ID") as TextBox).Text.Trim());
- sqcom.Parameters.AddWithValue("@m_name", (GridView1.Rows[e.RowIndex].FindControl("m_name") as TextBox).Text.Trim());
- sqcom.Parameters.AddWithValue("@learning_hours", (GridView1.Rows[e.RowIndex].FindControl("learning_hours") as TextBox).Text.Trim());
- sqcom.Parameters.AddWithValue("@lecturer", (GridView1.Rows[e.RowIndex].FindControl("lecturer") as TextBox).Text.Trim());
- sqcom.ExecuteNonQuery();
- Populategride();
- success.Text = "NEW RECORD UPDATED :-)";
- error.Text = "";
- }
- }
- catch (Exception ex)
- {
- success.Text = "";
- error.Text = ex.Message;
- }
- }
- protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
- {
- try
- {
- using (SqlConnection sqcon = new SqlConnection(connstring))
- {
- sqcon.Open();
- string query = "Delete from module where m_ID=@m_ID";
- SqlCommand sqcom = new SqlCommand(query, sqcon);
- sqcom.Parameters.AddWithValue("@m_ID", Convert.ToInt32(GridView1.DataKeys[e.RowIndex].Value.ToString()));
- sqcom.ExecuteNonQuery();
- Populategride();
- success.Text = "<h3> DELETED SUCCESSFULLY :-( </h3>";
- error.Text = "";
- }
- }
- catch (Exception ex)
- {
- success.Text = "";
- error.Text = ex.Message;
- }
- }
- protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
- {
- GridView1.EditIndex = -1;
- Populategride();
- }
- protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
- {
- GridView1.EditIndex = e.NewEditIndex;
- Populategride();
- }
- protected void Button1_Click(object sender, EventArgs e)
- {
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement