• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 739
  • Last Modified:

SQLDataAdapter's

How can i make the following work by using a Stored Procedure instead of using an SQL Query String?

Private Sub BindGrid()
Dim conn As SqlConnection
Dim dataSet As New DataSet
Dim adapter As SqlDataAdapter
Dim connectionString As String = _
ConfigurationManager.ConnectionStrings( _
"DBTest").ConnectionString
conn = New SqlConnection(connectionString)
adapter = New SqlDataAdapter( _
"SELECT DepartmentID, Department FROM Departments", _
conn)
adapter.Fill(dataSet, "Departments")
departmentsGrid.DataSource = dataSet
departmentsGrid.DataBind()
End Sub
0
asp_net2
Asked:
asp_net2
  • 10
  • 7
1 Solution
 
Bob LearnedCommented:
Something like this would be better:


    Public Function GetStoredProcedureData(ByVal connectionStringKey As String, ByVal procedureName As String) As DataTable
        Dim dt As New DataTable()
        Dim connectionString As String = ConfigurationManager.ConnectionStrings(connectionStringKey).ConnectionString
        Using adapter As New SqlDataAdapter(procedureName, connectionString)
            adapter.SelectCommand.CommandType = CommandType.StoredProcedure
            adapter.Fill(dt)
            Return dt
        End Using
    End Function

Open in new window

0
 
asp_net2Author Commented:
That does look nice but for someone like me can you tell me what that would look like with my example. Thank you!
0
 
rclintCommented:


Private Sub BindGrid()
departmentsGrid.DataSource = GetStoredProcedureData(connectionString, "ProcedureName")
departmentsGrid.DataBind()
End Sub

Open in new window

0
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
rclintCommented:
Oops forgot to initialize the connectionString
Private Sub BindGrid()
Dim connectionString As String = _
ConfigurationManager.ConnectionStrings( _
"DBTest").ConnectionString
departmentsGrid.DataSource = GetStoredProcedureData(connectionString, "ProcedureName")
departmentsGrid.DataBind()
End Sub

Open in new window

0
 
asp_net2Author Commented:
TheLearnedOne,

What if i have more than one DataAdapter? How would your script look then?
0
 
rclintCommented:
From my experience you can only have one dataadapter for each grid that you are trying to fill.  The Learned One's code would only provide one datatable.  Getting multiple datatables into a single grid would require 1. that the data was the same types and 2. a separate function that would combine the two sets of data into one datatable object.
0
 
asp_net2Author Commented:
No i meant multiple DataTables but then binding different data to each different DataTable using multiple DataAdapters.
0
 
rclintCommented:
Are you attempting to attach all the data to a single grid or a separate grid for each datatable?  If the latter then use the code we provided just repeat for as many grids as you have to fill.
Private Sub BindGrid()
Dim connectionString As String = _
ConfigurationManager.ConnectionStrings( _
"DBTest").ConnectionString
departmentsGrid.DataSource = GetStoredProcedureData(connectionString, "ProcedureName")
departmentsGrid.DataBind()
/*Grid 2*/
dept2Grid.DataSource = GetStoredProcedureData(connectionString, "Procedure2Name")
dept2Grid.DataBind()
End Sub

Open in new window

0
 
asp_net2Author Commented:
But how do i declare more than one DataAdapter and can you shoe me that? That's what i was referring to.
0
 
rclintCommented:
The above example does create a separate dataadapter for each grid.  Maybe I am not understanding what you are looking for.  Can you describe what it is that you are attempting to do?
0
 
asp_net2Author Commented:
I would like to create a subroutine or function that has many DataTables in a DataSet. I may need to get data from a Database for each DataTable and i wanted to know how to do this using the DataAdapter and binding the DataAdapter to each Stored Procedure that i have that will retreive data for different DataAdapters.
0
 
asp_net2Author Commented:
TheLearnedOne,

I like your exampe above however don't i need to define the SQL Connection something like the following below?

 Public Function GetStoredProcedureData(ByVal connectionStringKey As String, ByVal procedureName As String) As DataTable

        Define an SQL Connection
       Dim conn As SqlConnection

       ' Define SqlDataAdapter
       Dim adapter As SqlDataAdapter

       ' Initialize connection
       conn = New SqlConnection(connectionString)


        Dim dt As New DataTable()
        Dim connectionString As String = ConfigurationManager.ConnectionStrings(connectionStringKey).ConnectionString
        Using adapter As New SqlDataAdapter(procedureName, connectionString)
            adapter.SelectCommand.CommandType = CommandType.StoredProcedure
            adapter.Fill(dt)
            Return dt
        End Using
    End Function
0
 
rclintCommented:
The connection string is already being passed in (conenctionstringkey).  You do have to create the conn object, as you did, and open it.  I don't know the exact syntax for vb, I use c#.
Public Function GetStoredProcedureData(ByVal connectionStringKey As String, ByVal procedureName As String) As DataTable
 
  'Define an SQL Connection
       Dim conn As SqlConnection
 
       ' Define SqlDataAdapter
       Dim adapter As SqlDataAdapter
 
       ' Initialize connection
       conn = New SqlConnection(connectionStringKey)
 
        Dim dt As New DataTable()
        Using adapter As New SqlDataAdapter(procedureName, conn)
            adapter.SelectCommand.CommandType = CommandType.StoredProcedure
conn.Open
            adapter.Fill(dt)
con.Close
            Return dt
        End Using
    End Function

Open in new window

0
 
asp_net2Author Commented:
Do i still need to use an SQLCommand if i use an SQLDataApapter? Do i need both or just one?
0
 
asp_net2Author Commented:
Please see what i have below. I'm recieving a message at the following lines below that says 'Parameters is not a member of 'System.Data.SqlClient.SqlDataAdapter'

Protected Sub btnLocate_Click(ByVal sender As Object, ByVal e As ImageClickEventArgs) Handles LocateBtn.Click

        Dim connectionString As String = ConfigurationManager.ConnectionStrings("HR").ConnectionString
        Dim conn As SqlConnection = New SqlConnection(connectionString)

        Dim dsPSSA As New DataSet()
        Dim dtStudent As New DataTable("Students")
        Dim dtSites As New DataTable("Sites")
        Dim dtSpecialEd As New DataTable("SpecialEd")
        Dim dtGradeLevel As New DataTable("GradeLevel")
        Dim dtIEP As New DataTable("IEP")
        Dim dtTests As New DataTable("Tests")

        dsPSSA.Tables.AddRange(New DataTable() {dtStudent, dtSites, dtSpecialEd, dtGradeLevel, dtIEP, dtTests})

        Dim daStudents As New SqlDataAdapter("SelectStudent", connectionString)
        daStudents.SelectCommand.CommandType = CommandType.StoredProcedure
        daStudents.Fill(dtStudent)

        Dim daSites As New SqlDataAdapter("SelectSitesList", connectionString)
        daSites.SelectCommand.CommandType = CommandType.StoredProcedure
        daSites.Fill(dtSites)

        Dim daSpecialEd As New SqlDataAdapter("SelectSpecialEdList", connectionString)
        daSpecialEd.SelectCommand.CommandType = CommandType.StoredProcedure
        daSpecialEd.Fill(dtSpecialEd)

        Dim daGradeLevel As New SqlDataAdapter("SelectGradeLevel", connectionString)
        daGradeLevel.SelectCommand.CommandType = CommandType.StoredProcedure
        daGradeLevel.Fill(dtGradeLevel)

        Dim daIEP As New SqlDataAdapter("SelectIEP", connectionString)
        daIEP.SelectCommand.CommandType = CommandType.StoredProcedure
        daIEP.Fill(dtIEP)

        Dim daTests As New SqlDataAdapter("PSSA_SelectTests", connectionString)
        daTests.SelectCommand.CommandType = CommandType.StoredProcedure
        daTests.Fill(dtTests)

        daStudents.Parameters.Add("@student_id", SqlDbType.Int)       <--- message on these lines
        daStudents.Parameters("@student_id").Value = student.Text    <--- message on these lines
 
    End Sub
0
 
rclintCommented:
Dataadapters hold the data.  Commands are used to query for the data to put in to the DA.  The code below is a bit old so it may need updating.  I stopping using VB in framework 1.1.  
Dim conn As New SqlConnection(ConfigurationManager.ConnectionStrings("HR").ConnectionString)
Dim cmdStudents As New SqlCommand
cmdStudents.Connection = conn
cmdStudents.CommandText = "SelectStudent"
cmdStudents.CommandType = CommandType.StoredProcedure
'add an input parameter and supply a value for it
cmdStudents.Parameters.Add("@student_id", SqlDbType.Int, 10)
cmdStudents.Parameters("@student_id").Value = student.Text
Dim dsStudents As New DataSet
Dim daStudents As New SqlDataAdapter
daStudents.SelectCommand = cmdStudents
daStudent.Fill(dsStudents)
     

Open in new window

0
 
asp_net2Author Commented:
Okay, so you do need to use Commands to get the data from the DB and then have the DataAdapters to hold data. I thought that when using the Disconnected data approach that you could just use DataAdapters instead of Commands.
0
 
asp_net2Author Commented:
rclint,

Can you answer my post 22660914?
0

Featured Post

Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

  • 10
  • 7
Tackle projects and never again get stuck behind a technical roadblock.
Join Now