Solved

SQLDataAdapter's

Posted on 2008-10-03
18
688 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
Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

 
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

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

Suggested Solutions

In .NET 2.0, Microsoft introduced the Web Site.  This was the default way to create a web Project in Visual Studio 2005.  In Visual Studio 2008, the Web Application has been restored as the default web Project in Visual Studio/.NET 3.x The Web Si…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…

785 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