Click here to Skip to main content
15,894,106 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a salary_master table. And I want to fetch those data from database into the datagrid-view when the form loads using dataset.So, how to fetch only required data (i.e some of them but not all) from dataset and also keep the headers of gridview that I have defined as it loads the the columns also from database. below is the code for fetching data into the gridiew. Also which is better : dataset or using data-reader
---------------------------------------------------------------------
code:
---------------------------------------------------------------------
VB
Private Sub Load_Data_Grid()

        Try
            con.Open()
            ss = "SELECT * from salary_master"
            com = New SqlCommand(ss, con)
            da = New SqlDataAdapter(com)
            da.Fill(ds, "salary_master")
            DGV_Salary.DataSource = ds.Tables(0)
            com.ExecuteNonQuery()
        Catch ex As Exception
            MsgBox(ex.Message)
        Finally
            con.Close()
        End Try
     
    End Sub

--------------------------------------------------------------------
and in Load event I have called the above procedure...
Posted
Updated 9-Mar-15 7:03am
v2
Comments
CHill60 9-Mar-15 13:13pm    
When you say "some of them but not all" do you mean some rows or some columns?
A94 10-Mar-15 3:00am    
I mean some columns like id,name,designation,salary..

1 solution

You can create sql parameters that would limit the number of records returned, for example:
Dim con As New SqlConnection(sqlConnectionString)
      Try
          con.Open()
          Dim ss As String = "SELECT * from myTable where id=@id"
          Dim com As New SqlCommand(ss, con)
          Dim da As New SqlDataAdapter(com)


          Dim param As New SqlParameter
          param.SqlDbType = SqlDbType.Int
          param.ParameterName = "@id"
          param.Value = 7200
          da.SelectCommand.Parameters.Add(param)


          Dim ds As New DataSet
          da.Fill(ds, "my_table")
          dg.DataSource = ds.Tables(0)
          com.ExecuteNonQuery()
      Catch ex As Exception
          MsgBox(ex.Message)
      Finally
          con.Close()
      End Try


Look here for further info:

https://msdn.microsoft.com/en-us/library/ms254953(v=vs.110).aspx[^]
 
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