You are on page 1of 4

Insert, Update, Delete student records

Student.aspx
<%@ Page Language="VB" AutoEventWireup="false" CodeFile="Student.aspx.vb"
Inherits="Student" %>

<!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 runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>

Student ID :&nbsp;&nbsp;&nbsp;
<asp:TextBox ID="txtStudID" runat="server" Width="147px"></asp:TextBox>
<br />
<br />
Student Name :&nbsp;&nbsp;&nbsp;
<asp:TextBox ID="txtSname" runat="server" Width="219px"></asp:TextBox>
<br />
<br />
Area :&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
<asp:TextBox ID="txtArea" runat="server" Width="181px"></asp:TextBox>
<br />
<br />
Mobile :&nbsp;&nbsp;&nbsp;&nbsp;
<asp:TextBox ID="txtMobile" runat="server" Width="184px"></asp:TextBox>
<br />
<br />
Email :&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
<asp:TextBox ID="txtEmail" runat="server" Width="188px"></asp:TextBox>
<br />
<br />
Study In :&nbsp;&nbsp;
<asp:TextBox ID="txtStudy" runat="server" Width="186px"></asp:TextBox>
<br />
<br />
<br />
<asp:Button ID="btnSubmit" runat="server" Text="SUBMIT" />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
<asp:Button ID="btnReset" runat="server" Text="RESET" />

<br />
<br />
<br />
<br />
<asp:GridView ID="grd" runat="server" CellPadding="4" ForeColor="#333333"
GridLines="None" AutoGenerateDeleteButton="True"
AutoGenerateEditButton="true"
AutoGenerateColumns="true"
DataKeyNames="StudID"
OnRowEditing="OnRowEditing"
OnRowCancelingEdit="OnRowCancelingEdit"
OnRowUpdating = "OnRowUpdating"
OnRowDeleting = "RowDelete"
>
<AlternatingRowStyle BackColor="White" />
<FooterStyle BackColor="#990000" Font-Bold="True" ForeColor="White" />
<HeaderStyle BackColor="#990000" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#FFCC66" ForeColor="#333333" HorizontalAlign="Center"
/>
<RowStyle BackColor="#FFFBD6" ForeColor="#333333" />
<SelectedRowStyle BackColor="#FFCC66" Font-Bold="True" ForeColor="Navy" />
<SortedAscendingCellStyle BackColor="#FDF5AC" />
<SortedAscendingHeaderStyle BackColor="#4D0000" />
<SortedDescendingCellStyle BackColor="#FCF6C0" />
<SortedDescendingHeaderStyle BackColor="#820000" />
</asp:GridView>
<br />
<br />

<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />

</div>
</form>
</body>
</html>

Imports System.Data.SqlClient
Imports System.Data
Partial Class Student
Inherits System.Web.UI.Page
Dim strConnection As String = "Data Source=.\SQLEXPRESS;AttachDbFilename=D:\College\
ASP.NET\2022\Website_B\DB2022.mdf;Integrated Security=True;Connect Timeout=30;User
Instance=True"
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles
Me.Load
If IsPostBack = False Then
FillData()
End If
End Sub
Protected Sub btnSubmit_Click(ByVal sender As Object, ByVal e As System.EventArgs)
Handles btnSubmit.Click
Dim cn As New SqlConnection(strConnection)
Dim cmd As New SqlCommand
Dim query As String
cn.Open()
query = "Insert into studMst values(" & txtStudID.Text & ",'" & _
txtSname.Text & "','" & txtArea.Text & "','" & _
txtMobile.Text & "','" & txtEmail.Text & "','" & _
txtStudy.Text & "')"

cmd.CommandText = query : cmd.Connection = cn


cmd.ExecuteNonQuery()
cmd.Dispose()
If cn.State = 1 Then cn.Close()
cn.Dispose() : ClearData()
MsgBox("Record Saved Successfully")
FillData()
End Sub
Private Sub ClearData()
txtArea.Text = ""
txtEmail.Text = ""
txtMobile.Text = "" : txtSname.Text = ""
txtStudID.Text = "" : txtStudy.Text = ""
End Sub
Private Sub FillData()
Dim cn As New SqlConnection(strConnection)
Dim cmd As New SqlCommand
Dim query As String
Dim ds As New DataSet 'To Hold database recordss
Dim da As New SqlDataAdapter
cn.Open()
query = "Select StudID,sname,area,mobile,email,study from StudMst order by
StudID"
cmd.CommandText = query
cmd.Connection = cn
da.SelectCommand = cmd
da.Fill(ds)
grd.DataSource = ds.Tables(0)
grd.DataBind()
ds.Dispose() : da.Dispose() : cmd.Dispose()
If cn.State = 1 Then cn.Close()
cn.Dispose()
End Sub

Public Sub OnRowEditing(ByVal sender As Object, ByVal e As GridViewEditEventArgs)


grd.EditIndex = e.NewEditIndex
FillData()
End Sub
Public Sub OnRowCancelingEdit(ByVal sender As Object, ByVal e As
GridViewCancelEditEventArgs)
grd.EditIndex = -1
FillData()
End Sub
Public Sub OnRowUpdating(ByVal sender As Object, ByVal e As GridViewUpdateEventArgs)
Dim Query As String
Dim strName, strMobile, strArea, strEmail, strStudy As String
Dim row As GridViewRow = grd.Rows(e.RowIndex)
Dim intCode As Int32 = CType(grd.DataKeys(e.RowIndex).Values(0), Int32)
strName = TryCast(row.Cells(2).Controls(0), TextBox).Text
strArea = TryCast(row.Cells(3).Controls(0), TextBox).Text
strMobile = TryCast(row.Cells(4).Controls(0), TextBox).Text
strEmail = TryCast(row.Cells(5).Controls(0), TextBox).Text
strStudy = TryCast(row.Cells(6).Controls(0), TextBox).Text

Query = "Update studMst Set Sname='" & _


strName & "',Area='" & strArea & "',Mobile='" & _
strMobile & "',EMail='" & strEmail & "',Study='" & _
strStudy & "' where StudID=" & intCode & ""

Dim cn As New SqlConnection(strConnection)


Dim cmd As New SqlCommand
cn.Open()

cmd.CommandText = Query : cmd.Connection = cn


cmd.ExecuteNonQuery()
cmd.Dispose()
If cn.State = 1 Then cn.Close()
cn.Dispose()
grd.EditIndex = -1
FillData()
End Sub
Public Sub RowDelete(ByVal Sender As Object, ByVal e As GridViewDeleteEventArgs)
Dim Query As String
If MsgBox("Are you sure? want to delete?", vbYesNo + vbQuestion +
vbDefaultButton2) = vbNo Then
grd.EditIndex = -1
Exit Sub
End If
Dim intCode As Int32 = CType(grd.DataKeys(e.RowIndex).Values(0), Int32)

Query = "Delete from studMst where StudID=" & intCode & ""
Dim cn As New SqlConnection(strConnection)
Dim cmd As New SqlCommand
cn.Open()
cmd.CommandText = Query : cmd.Connection = cn
cmd.ExecuteNonQuery()
cmd.Dispose()
If cn.State = 1 Then cn.Close()
cn.Dispose()
grd.EditIndex = -1
FillData()

End Sub

End Class

You might also like