0
1.7kviews
Write an ASP.NET program to insert a new record in database

Mumbai University > Information Technology > Sem 4 > Web Programming

Marks: 10M

Year: May 2014, Dec 2014, Dec 2015

1 Answer
0
2views

Default.aspx

<%@ Page Language="VB" AutoEventWireup="false" CodeFile="Default.aspx.vb" Inherits="_Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "https://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="https://www.w3.org/1999/xhtml">
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:GridView ID="GridView1" runat="server"  
OnRowCommand="gridview1_RowCommand" >
</asp:GridView>

&nbsp;<br />

<asp:Button ID="Button2" runat="server" Text="Insert" />

&nbsp;&nbsp;
<br />

Roll_No :&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;

<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<br />
<br />
        Student_Name&nbsp; :&nbsp;&nbsp;&nbsp;&nbsp;
<asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
<br />
<br />
        Phone_Number :&nbsp;&nbsp;&nbsp;&nbsp;
<asp:TextBox ID="TextBox3" runat="server"></asp:TextBox>
<br />

<br />

Adderss :

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;

<asp:TextBox ID="TextBox4" runat="server"></asp:TextBox>
<br />
<br />

        Comparing value :&nbsp;&nbsp;
<asp:TextBox ID="TextBox5" runat="server"></asp:TextBox>
<br />
<br />
</div>
</form>
</body>
</html>

Default.aspx.vb

Imports System.Data
Imports System.Data.SqlClient
Partial Class _Default
    Inherits System.Web.UI.Page
    Dim mycom As SqlCommand
    Dim mycon As SqlConnection
    Dim ds As DataSet
    Dim adp As SqlDataAdapter
    Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles 
Button1.Click
mycon = New SqlConnection("server=webserver1;user id =sa;password=sa;database=VUW")
mycon.Open()
mycom = New SqlCommand("select * from t1", mycon)
ds = New DataSet
adp = New SqlDataAdapter(mycom)
adp.Fill(ds, "t1")
        GridView1.DataSource = ds
GridView1.DataBind()
mycon.Close()
    End Sub
    Protected Sub Button2_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button2.Click
mycon = New SqlConnection("server=webserver1;user id =sa;password=sa;database=VUW")
mycon.Open()
mycom = New SqlCommand("insert into t1 (Roll_No,Student_Name,Phone_No,Address) values ('" & TextBox1.Text & "','" & TextBox2.Text & "','" & TextBox3.Text & "','" & TextBox4.Text & "')", mycon)
mycom.ExecuteNonQuery()
mycon.Close()
    End Sub

    Protected Sub GridView1_RowCommand(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewCommandEventArgs) Handles GridView1.RowCommand
        If e.CommandName = "Select" Then
            Dim index As Integer = Convert.ToInt32(e.CommandArgument)
            Dim r As GridViewRow = GridView1.Rows(index)
Session.Add("Roll_No", r.Cells(1).Text)
Session.Add("Student_Name", r.Cells(2).Text)
Session.Add("Phone_No", r.Cells(3).Text)
Session.Add("Address", r.Cells(4).Text)
        End If
        TextBox1.Text = Session("Roll_No")
        TextBox2.Text = Session("Student_Name")
        TextBox3.Text = Session("Phone_No")
        TextBox4.Text = Session("Address")
    End Sub
End Class
Please log in to add an answer.