Solved

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

Posted on 2004-04-22
15
2,313 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
[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
  • 8
  • 4
  • 2
  • +1
15 Comments
 
LVL 52

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 52

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
SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

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

Get Actionable Data from Your Monitoring Solution

Your communication platform is only as good as the relevance of the information you send. Ensure your alerts get to the right people every time with actionable responses. Create escalation rules that ensure everyone follows the process and nothing is left to chance.

Question has a verified solution.

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

IP addresses can be stored in a database in any of several ways.  These ways may vary based on the volume of the data.  I was dealing with quite a large amount of data for user authentication purpose, and needed a way to minimize the storage.   …
More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
There are cases when e.g. an IT administrator wants to have full access and view into selected mailboxes on Exchange server, directly from his own email account in Outlook or Outlook Web Access. This proves useful when for example administrator want…
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.…

695 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