punjabtechnicaluniversity.blogspot.in
Twisted Form of Question :- How can we use dataadapter to fill a dataset ?
Private Sub LoadData()
Dim strConnectionString As String
strConnectionString = AppSettings.Item(“ConnectionString”)
Dim objConn As New SqlConnection(strConnectionString)
objConn.Open()
Dim objCommand As New SqlCommand(“Select FirstName from
Employees”)
objCommand.Connection = objConn
Dim objDataAdapter As New SqlDataAdapter()
objDataAdapter.SelectCommand = objCommand
Dim objDataSet As New DataSet
End Sub
Dim objConn As New SqlConnection(strConnectionString)
objConn.Open()
First step is to open the connection.Again note the connection string is loaded from config file.
Dim objCommand As New SqlCommand(“Select FirstName from Employees”)
objCommand.Connection = objConn
Dim objDataAdapter As New SqlDataAdapter()
objDataAdapter.SelectCommand = objCommand
Third steps is to create the Adapter object and pass the command object to the adapter object.
objDataAdapter.Fill(objDataSet)
Fourth step is to load the dataset using the “Fill” method of the dataadapter.
lstData.DataSource = objDataSet.Tables(0).DefaultView
lstData.DisplayMember = “FirstName”
lstData.ValueMember = “FirstName”
Twisted Form of Question :- How can we use dataadapter to fill a dataset ?
Sample code is provided in “WindowsDataSetSample” folder in CD.”LoadData” has all the implementation of connecting and loading to dataset.This dataset is finally binded to a ListBox.Below is the sample code.
Private Sub LoadData()
Dim strConnectionString As String
strConnectionString = AppSettings.Item(“ConnectionString”)
Dim objConn As New SqlConnection(strConnectionString)
objConn.Open()
Dim objCommand As New SqlCommand(“Select FirstName from
Employees”)
objCommand.Connection = objConn
Dim objDataAdapter As New SqlDataAdapter()
objDataAdapter.SelectCommand = objCommand
Dim objDataSet As New DataSet
End Sub
In such type of question’s interviewer is looking from practical angle , that have you worked with dataset and datadapters.Let me try to explain the above code first and then we move to what steps to say suring interview.
objConn.Open()
First step is to open the connection.Again note the connection string is loaded from config file.
Dim objCommand As New SqlCommand(“Select FirstName from Employees”)
objCommand.Connection = objConn
Second step is to create a command object with appropriate SQL and set the connection object to this command.
objDataAdapter.SelectCommand = objCommand
Third steps is to create the Adapter object and pass the command object to the adapter object.
objDataAdapter.Fill(objDataSet)
Fourth step is to load the dataset using the “Fill” method of the dataadapter.
lstData.DataSource = objDataSet.Tables(0).DefaultView
lstData.DisplayMember = “FirstName”
lstData.ValueMember = “FirstName”
Fifth step is to bind to the loaded dataset with the GUI.At this moment sample has listbox as the UI.Binding of the UI is done by using DefaultView of the dataset.Just to revise every dataset has tables and every table has views.In this sample we have only loaded one table i.e. Employees table so we are referring that with a index of zero.
0 comments:
Post a Comment
North India Campus