8c)Create a web application to display Using Disconnected Data Access and Databinding using GridView.

program:


Default.aspx

<%@ PageLanguage="C#"AutoEventWireup="true"CodeBehind="Default.aspx.cs"
Inherits="WebApplication11._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:111px;
}
</style>
</head>
<body>
<form id="form1"runat="server">
<tableclass="style1">
<tr>
<tdclass="style2">
ID</td>
<td>
<asp:TextBoxID="TextBox_id"runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<tdclass="style2">
Name</td>
<td>
<asp:TextBoxID="TextBox_name"runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<tdclass="style2">
&nbsp;</td>
<td>
&nbsp;</td>
</tr>
<tr>
<tdclass="style2">
<asp:ButtonID="Button1_Add"runat="server"onclick="Button1_Add_Click"
Text="ADD"Width="73px"/>
</td>
<td>
<asp:ButtonID="Button2_Update"runat="server"
onclick="Button2_Update_Click"
Text="UPDATE"/>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
<asp:ButtonID="Button3_Delete"runat="server"onclick="Button3_Delete_Click"
Text="DELETE"/>
</td>
</tr>
</table>
<div>
</div>
<asp:GridViewID="GridView1"runat="server">
</asp:GridView>
</form>
</body>
</html>



Web.config


<?xmlversion="1.0"?>
<configuration>
<appSettings/>
<system.web>
<compilationdebug="true">
</compilation>
<authenticationmode="Windows"/>
</system.web>
<connectionStrings>
<addname="disconnect"connectionString="Data
Source=.\SQLEXPRESS;AttachDbFilename=C:\Users\BNB\Documents\VisualStudio
2008\Projects\WebApplication11\WebApplication11\App_Data\Database1.mdf;Integrated
Security=True;UserInstance=True"></add>
</connectionStrings>
</configuration>




Default.aspx.cs


usingSystem;
usingSystem.Collections;
usingSystem.Configuration;
usingSystem.Data;
usingSystem.Web;
usingSystem.Web.Security;
usingSystem.Web.UI;
usingSystem.Web.UI.HtmlControls;
usingSystem.Web.UI.WebControls;
usingSystem.Web.UI.WebControls.WebParts;
usingSystem.Data.Sql;
usingSystem.Data.SqlClient;
usingSystem.Data.SqlTypes;
namespaceWebApplication11
{
publicpartialclass_Default:System.Web.UI.Page
{
publicDataSetds;
SqlDataAdapterda;
SqlCommandBuildercmd;
SqlConnectioncon=new
SqlConnection(ConfigurationManager.ConnectionStrings["disconnect"].ConnectionString);
protectedvoidPage_Load(objectsender,EventArgse)
{
SqlDataAdapterda=newSqlDataAdapter("select*from Student",con);
DataSetds=newDataSet();
da.Fill(ds);
GridView1.DataSource=ds.Tables[0];
GridView1.DataBind();
}
protectedvoidButton1_Add_Click(objectsender,EventArgse)
{
SqlDataAdapterda=newSqlDataAdapter("select*from Student",con);
da.InsertCommand=newSqlCommand("insertintoStudent
values(@ID,@Name)",con);
SqlCommandcmdInsert=da.InsertCommand;
cmdInsert.Parameters.AddWithValue("@ID",Int32.Parse(TextBox_id.Text));
cmdInsert.Parameters.AddWithValue("@Name",TextBox_name.Text);
con.Open();
cmdInsert.ExecuteNonQuery();
Response.Write("Recordisinserted");
DataSetds=newDataSet();
da.Fill(ds);
GridView1.DataSource=ds.Tables[0];
GridView1.DataBind();
}
protectedvoidButton2_Update_Click(objectsender,EventArgse)
{
intn=Int32.Parse(TextBox_id.Text);
SqlDataAdapterda=newSqlDataAdapter("select*from Student",con);
da.UpdateCommand=newSqlCommand("updateStudentset
ID=@ID,Name=@NameWHEREID="+n,con);
SqlCommandcmdUpdate=da.UpdateCommand;
cmdUpdate.Parameters.AddWithValue("@ID",Int32.Parse(TextBox_id.Text));
cmdUpdate.Parameters.AddWithValue("@Name",TextBox_name.Text);
con.Open();
cmdUpdate.ExecuteNonQuery();
Response.Write("RecordisUpdated");
DataSetds=newDataSet();
da.Fill(ds);
GridView1.DataSource=ds.Tables[0];
GridView1.DataBind();
}
protectedvoidButton3_Delete_Click(objectsender,EventArgse)
{
intn=Int32.Parse(TextBox_id.Text);
SqlDataAdapterda=newSqlDataAdapter("select*from Student",con);
da.DeleteCommand=newSqlCommand("deletefrom StudentwhereID="+n,con);
SqlCommandcmdDelete=da.DeleteCommand;
cmdDelete.Parameters.AddWithValue("@ID",Int32.Parse(TextBox_id.Text));
cmdDelete.Parameters.AddWithValue("@Name",TextBox_name.Text);
con.Open();
cmdDelete.ExecuteNonQuery();
Response.Write("RecordisDeleted");
DataSetds=newDataSet();
da.Fill(ds);
GridView1.DataSource=ds.Tables[0];
GridView1.DataBind();
}
}
}

Comments