Solved

SQLDataAdapter's

Posted on 2008-10-03
18
718 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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
 
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

Enroll in June's Course of the Month

June’s Course of the Month is now available! Experts Exchange’s Premium Members, Team Accounts, and Qualified Experts have access to a complimentary course each month as part of their membership—an extra way to sharpen your skills and increase training.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I have developed many web applications with asp & asp.net and to add and use a dropdownlist was always a very simple task, but with the new asp.net, setting the value is a bit tricky and its not similar to the old traditional method. So in this a…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
In this video, viewers will be given step by step instructions on adjusting mouse, pointer and cursor visibility in Microsoft Windows 10. The video seeks to educate those who are struggling with the new Windows 10 Graphical User Interface. Change Cu…
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…

691 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