7c. Create a web application for inserting and deleting record from a database. (Using Execute-Non Query).

Program:


Default.aspx

<%@PageLanguage="C#"AutoEventWireup="true"CodeBehind="Default . aspx.cs" Inherits="WebApplication4._Default "%>
<!DOCTYPEhtmlPUBLIC"-//W3C//DTDXHTML1.0Transitional//EN" " http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional . dtd" >
<htmlxmlns="http://www.w3.org/1999/xhtml ">
<headrunat ="server " >
<title>UntitledPage</title>
<styletype="text/css" >
. style1
{
width:100%;
}
. style2
{
width:80px;
}
. style3
{
width:168px;
}
</style>
</head>
<body >
<formid="form1"runat ="server " >
<div >
<tableclass="style1" >
<tr >
<tdclass="style2" >
Name</td>
<tdclass="style3" >
<asp:TextBoxID="TextBox1"runat ="server " ></asp:TextBox>
</td>
</tr >
<tr >
<tdclass="style2" >
ID</td>
<tdclass="style3" >
<asp:TextBoxID="TextBox2"runat ="server " ></asp:TextBox>
</td>
</tr >
<tr >
<tdclass="style2" >
Contact</td>
<tdclass="style3" >
<asp:TextBoxID="TextBox3"runat ="server " ></asp:TextBox>
</td>
</tr >
<tr >
<tdclass="style2" >
Country</td>
<tdclass="style3" >
<asp:TextBoxID="TextBox4"runat ="server " ></asp:TextBox>
</td>
</tr >
<tr >
<tdclass="style2" >
State</td>
<tdclass="style3" >
<asp:TextBoxID="TextBox5"runat ="server " ></asp:TextBox>
</td>
</tr >
<tr >
<tdclass="style2" >
&nbsp; </td>
<tdclass="style3" >
&nbsp; </td>
</tr >
<tr >
<tdclass="style2" >
<asp:ButtonID="Button1"runat ="server "onclick="Button1_Click"
Text ="Insert "
Width="78px"/>
</td>
<tdclass="style3" >
<asp:ButtonID="Button2"runat ="server "onclick="Button2_Click"
Text ="Delete"
Width="78px"/>
</td>
</tr >
</table>
</div >
<asp:LabelID="Label1"runat ="server " ></asp:Label >
</form>
</body >
</html >



Default.aspx.cs

usingSystem;
usingSystem.Collections;
usingSystem.Configuration;
usingSystem.Data;
usingSystem.Linq;
usingSystem.Web;
usingSystem.Web.Security ;
usingSystem.Web.UI ;
usingSystem.Web.UI . HtmlControls;
usingSystem.Web.UI . WebControls;
usingSystem.Web.UI . WebControls.WebParts;
usingSystem.Xml . Linq;
usingSystem.Data.SqlTypes;
usingSystem.Data.SqlClient ;
usingSystem.Data.Sql ;
namespaceWebApplication4
{
publicpartialclass_Default:System.Web.UI . Page
{
SqlConnectionsqlcon=newSqlConnection( @"Data Source=.\SQLEXPRESS;AttachDbFilename=C:\Users\BNB\Documents\VisualStudio 2008\Projects\WebApplication4\WebApplication4\App_Data\Database1.mdf ; Integrated Security=True;UserInstance=True" );
protectedvoidPage_Load(objectsender , EventArgse)
{
}
protectedvoidButton1_Click(objectsender , EventArgse)
{
stringInsertQuery="insertintoDB values(@Name,@ID,@Contact , @Country , @State)" ;
SqlCommandcmd=newSqlCommand(InsertQuery , sqlcon);
cmd.Parameters.AddWithValue( " @Name" , TextBox1.Text);
cmd.Parameters.AddWithValue( " @ID" , TextBox2.Text);
cmd.Parameters.AddWithValue( " @Contact " , TextBox3.Text);
cmd.Parameters.AddWithValue( " @Country " , TextBox4.Text);
cmd.Parameters.AddWithValue( " @State" , TextBox5.Text);
sqlcon.Open();
cmd.ExecuteNonQuery();
Label1.Text="RecordInsertedSuccessfully " ;
sqlcon.Close();
}
protectedvoidButton2_Click(objectsender , EventArgse)
{
stringInsertQuery="deletefromDBwhereName=@Name" ;
SqlCommandcmd=newSqlCommand(InsertQuery , sqlcon);
cmd.Parameters.AddWithValue( " @Name" , TextBox1.Text);
sqlcon.Open();
cmd.ExecuteNonQuery();
Label1.Text="RecordDeletedSuccessfully " ;
sqlcon.Close();
}
}
}

Comments