steps:
1) Create the simple web application (asp.net with C#).
2) Create new table (User) in your database.
Column Name | Data Type |
UserId | int (Pk and AutoIncrement) |
UserName | varchar(50) |
Address1 | varchar(50) |
EmailAddress | varchar(50) |
3) In your application (.aspx file) put the below code. we have first true the property of gridview (ShowFooter) and put the asp control in to the FooterTemplate. put the one label for display message.Here I not implement the edit command.
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="UserID" CellPadding="4" ForeColor="#333333" GridLines="None" ShowFooter="True"> <Columns> <asp:TemplateField HeaderText="UserID" InsertVisible="False" SortExpression="UserID"> <ItemTemplate> <asp:Label ID="lblUserID" runat="server" Text='<%# Eval("UserID") %>'></asp:Label> </ItemTemplate> </asp:TemplateField> <asp:TemplateField HeaderText="UserName" SortExpression="UserName"> <EditItemTemplate> <asp:TextBox ID="txtUserName" runat="server" Text='<%# Bind("UserName") %>'></asp:TextBox> </EditItemTemplate> <ItemTemplate> <asp:Label ID="lblUserName" runat="server" Text='<%# Eval("UserName") %>'></asp:Label> </ItemTemplate> <FooterTemplate> <asp:TextBox ID="txtfooterUserName" runat="server"></asp:TextBox> <%-- Here we Put the RequiredFieldValidator for textbox (txtfooterUserName) --%> <asp:RequiredFieldValidator ID="rfvUserName" runat="server" ControlToValidate="txtfooterUserName" ErrorMessage="*"></asp:RequiredFieldValidator> </FooterTemplate> </asp:TemplateField> <asp:TemplateField HeaderText="Address1" SortExpression="Address1"> <EditItemTemplate> <asp:TextBox ID="txtAddress1" runat="server" Text='<%# Bind("Address1") %>'></asp:TextBox> </EditItemTemplate> <ItemTemplate> <asp:Label ID="lblAddress1" runat="server" Text='<%# Eval("Address1") %>'></asp:Label> </ItemTemplate> <FooterTemplate> <asp:TextBox ID="txtfooterAddress1" runat="server"></asp:TextBox> </FooterTemplate> </asp:TemplateField> <asp:TemplateField HeaderText="EmailAddress" SortExpression="EmailAddress"> <EditItemTemplate> <asp:TextBox ID="txtAddress1" runat="server" Text='<%# Bind("EmailAddress") %>'></asp:TextBox> </EditItemTemplate> <ItemTemplate> <asp:Label ID="lblEmailAddress" runat="server" Text='<%# Eval("EmailAddress") %>'></asp:Label> </ItemTemplate> <FooterTemplate> <asp:TextBox ID="txtfooterEmailAddress" runat="server"></asp:TextBox> <asp:RequiredFieldValidator ID="rfvEmailAddress" runat="server" ControlToValidate="txtfooterEmailAddress" ErrorMessage="*"></asp:RequiredFieldValidator> <asp:RegularExpressionValidator ID="revEmailAddress" runat="server" ControlToValidate="txtfooterEmailAddress" ValidationExpression="\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*"
ErrorMessage="Please Enter Valid Email Address" ></asp:RegularExpressionValidator> </FooterTemplate> </asp:TemplateField> <asp:TemplateField HeaderText="EditRecord"> <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="Edit" Text="Edit"></asp:LinkButton> </ItemTemplate> <FooterTemplate> <asp:LinkButton ID="lnkInsert" runat="server" Text ="Insert" OnClick="lnkInsert_Click"></asp:LinkButton> </FooterTemplate> </asp:TemplateField> </Columns> <FooterStyle BackColor="Silver" Font-Bold="True" ForeColor="White" /> <RowStyle BackColor="#EFF3FB" /> <PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" /> <SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" /> <HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" /> <EditRowStyle BackColor="#2461BF" /> <AlternatingRowStyle BackColor="White" /> </asp:GridView><asp:Label ID="lblMessage" runat="server" Text ="Insert Record Successfully" Visible ="false" > </asp:Label>
4) Open Code Behind File and on the page load event put the below code.
protected void Page_Load(object sender, EventArgs e) { //check whether page is postback or not //if page is not postback at that time we bind the gridview if (!Page.IsPostBack) { //call the function BindGridView BindGridView(); } } public void BindGridView() { //here write connection string string strsql = DBConnection.sqlstr; //create object for sqlconnection SqlConnection sqlcon = new SqlConnection(strsql); //here i use the query //create the object of sqlcommand SqlCommand sqlcmd = new SqlCommand("select * from [User]", sqlcon); //create sqldataadapter object and give the sqlcommand as parameter SqlDataAdapter adp = new SqlDataAdapter(sqlcmd); //declare the dataset DataSet ds = new DataSet(); //fill dataset using fill method of SqlDataAdapter adp.Fill(ds); //bind the GridView1 GridView1.DataSource = ds.Tables[0]; GridView1.DataBind(); }
5) On Click Event of the Insert button put the below code or directly paste below code.
protected void lnkInsert_Click(object sender, EventArgs e) { string strsql = DBConnection.sqlstr; //create object for sqlconnection SqlConnection sqlcon = new SqlConnection(strsql); //here i use the query string strQuery = "Insert into [User] (UserName,Address1,EmailAddress) values (@UserName,@Address1,@EmailAddress)"; //create the object of sqlcommand SqlCommand sqlcmd = new SqlCommand(strQuery, sqlcon); //find the footer row for UserName And Assign the value sqlcmd.Parameters.Add("@UserName", SqlDbType.VarChar, 50).Value = ((TextBox)GridView1.FooterRow.FindControl("txtfooterUserName")).Text; sqlcmd.Parameters.Add("@Address1", SqlDbType.VarChar, 50).Value = ((TextBox)GridView1.FooterRow.FindControl("txtfooterAddress1")).Text; sqlcmd.Parameters.Add("@EmailAddress", SqlDbType.VarChar, 50).Value = ((TextBox)GridView1.FooterRow.FindControl("txtfooterEmailAddress")).Text; //open the sql connection sqlcon.Open(); // Execute the Insert Command In to Database sqlcmd.ExecuteNonQuery(); //close the sqlconnection sqlcon.Close(); //if record successfully insert we display the message lblMessage.Visible = true; //bind grid view for latest record BindGridView(); }
Check Application.
Thanks.
Leave a reply to Kevin Cancel reply