Click here to Skip to main content
15,894,410 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a datagrid and when it loads all the records are fetched from database and displayed in the datagrid. In the form I also have functionality to search the record by id. but I want to get it done by using LINQ i.e it should search from datagrid and display the record of that id which I enter. Below is the code for fetching the record by id using datareader.
-----------------------------------------------------------------
code:
-----------------------------------------------------------------
VB
Private Sub btnsearch_Click(sender As Object, e As EventArgs) Handles btnsearch.Click
       DGV_empno.Rows.Clear()
       Try
           con.Open()
           com = New SqlCommand("SELECT * from emp_master where empid=@empid", con)
           com.Parameters.AddWithValue("@empid", txtempno.Text)
           dr = com.ExecuteReader
           While dr.Read
               i = DGV_empno.Rows.Add(+1)
               DGV_empno.Rows(i).Cells(0).Value = dr("empid")
               DGV_empno.Rows(i).Cells(1).Value = dr("empname")
               DGV_empno.Rows(i).Cells(2).Value = dr("dob")
               DGV_empno.Rows(i).Cells(3).Value = dr("doj")
               DGV_empno.Rows(i).Cells(4).Value = dr("dept")
               DGV_empno.Rows(i).Cells(5).Value = dr("desig")
               DGV_empno.Rows(i).Cells(6).Value = dr("exp")
               DGV_empno.Rows(i).Cells(7).Value = dr("salary")
           End While

       Catch ex As Exception
           MsgBox(ex.Message)
       Finally
           dr.Close()
           con.Close()
       End Try


   End Sub
Posted
Comments
ZurdoDev 17-Mar-15 12:29pm    
Are you asking someone to re-write this using LINQ? It is not clear what you are asking.
A94 17-Mar-15 12:36pm    
First of all the records as fetched in datagrid using stored procedure when the form loads. I have added a functionality by which a record can be searched by using the id. When the user enters id, the record should be searched in datagrid and it should be displayed using LINQ...

1 solution

I have no idea what stops you on using Linq ;)

Have a look here:
Introduction to LINQ in Visual Basic[^]
LINQ to SQL in VB.NET[^]
Using LINQ to SQL in Visual Basic[^]
Querying a Database with LINQ to SQL Using Visual Basic 2010[^]
LINQ - Sample Queries (101 samples)[^]

I hope this set of articles helps you to write proper code ;)
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900