Quantcast
Channel: Shailendra Saxena
Viewing all articles
Browse latest Browse all 18

Linq To SQL Step By Step in VB.Net

$
0
0
Hi,
Following are steps to  Linq To SQL Step By Step in  VB.Net.


Step 1. Create a New Web Project.
Step 2.Add new item à Dataà Linq To SQL Classes.Name[ConDB.dbml]
Step 3. Add DataBase à NorthWind.Add Tables and Stored Procedures.
Step 4. Add New Web Form Name[LINQTOSQL]
Step5. Add controls  in Winform as Below.
Step 6. Add the below code in codebehind.
Public Class LinqToSql
    Inherits System.Web.UI.Page
    Dim dbcon As New ConDBDataContext()
    Protected Sub Page_Load(ByVal sender As ObjectByVal e As System.EventArgsHandles Me.Load
        If Not IsPostBack Then
            FillDropDownlist()
        End If
    End Sub
    Private Sub FillDropDownlist()
        ''For Filling the Dropdwonlist
        Dim custQuery = _
        From cust In dbcon.Customers _
        Select cust.CustomerID
        Order By CustomerID
        DropDownList1.DataSource = custQuery
        DropDownList1.DataBind()
    End Sub

    Protected Sub CMDGo_Click(ByVal sender As ObjectByVal e As EventArgsHandles CMDGo.Click
        ''For Showing the Company name on the basis of selected customerid 
        Dim custQuery = _
        From cust In dbcon.Customers _
        Where cust.CustomerID = DropDownList1.Text
        Select cust.CompanyName

        For Each companyname In custQuery
            txtcustomername.Text = companyname
        Next

    End Sub

    Protected Sub CMDUpdate_Click(ByVal sender As ObjectByVal e As EventArgsHandles CMDUpdate.Click
        ''For Updating the data
        Dim custQuery = _
        (From cust In dbcon.Customers _
        Where cust.CustomerID = DropDownList1.Text
                Select cust)

        For Each Customer In custQuery
            'If Customer.CustomerID = DropDownList1.Text Then
            Customer.CompanyName = txtcustomername.Text
            ' End If
        Next
        dbcon.SubmitChanges()
        FillDropDownlist()
    End Sub

    Protected Sub CMDAdd_Click(ByVal sender As ObjectByVal e As EventArgsHandles CMDAdd.Click
        '' For Adding New Data
        Dim cust As New Customer With {.CompanyName = txtcustomername.Text, _
         .City = "London", .CustomerID = txtCompanyid.Text, .PostalCode = 55555, .Phone = "555-555-5555"}
        dbcon.Customers.InsertOnSubmit(cust)
        dbcon.SubmitChanges()
        FillDropDownlist()
    End Sub

    Protected Sub CMDDel_Click(ByVal sender As ObjectByVal e As EventArgsHandles CMDDel.Click
        ''For Deleting the Data
        Dim DelQry = From cust In dbcon.Customers _
        Where cust.CustomerID = DropDownList1.Text _
        Select cust

        If DelQry.Count > 0 Then
            dbcon.Customers.DeleteOnSubmit(DelQry.First)
            dbcon.SubmitChanges()
            FillDropDownlist()
        End If

    End Sub

    Protected Sub CMDExecProc_Click(ByVal sender As ObjectByVal e As EventArgsHandles CMDExecProc.Click
        ''For Filling the Gridview by executing the Stored Proc.
        Dim sprocqry = dbcon.SalesByCategory("seafood""1998")
        GridView1.DataSource = sprocqry
        GridView1.DataBind()
    End Sub
End Class



Viewing all articles
Browse latest Browse all 18

Trending Articles