Hi, in this post I mentioned that how to insert using Linq in asp.net with c#. So for this purpose you have to follow instruction as below.
First of all take one LinqtoSql file,by right click on project and select addnewitem and add the SqltoLinq file give the name LinqDemo to this file, when you add it display one dialog box click on yes on that box.
Now your solution explorer looks like below, your LinqtoSql file place under App_Code folder automatically highlighted in below image.
Now in code behind file do the code as follow. In Add Button Click we insert the record in database.
First of all take one LinqtoSql file,by right click on project and select addnewitem and add the SqltoLinq file give the name LinqDemo to this file, when you add it display one dialog box click on yes on that box.
Now your solution explorer looks like below, your LinqtoSql file place under App_Code folder automatically highlighted in below image.
Now take one .aspx page and design as follow.
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="LinqSimple.aspx.cs" Inherits="LinqSimple" %> <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"></head> <body> <form id="form1" runat="server"> <div> <table> <tr> <td> <asp:Label ID="lblname" runat="server" Text="Student Name"></asp:Label> </td> <td> <asp:TextBox ID="txtname" runat="server"></asp:TextBox> </td> <td> <asp:Label ID="lblstd" runat="server" Text="Standard"></asp:Label> </td> <td> <asp:TextBox ID="txtstd" runat="server"></asp:TextBox> </td> </tr> <tr> <td> <asp:Label ID="lblcnt" runat="server" Text="ContactNo"></asp:Label> </td> <td> <asp:TextBox ID="txtcnt" runat="server"></asp:TextBox> </td> <td> <asp:Label ID="lblresult" runat="server" Text="Result"></asp:Label> </td> <td> <asp:TextBox ID="txtresult" runat="server"></asp:TextBox> </td> </tr> <tr> <td> <asp:Button ID="btnadd" runat="server" Text="Add" OnClick="btnadd_Click" /> </td> </tr> </table> </div> </form> </body> </html>
Your Page Looks like below.
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; public partial class LinqSimple : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { } protected void btnadd_Click(object sender, EventArgs e) { using (LinqDemoDataContext database = new LinqDemoDataContext()) { Student s=new Student() { name=txtname.Text, std=txtstd.Text, cntno=Convert.ToInt32(txtcnt.Text), result=txtresult.Text }; database.Students.InsertOnSubmit(s); database.SubmitChanges(); } } }Hope it will help you.
0 comments:
Post a Comment