Solved

SQLDataAdapter's

Posted on 2008-10-03
18
664 Views
Last Modified: 2012-06-27
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
Comment
Question by:asp_net2
  • 10
  • 7
18 Comments
 
LVL 96

Expert Comment

by:Bob Learned
ID: 22635273
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
 
LVL 4

Author Comment

by:asp_net2
ID: 22636113
That does look nice but for someone like me can you tell me what that would look like with my example. Thank you!
0
 
LVL 1

Expert Comment

by:rclint
ID: 22636269



Private Sub BindGrid()

departmentsGrid.DataSource = GetStoredProcedureData(connectionString, "ProcedureName")

departmentsGrid.DataBind()

End Sub

Open in new window

0
 
LVL 1

Expert Comment

by:rclint
ID: 22636291
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
 
LVL 4

Author Comment

by:asp_net2
ID: 22636422
TheLearnedOne,

What if i have more than one DataAdapter? How would your script look then?
0
 
LVL 1

Expert Comment

by:rclint
ID: 22636485
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
 
LVL 4

Author Comment

by:asp_net2
ID: 22636720
No i meant multiple DataTables but then binding different data to each different DataTable using multiple DataAdapters.
0
 
LVL 1

Expert Comment

by:rclint
ID: 22636832
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
 
LVL 4

Author Comment

by:asp_net2
ID: 22637212
But how do i declare more than one DataAdapter and can you shoe me that? That's what i was referring to.
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 1

Expert Comment

by:rclint
ID: 22637236
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
 
LVL 4

Author Comment

by:asp_net2
ID: 22637343
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
 
LVL 4

Author Comment

by:asp_net2
ID: 22637399
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
 
LVL 1

Expert Comment

by:rclint
ID: 22649480
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
 
LVL 4

Author Comment

by:asp_net2
ID: 22651145
Do i still need to use an SQLCommand if i use an SQLDataApapter? Do i need both or just one?
0
 
LVL 4

Author Comment

by:asp_net2
ID: 22651247
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
 
LVL 1

Accepted Solution

by:
rclint earned 500 total points
ID: 22651673
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
 
LVL 4

Author Comment

by:asp_net2
ID: 22660914
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
 
LVL 4

Author Comment

by:asp_net2
ID: 22672341
rclint,

Can you answer my post 22660914?
0

Featured Post

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Suggested Solutions

One of the pain points with developing AJAX, JavaScript, JQuery, and other client-side behaviors is that JavaScript doesn’t allow for cross domain request for pulling content. For example, JavaScript code on www.johnchapman.name could not pull conte…
Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

746 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now