Solved

SQLDataAdapter's

Posted on 2008-10-03
18
675 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
SignalR - getting new data from sql DB 5 79
Display mvc partial view with errors on parent page 1 26
c# LinkButton OnClientClick 2 34
Run time Error 4 34
In this Article, I will provide a few tips in problem and solution manner. Opening an ASPX page in Visual studio 2003 is very slow. To make it fast, please do follow below steps:   Open the Solution/Project. Right click the ASPX file to b…
In an ASP.NET application, I faced some technical problems. In this article, I list them out and show the solutions that I found.  I hope it will be useful. Problem: After closing a pop-up window, the parent page should be refreshed automaticall…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

914 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