Advertisement
crxssrazr93

.Net BackEnd Code

Jun 23rd, 2018
1,291
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
ASP 5.96 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Web;
  5. using System.Web.UI;
  6. using System.Web.UI.WebControls;
  7. using System.Data.SqlClient;
  8. using System.Data;
  9.  
  10. namespace NET_Mockup
  11. {
  12.     public partial class Module_Details : System.Web.UI.Page
  13.     {
  14.         string connstring = @"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=C:\Users\crxss\source\repos\NET Mockup\NET Mockup\App_Data\metrodb.mdf;Integrated Security=True";
  15.         protected void Page_Load(object sender, EventArgs e)
  16.         {
  17.             if (!IsPostBack)
  18.             {
  19.                 Populategride();
  20.             }
  21.         }
  22.         void Populategride()
  23.         {
  24.             DataTable dtbl = new DataTable();
  25.             using (SqlConnection sqcon = new SqlConnection(connstring))
  26.             {
  27.                 sqcon.Open();
  28.                 SqlDataAdapter sqda = new SqlDataAdapter("Select * FROM module", sqcon);
  29.                 sqda.Fill(dtbl);
  30.             }
  31.             if (dtbl.Rows.Count > 0)
  32.             {
  33.                 GridView1.DataSource = dtbl;
  34.                 GridView1.DataBind();
  35.             }
  36.             else
  37.             {
  38.                 dtbl.Rows.Add(dtbl.NewRow());
  39.                 GridView1.DataSource = dtbl;
  40.                 GridView1.DataBind();
  41.                 GridView1.Rows[0].Cells.Clear();
  42.                 GridView1.Rows[0].Cells.Add(new TableCell());
  43.                 GridView1.Rows[0].Cells[0].ColumnSpan = dtbl.Columns.Count;
  44.                 GridView1.Rows[0].Cells[0].Text = "No Data Found!";
  45.                 GridView1.Rows[0].Cells[0].HorizontalAlign = HorizontalAlign.Center;
  46.             }
  47.         }
  48.  
  49.         protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
  50.         {
  51.             try
  52.             {
  53.                 if (e.CommandName.Equals("AddNew"))
  54.                 {
  55.                     using (SqlConnection sqcon = new SqlConnection(connstring))
  56.                     {
  57.                         sqcon.Open();
  58.                         string query = "Insert into module(m_ID,m_name,learning_hours,lecturer) values(@m_ID,@m_name,@learning_hours,@lecturer)";
  59.                         SqlCommand sqcom = new SqlCommand(query, sqcon);
  60.                         sqcom.Parameters.AddWithValue("@m_ID", (GridView1.FooterRow.FindControl("text_m_ID_footer") as TextBox).Text.Trim());
  61.                         sqcom.Parameters.AddWithValue("@m_name", (GridView1.FooterRow.FindControl("text_m_name_footer") as TextBox).Text.Trim());
  62.                         sqcom.Parameters.AddWithValue("@learning_hours", (GridView1.FooterRow.FindControl("text_learning_hours_footer") as TextBox).Text.Trim());
  63.                         sqcom.Parameters.AddWithValue("@lecturer", (GridView1.FooterRow.FindControl("text_lecturer_footer") as TextBox).Text.Trim());
  64.                         sqcom.ExecuteNonQuery();
  65.                         Populategride();
  66.                         success.Text = "NEW RECORD INSERTED SUCCESSFULLY :-)";
  67.                         error.Text = "";
  68.                     }
  69.                 }
  70.             }
  71.             catch (Exception ex)
  72.             {
  73.                 success.Text = "";
  74.                 error.Text = ex.Message;
  75.             }
  76.  
  77.         }
  78.  
  79.         protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
  80.         {
  81.             try
  82.             {
  83.                 using (SqlConnection sqcon = new SqlConnection(connstring))
  84.                 {
  85.                     sqcon.Open();
  86.                     string query = "update module set m_name=@m_name, learning_hours=@learning_hours, lecturer=@lecturer where m_ID=@m_ID";
  87.                     SqlCommand sqcom = new SqlCommand(query, sqcon);
  88.                     sqcom.Parameters.AddWithValue("@m_ID", (GridView1.Rows[e.RowIndex].FindControl("m_ID") as TextBox).Text.Trim());
  89.                     sqcom.Parameters.AddWithValue("@m_name", (GridView1.Rows[e.RowIndex].FindControl("m_name") as TextBox).Text.Trim());
  90.                     sqcom.Parameters.AddWithValue("@learning_hours", (GridView1.Rows[e.RowIndex].FindControl("learning_hours") as TextBox).Text.Trim());
  91.                     sqcom.Parameters.AddWithValue("@lecturer", (GridView1.Rows[e.RowIndex].FindControl("lecturer") as TextBox).Text.Trim());
  92.  
  93.                     sqcom.ExecuteNonQuery();
  94.                     Populategride();
  95.                     success.Text = "NEW RECORD UPDATED :-)";
  96.                     error.Text = "";
  97.                 }
  98.  
  99.             }
  100.             catch (Exception ex)
  101.             {
  102.                 success.Text = "";
  103.                 error.Text = ex.Message;
  104.             }
  105.         }
  106.  
  107.         protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
  108.         {
  109.             try
  110.             {
  111.                 using (SqlConnection sqcon = new SqlConnection(connstring))
  112.                 {
  113.                     sqcon.Open();
  114.                     string query = "Delete from module where m_ID=@m_ID";
  115.                     SqlCommand sqcom = new SqlCommand(query, sqcon);
  116.                     sqcom.Parameters.AddWithValue("@m_ID", Convert.ToInt32(GridView1.DataKeys[e.RowIndex].Value.ToString()));
  117.                     sqcom.ExecuteNonQuery();
  118.                     Populategride();
  119.                     success.Text = "<h3> DELETED SUCCESSFULLY :-( </h3>";
  120.                     error.Text = "";
  121.                 }
  122.             }
  123.             catch (Exception ex)
  124.             {
  125.                 success.Text = "";
  126.                 error.Text = ex.Message;
  127.             }
  128.         }
  129.  
  130.         protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
  131.         {
  132.             GridView1.EditIndex = -1;
  133.             Populategride();
  134.         }
  135.  
  136.         protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
  137.         {
  138.             GridView1.EditIndex = e.NewEditIndex;
  139.             Populategride();
  140.         }
  141.  
  142.         protected void Button1_Click(object sender, EventArgs e)
  143.         {
  144.  
  145.         }
  146.     }
  147. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement