Search Your Dot Net Topic

Sunday, 18 April 2021

linq to sql insert record, linq to sql add record,, linq to sql insert...




Step by step guide:

1. Create project .

2. Add a aspx page --- default.aspx

3. Add LINQ TO SQL class. Visual Studio asked to create in APP_CODE 
folder.

4. VIEW option click on SERVER EXPLORER or press [CTRL +W + L]

5. Server Explorer you can see now.

6. On Server Explorer Right click   DATA CONNECTIONS --->Add 
Connections.

7. As you connection established, now you click on Database you had added in connection then click on TABLES expand it this section.

8. Switch to Solution Explorer double click on DBML file which is located inside APP_CODE .

9. Drag your table on the DBML canvas.

10. As you completed drag operation you can see Web.Config file auto updated with CONNECTION STRING.

Now copy following code and enjoy.

I provide Asp.Net WebForm, MVC, Core training. You can me contact for further guidance.

Mobile : 9869166077
Email :  manojbkalla@hotmail.com // mr.manojbkalla@gmail.com


CODE:   Default.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="asp" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
       
<table>
           <tr>
               <td>
                   Friend Name
               </td>
               <td>
                   <asp:TextBox ID="txtFriendName" runat="server"></asp:TextBox>
               </td>
           </tr>

           <tr>
               <td>
                   Mobile 
               </td>
               <td>
                   <asp:TextBox ID="txtMobile" runat="server"></asp:TextBox>
               </td>
           </tr>
    <tr>
        <td colspan="2">
            <asp:Button ID="btnSubmit" runat="server" OnClick="btnSubmit_Click" Text="Submit" />
        </td>
    </tr>
       </table>

    </div>
    </form>
</body>
</html>





CODE :    Default.aspx.cs 


using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }

    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        FriendDataClassesDataContext db = new FriendDataClassesDataContext();
        dbFriend newfriend = new dbFriend();
        newfriend.FriendName = txtFriendName.Text;
        newfriend.Mobile = txtMobile.Text;
        db.dbFriends.InsertOnSubmit(newfriend);
        db.SubmitChanges();
    }
}






CODE :  web.config
<?xml version="1.0"?>
<!--
  For more information on how to configure your ASP.NET application, please visit
  http://go.microsoft.com/fwlink/?LinkId=169433
  -->
<configuration>
  
  <connectionStrings>
    <add name="dbLearningConnectionString" connectionString="Data Source=DESKTOP-IJ20K2E\SQLEXPRESS;Initial Catalog=dbLearning;Integrated Security=True"
      providerName="System.Data.SqlClient" />
  </connectionStrings>
  <system.web>
    <compilation debug="true" targetFramework="4.5.2">
      <assemblies>
        <add assembly="System.Data.Linq, Version=4.0.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089"/>
      </assemblies>
    </compilation>
    <httpRuntime targetFramework="4.5.2"/>
  </system.web>
</configuration>



CODE:  SQL SCRIPT TO CREATE TABLE

/****** Object:  Table [dbo].[dbFriends]    Script Date: 24-04-2021 21:01:20 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

CREATE TABLE [dbo].[dbFriends](
[FriendID] [int] IDENTITY(1,1) NOT NULL,
[FriendName] [varchar](100) NULL,
[Mobile] [varchar](50) NULL,
 CONSTRAINT [PK_dbFriends] PRIMARY KEY CLUSTERED 
(
[FriendID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON, OPTIMIZE_FOR_SEQUENTIAL_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY]
GO


No comments:

Post a Comment