Solved

Fill a dataset with a stored procedure (VB.NET)

Posted on 2004-04-22
15
2,306 Views
Last Modified: 2008-09-23
Hi, I can't find the syntax for filling a dataset. I can create a SqlDataCommand object and bind a datagrid to it, but I can't seem to get it into a dataset.
Thanks!
0
Comment
Question by:sumo_the_cat
  • 8
  • 4
  • 2
  • +1
15 Comments
 
LVL 49

Accepted Solution

by:
Ryan Chong earned 100 total points
ID: 10892719
Example using CommandType.StoredProcedure with Parameter:

Dim conn As SqlConnection = New SqlConnection(appSettings.GetSetting("constring"))
        Dim cmdSP As SqlCommand = New SqlCommand("MySP", conn)

        cmdSP.CommandType = CommandType.StoredProcedure

        Dim RetValue As SqlParameter = cmdSP.Parameters.Add("@EnterMonth", SqlDbType.NChar, 2)
        RetValue.Value = "07"

        conn.Open()

        Dim da As New SqlDataAdapter(cmdSP)
        Dim ds As DataSet = New DataSet()
        da.Fill(ds, selectedSP)

        ' Attach dataset's DefaultView to the DGrid control
        DGrid.DataSource = ds.Tables(0)
        DGrid.CaptionText = ds.Tables(0).TableName & ": " & ds.Tables(0).Rows.Count & " records found."

        conn.Close()

Try customize according to your requirement. cheers
0
 
LVL 49

Expert Comment

by:Ryan Chong
ID: 10892737
oops, try change the line of :

Dim cmdSP As SqlCommand = New SqlCommand("MySP", conn)

to

Dim cmdSP As SqlCommand = New SqlCommand(selectedSP, conn)

then define:

Dim selectedSP As String = "MyStoredProcedure"

Hope this helps

0
 
LVL 2

Author Comment

by:sumo_the_cat
ID: 10892802
Do you need to use a DataAdapter? I thought that one of the points of using sprocs was that it cuts out the middle man.
0
 
LVL 10

Expert Comment

by:EBatista
ID: 10893091
well, its not absolutly necessary to use dataadapter, you could use an datareader instead:
DataGrid1.DataSource=yourCommand.ExecuteReader()
0
 
LVL 2

Author Comment

by:sumo_the_cat
ID: 10893435
DataGrid1.DataSource=yourCommand.ExecuteReader()

I'm sure that's the first thing I tried! I'll let you know when I get back in a couple of hours!
0
 
LVL 2

Author Comment

by:sumo_the_cat
ID: 10894489
Whoops, misread that in my haste to get to the door. EBatista, I want to put the data into a dataset, not a datagrid.
0
 
LVL 96

Assisted Solution

by:Bob Learned
Bob Learned earned 100 total points
ID: 10894949
You can use all the code, or you can use the Data Access Application Block from Microsoft:

Data Access Application Block for .NET
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnbda/html/daab-rm.asp

(1) Download the DLL
(2) Add a reference
(3) Syntax would be:

Dim ds As DataSet = SqlHelper.ExecuteDataset( _
      "SERVER=(local);DATABASE=Northwind;INTEGRATED SECURITY=True;", _    
      CommandType.StoredProcedure, "SalesByCategory")

(4) There are a lot of other options to make life a lot easier.

Bob

0
Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

 
LVL 10

Assisted Solution

by:EBatista
EBatista earned 100 total points
ID: 10896111
then, if your data comes from a database you have to use the DataAdapter to fill a DataSet.
I think you are a bit confused about the DataAdapters and stored procedures. DataAdapters have a string property called SelectCommand, that property is used by the DataAdapter when you call its Fill method to fill a dataset, that property can be either a sql string or a command object (SqlCommand or OleDbCommand), even it could be the stored procedure name you built in the database. All that means that DataAdapters and sprocs work together to fill a dataset:
Dim yourDataAdapter as new SqlDataAdapter()
yourDataAdapter.SelectCommand="storeProcedureName" 'or Command name or sql query string
' it is the same to say:
' Dim yourDataAdapter as New DataAdapter("sprocName")
yourDataAdapter.Fill(yourDataSet,"yourDataTableName")


but if you want to fill the dataset with your own data, ie: data coming from controls or any other source you can fill the dataset manually:
Dim theDataSet as New DataSet()
Dim theDataTable as new DataTable
Dim theRow as DataRow

'Adding columns to the DataTable
theDataTable.Columns.Add("UserId")
theDataTable.Columns.Add("UserName")

'Adding Rows
theRow=theDataTable.NewRow()
theRow("UserId")=TextBox1.Text
theRow("UserName")=TextBox2.Text

'Adding the DataTable to the DataSet
theDataSet.Tables.Add(theDataTable)

yourDataSet.Tables.Add(yourDataTable)

hope all this clarify a bit
0
 
LVL 2

Author Comment

by:sumo_the_cat
ID: 10897373
Ahh. Thanks for that.

TheLearnedOne, that looks brilliant. But I'm being a bit stupid - I can't find the Microsoft.ApplicationBlocks.Data.dll after downloading this. The help just says "Set a reference to it" - but I can't find it (even after searching my harddrive!)
0
 
LVL 2

Author Comment

by:sumo_the_cat
ID: 10902789
Had to build the project which compiled into a .dll. Then referenced it. Thanks for that golden nugget, LearnedOne.
0
 
LVL 2

Author Comment

by:sumo_the_cat
ID: 10902826
ryancys answered the question I actually asked, though didn't quite mean, so s/he gets the accepted answer.
Cheers averyone.
0
 
LVL 10

Expert Comment

by:EBatista
ID: 10903132
look for the dll file name you just downloaded (fileName.dll) and paste it in the bin directory, then right click the Reference node in the Solution Explorer and hit the Add Reference command,a dialog box should appears, select the Project tab and hit the Browse button to navigate to the bin directory and then select the fileName.dll file.
0
 
LVL 2

Author Comment

by:sumo_the_cat
ID: 10903183
like i said, had to compile the project to make the .dll - no dll provided by M$. BTW, you wouldn't need to paste to the bin dir anyway, since adding a reference to a project copies all the necessary files to your bin dir automatically.
0
 
LVL 10

Expert Comment

by:EBatista
ID: 10903452
sumo, i have posted my comment before notice that you have closed this thread

...yes it is no necesary to copy the project manually, it is just that i use to copy the assamble directly to the bin folder when it comes from a download, but when i have to program it my self i do the usual as you do.
0
 
LVL 2

Author Comment

by:sumo_the_cat
ID: 10905034
cool!
0

Featured Post

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

A basic question.. “What is the Garbage Collector?” The usual answer given back: “Garbage collector is a background thread run by the CLR for freeing up the memory space used by the objects which are no longer used by the program.” I wondered …
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

758 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

21 Experts available now in Live!

Get 1:1 Help Now