Open Visual Studio :p
Create a new website and add form in it.
Write click on website Name -> Add New Item -> Select SQL Server Database :-
Add new Table in Database.mdf
Add 3 fields Id,Name,City
and save it.
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<style type="text/css">
.style1
{
width: 97px;
}
</style>
</head>
<body>
<form id="form1" runat="server">
<div>
<table style="width:100%;">
<tr>
<td class="style1">
Name</td>
<td>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
</td>
<td>
</td>
</tr>
<tr>
<td class="style1">
City</td>
<td>
<asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
</td>
<td>
</td>
</tr>
<tr>
<td class="style1">
</td>
<td>
<asp:Button ID="Button1" runat="server" Text="Save" onclick="Button1_Click" />
<asp:Button ID="Button2" runat="server" onclick="Button2_Click" Text="Show" />
</td>
<td>
</td>
</tr>
</table>
</div><br /><br />
<asp:DataList ID="DataList1" runat="server" BackColor="#CCCCCC"
BorderColor="#999999" BorderStyle="Solid" BorderWidth="3px" CellPadding="4"
CellSpacing="2" ForeColor="Black" GridLines="Both">
<FooterStyle BackColor="#CCCCCC" />
<HeaderStyle BackColor="Black" Font-Bold="True" ForeColor="White" />
<ItemStyle BackColor="White" />
<ItemTemplate>
Name:
<asp:Label ID="Label1" runat="server" Text='<%# bind("Name") %>'></asp:Label>
<br />
City:
<asp:Label ID="Label2" runat="server" Text='<%# Bind("City") %>'></asp:Label>
</ItemTemplate>
<SelectedItemStyle BackColor="#000099" Font-Bold="True" ForeColor="White" />
</asp:DataList>
</form>
</body>
</html>
Create a new website and add form in it.
Write click on website Name -> Add New Item -> Select SQL Server Database :-
Add new Table in Database.mdf
Add 3 fields Id,Name,City
and save it.
Write the following code in the Default.aspx :-
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<style type="text/css">
.style1
{
width: 97px;
}
</style>
</head>
<body>
<form id="form1" runat="server">
<div>
<table style="width:100%;">
<tr>
<td class="style1">
Name</td>
<td>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
</td>
<td>
</td>
</tr>
<tr>
<td class="style1">
City</td>
<td>
<asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
</td>
<td>
</td>
</tr>
<tr>
<td class="style1">
</td>
<td>
<asp:Button ID="Button1" runat="server" Text="Save" onclick="Button1_Click" />
<asp:Button ID="Button2" runat="server" onclick="Button2_Click" Text="Show" />
</td>
<td>
</td>
</tr>
</table>
</div><br /><br />
<asp:DataList ID="DataList1" runat="server" BackColor="#CCCCCC"
BorderColor="#999999" BorderStyle="Solid" BorderWidth="3px" CellPadding="4"
CellSpacing="2" ForeColor="Black" GridLines="Both">
<FooterStyle BackColor="#CCCCCC" />
<HeaderStyle BackColor="Black" Font-Bold="True" ForeColor="White" />
<ItemStyle BackColor="White" />
<ItemTemplate>
Name:
<asp:Label ID="Label1" runat="server" Text='<%# bind("Name") %>'></asp:Label>
<br />
City:
<asp:Label ID="Label2" runat="server" Text='<%# Bind("City") %>'></asp:Label>
</ItemTemplate>
<SelectedItemStyle BackColor="#000099" Font-Bold="True" ForeColor="White" />
</asp:DataList>
</form>
</body>
</html>
Now write the following code in the Default.aspx.cs :-
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
public partial class _Default : System.Web.UI.Page
{
SqlConnection cn;
SqlCommand cmd;
SqlDataAdapter da;
protected void Page_Load(object sender, EventArgs e)
{
cn = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDbFilename=E:\C#\WebSites\DataList1\App_Data\MyDataList.mdf;Integrated Security=True;User Instance=True");
}
protected void Button1_Click(object sender, EventArgs e)
{
cmd = new SqlCommand("Insert into Mahi values (@name,@city)", cn);
cmd.Parameters.AddWithValue("@name", TextBox1.Text);
cmd.Parameters.AddWithValue("@city", TextBox2.Text);
cn.Open();
cmd.ExecuteNonQuery();
cn.Close();
}
protected void Button2_Click(object sender, EventArgs e)
{
da = new SqlDataAdapter("Select* from Mahi", cn);
DataSet ds = new DataSet();
da.Fill(ds);
DataList1.DataSource = ds;
DataList1.DataBind();
}
}


No comments:
Post a Comment