Lab 5 Code

Register.aspx

code

<%@ Page Language="VB" AutoEventWireup="false" CodeFile="Register.aspx.vb" Inherits="Register" %> <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title>Register Student</title> <style> body { font-family: Arial; background-color: #f9f9f9; } .form-container { width: 400px; margin: auto; margin-top: 40px; padding: 20px; background-color: #fff; border: 2px solid #ccc; border-radius: 10px; } .form-container h2 { text-align: center; } .form-container table { width: 100%; } .form-container td { padding: 8px; } .form-container input[type="text"] { width: 95%; padding: 5px; } .form-container input[type="submit"] { width: 100%; padding: 10px; background-color: #007BFF; border: none; color: white; font-weight: bold; border-radius: 5px; } .message { margin-top: 10px; text-align: center; font-weight: bold; color: red; } </style> </head> <body> <form id="form1" runat="server"> <div class="form-container"> <h2>Register Student</h2> <table> <tr> <td>Name:</td> <td><asp:TextBox ID="txtName" runat="server"></asp:TextBox></td> </tr> <tr> <td>CNIC:</td> <td><asp:TextBox ID="txtCNIC" runat="server"></asp:TextBox></td> </tr> <tr> <td>Registration Number:</td> <td><asp:TextBox ID="txtRegNo" runat="server"></asp:TextBox></td> </tr> <tr> <td>Phone Number:</td> <td><asp:TextBox ID="txtPhone" runat="server"></asp:TextBox></td> </tr> <tr> <td>Address:</td> <td><asp:TextBox ID="txtAddress" runat="server"></asp:TextBox></td> </tr> <tr> <td colspan="2" align="center"> <asp:Button ID="btnRegister" runat="server" Text="Register" OnClick="btnRegister_Click" /> </td> </tr> </table> <asp:Label ID="lblMessage" runat="server" CssClass="message"></asp:Label> </div> </form> </body> </html>

Register.aspx.vb

code

Imports System.Data Imports System.Data.SqlClient Partial Class Register Inherits System.Web.UI.Page ' Define your connection string Dim connString As String = "workstation id=db_talal7_07.mssql.somee.com;packet size=4096;user id=talal7_07_SQLLogin_1;pwd=kslv811wh2;data source=db_talal7_07.mssql.somee.com;persist security info=False;initial catalog=db_talal7_07;TrustServerCertificate=True" Protected Sub btnRegister_Click(sender As Object, e As EventArgs) ' Declare connection and command objects Dim conn As New SqlConnection(connString) Dim cmd As New SqlCommand() ' SQL query to get the next available USERID Dim getMaxIdQuery As String = "SELECT ISNULL(MAX(USERID), 0) + 1 FROM REGISTERUSER" Dim newUserId As Integer Try ' Open the connection and fetch the new USERID conn.Open() Dim getIdCmd As New SqlCommand(getMaxIdQuery, conn) newUserId = Convert.ToInt32(getIdCmd.ExecuteScalar()) ' SQL query to insert the new student record Dim query As String = "INSERT INTO REGISTERUSER (USERID, NAME, CNIC, REGISTRATIONNUMBER, PHONENUMBER, ADDRESS) VALUES (@UserID, @Name, @CNIC, @RegNo, @Phone, @Address)" cmd = New SqlCommand(query, conn) ' Add parameters to the SQL query cmd.Parameters.AddWithValue("@UserID", newUserId) cmd.Parameters.AddWithValue("@Name", txtName.Text) cmd.Parameters.AddWithValue("@CNIC", txtCNIC.Text) cmd.Parameters.AddWithValue("@RegNo", txtRegNo.Text) cmd.Parameters.AddWithValue("@Phone", txtPhone.Text) cmd.Parameters.AddWithValue("@Address", txtAddress.Text) ' Execute the query to insert the data cmd.ExecuteNonQuery() ' Display a success message lblMessage.Text = "Student registered successfully." Catch ex As Exception ' Display error message if any exception occurs lblMessage.Text = "Error: " & ex.Message Finally ' Close the connection if it's open If conn.State = ConnectionState.Open Then conn.Close() End If End Try End Sub End Class

SearchStudent.aspx

code

<%@ Page Language="VB" AutoEventWireup="false" CodeFile="SearchStudent.aspx.vb" Inherits="SearchStudent" %> <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title>Search Student</title> <style> body { font-family: Arial; margin: 20px; } .form-container { max-width: 600px; margin: auto; } input[type="text"], input[type="submit"] { padding: 10px; margin: 5px 0; width: 100%; box-sizing: border-box; } table { width: 100%; border-collapse: collapse; margin-top: 20px; } table, th, td { border: 1px solid #999; } th, td { padding: 8px; text-align: left; } .nav { margin-bottom: 20px; } .nav a { margin-right: 10px; text-decoration: none; color: #007BFF; } .nav a:hover { text-decoration: underline; } </style> </head> <body> <div class="form-container"> <h2>Search Student</h2> <div class="nav"> <a href="SearchStudent.aspx">Search</a> </div> <form id="form1" runat="server"> <asp:Label ID="lblSearch" runat="server" Text="Enter Name or Reg Number:"></asp:Label> <asp:TextBox ID="txtSearch" runat="server" placeholder="e.g. Usman Tariq or REG005"></asp:TextBox> <asp:Button ID="btnSearch" runat="server" Text="Search" OnClick="btnSearch_Click" /> <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="True" /> </form> </div> </body> </html>

SearchStudent.aspx.vb

code

Imports System.Data Imports System.Data.SqlClient Partial Class SearchStudent Inherits System.Web.UI.Page Protected Sub btnSearch_Click(sender As Object, e As EventArgs) Dim searchTerm As String = txtSearch.Text.Trim() Dim conStr As String = "workstation id=db_talal7_07.mssql.somee.com;packet size=4096;user id=talal7_07_SQLLogin_1;pwd=kslv811wh2;data source=db_talal7_07.mssql.somee.com;persist security info=False;initial catalog=db_talal7_07;TrustServerCertificate=True" Using con As New SqlConnection(conStr) Dim query As String = "SELECT * FROM REGISTERUSER WHERE NAME LIKE @term OR REGISTRATIONNUMBER LIKE @term" Dim cmd As New SqlCommand(query, con) cmd.Parameters.AddWithValue("@term", "%" & searchTerm & "%") Dim adapter As New SqlDataAdapter(cmd) Dim dt As New DataTable() adapter.Fill(dt) GridView1.DataSource = dt GridView1.DataBind() End Using End Sub End Class
Web hosting by Somee.com