You are on page 1of 2

Insert into Database

Table Structure(tbl_first)

Form Layout

<%@ Page Language="C#" AutoEventWireup="true"


CodeFile="Default.aspx.cs" Inherits="_Default" %>
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.SqlClient" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script language="C#" runat="server">
void ins(object e, EventArgs s) {
try
{
SqlConnection myConn = new SqlConnection();
myConn.ConnectionString = "Data Source=HOUSE-
8CDAD0BD7\\SQLEXPRESS;Initial Catalog=test;Integrated
Security=True";
myConn.Open();
string strqry = "Insert into tbl_first values (" +
TextBox1.Text + ",'" + TextBox2.Text + "','" + TextBox3.Text +
"')";
SqlCommand myCom = new SqlCommand(strqry, myConn);
int numrow = myCom.ExecuteNonQuery();
myConn.Close();
TextBox1.Text = TextBox2.Text = TextBox3.Text = "";

}
catch (Exception x) {
Label4.Text = x.ToString( );

RESOURCE PERSON: MUHAMMAD ADEEL ABID Page 1


Insert into Database

}
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Label ID="Label4"
runat="server"></asp:Label>&nbsp;<br />
<asp:Label ID="Label1" runat="server" Text="Enter Sr
#"></asp:Label>
<asp:TextBox ID="TextBox1"
runat="server"></asp:TextBox><br />
<br />
<asp:Label ID="Label2" runat="server" Text="Enter
Name"></asp:Label>
<asp:TextBox ID="TextBox2"
runat="server"></asp:TextBox><br />
<br />
<asp:Label ID="Label3" runat="server" Text="Enter
Address"></asp:Label>
<asp:TextBox ID="TextBox3"
runat="server"></asp:TextBox><br />
<br />
<asp:Button ID="Button1" runat="server" Text="Insert"
onClick="ins"/><br />
<br />
&nbsp;
</div>
</form>
</body>
</html>

RESOURCE PERSON: MUHAMMAD ADEEL ABID Page 2

You might also like