Step 1: Create web Page and Insert Following Control in Form tag:
<form id="form1" runat="server" >
Name <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<br />
Mobile No <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
<br />
Email Id <asp:TextBox ID="TextBox3" runat="server"></asp:TextBox>
<br />
<asp:Button ID="txt_Insert" runat="server" onclick="txt_Insert_Click"
Text="Submit" />
<br />
<br />
Step 2:- Insert GridView and Connection with Table in Database
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
DataKeyNames="Id" DataSourceID="SqlDataSource1"
onrowcommand="GridView1_RowCommand">
<Columns>
<asp:TemplateField ShowHeader="False">
<EditItemTemplate>
<asp:LinkButton ID="LinkButton1" runat="server" CausesValidation="True"
CommandName="Update" Text="Update"></asp:LinkButton>
<asp:LinkButton ID="LinkButton2" runat="server" CausesValidation="False"
CommandName="Cancel" Text="Cancel"></asp:LinkButton>
</EditItemTemplate>
<ItemTemplate>
<asp:LinkButton ID="LinkButton1" runat="server" CausesValidation="False"
CommandName="Edits" CommandArgument ='<%# Bind("Id") %>' Text="Edit"></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Id" InsertVisible="False" SortExpression="Id"
Visible="False">
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Bind("Id") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="Name" HeaderText="Name" SortExpression="Name" />
<asp:BoundField DataField="Mobile_No" HeaderText="Mobile_No"
SortExpression="Mobile_No" />
<asp:BoundField DataField="Email_Id" HeaderText="Email_Id"
SortExpression="Email_Id" />
</Columns>
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:admin %>"
DeleteCommand="DELETE FROM [Exam] WHERE [Id] = @Id"
InsertCommand="INSERT INTO [Exam] ([Name], [Mobile_No], [Email_Id]) VALUES (@Name, @Mobile_No, @Email_Id)"
SelectCommand="SELECT * FROM [Exam]"
UpdateCommand="UPDATE [Exam] SET [Name] = @Name, [Mobile_No] = @Mobile_No, [Email_Id] = @Email_Id WHERE [Id] = @Id">
<DeleteParameters>
<asp:Parameter Name="Id" Type="Int64" />
</DeleteParameters>
<InsertParameters>
<asp:Parameter Name="Name" Type="String" />
<asp:Parameter Name="Mobile_No" Type="String" />
<asp:Parameter Name="Email_Id" Type="String" />
</InsertParameters>
<UpdateParameters>
<asp:Parameter Name="Name" Type="String" />
<asp:Parameter Name="Mobile_No" Type="String" />
<asp:Parameter Name="Email_Id" Type="String" />
<asp:Parameter Name="Id" Type="Int64" />
</UpdateParameters>
</asp:SqlDataSource>
</form>
Step 3 : Double Click On Button and insert Following Code on in .CS Page :-
Insert Code In Namespace On CS Page ....
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["asprockers"].ConnectionString);
Code on Insert Button......
protected void txt_Insert_Click(object sender, EventArgs e)
{
if (con.State == ConnectionState.Open)
{
con.Close();
}
con.Open();
SqlCommand cmd = new SqlCommand("insert into Exam(Name,Mobile_No,Email_Id) values(@Name,@Mobile_No,@Email_Id)", con);
cmd.Parameters.AddWithValue("@Name", TextBox1.Text);
cmd.Parameters.AddWithValue("@Mobile_No", TextBox2.Text);
cmd.Parameters.AddWithValue("@Email_Id", TextBox3.Text);
cmd.ExecuteNonQuery();
con.Close();
TextBox1.Text = "";
TextBox2.Text = "";
TextBox3.Text = "";
GridView1.DataBind();
}
Code on Gridview Row Command Event...
if (e.CommandName == "Edits")
{
string ss= e.CommandArgument.ToString();
if (con.State == ConnectionState.Open)
{
con.Close();
}
SqlCommand cmd = new SqlCommand("select * from Exam where Id='" + ss.ToString() + "'", con);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
da.Fill(dt);
if (dt.Rows.Count > 0)
{
TextBox1.Text = dt.Rows[0]["Name"].ToString();
TextBox2.Text = dt.Rows[0]["Mobile_No"].ToString();
TextBox3.Text = dt.Rows[0]["Email_Id"].ToString();
}
No comments:
Post a Comment