Datagrid based off SP with a parameter

new@vb

New member
Joined
Nov 19, 2009
Messages
1
Programming Experience
Beginner
Hey everyone, I just started doing some coding in VB.Net with Visual Studio 2005. I need some help please! Basically on my form I have a new datagrid called DGBcs, I want this to populate with data from the stored procedure that accepts a parameter from another form. I would like the datagrid to populate once the form is opened. Here is my code right now I get a long error message but saying Procedure 'usp_ReturnWomenHData' expects parameter '@MsfcNumV', which was not supplied, I am pretty sure I am suppling it but I must have it wrong somewhere. Thanks for all your help! :D


Private Sub FrmWomensHealth_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim MyConnection As New SqlConnection My.Settings.MemberManagerConnectionString)

Dim MyCommand As New SqlCommand("usp_ReturnWomenHData", MyConnection)

MyCommand.CommandType = CommandType.StoredProcedure

Try
Dim MsfcNumV As New SqlParameter("@MsfcNumV", SqlDbType.VarChar)

MsfcNumV.Direction = ParameterDirection.Input

MsfcNumV.Value = FrmMainMenu.txtMsfcNum.Text

MyCommand.Parameters.Add(MsfcNumV)

MyConnection.Open()

Dim WomenHAdapter As SqlDataAdapter = New SqlDataAdapter("usp_ReturnWomenHData", MyConnection)

Dim BreastCancer As DataSet = New DataSet

WomenHAdapter.Fill(BreastCancer, "usp_ReturnWomenHData")

DgBcs.DataSource = BreastCancer

MyConnection.Close()

Catch ex As Exception
MessageBox.Show(ex.ToString)
End Try
End Sub
 
On your DataGrid you have to assign the Table within the dataset not just pass it the whole dataset as the datasource.
 
You are adding the parameter to the SQLCommand object but then doing nothing with it. The piece that is actually going out the the DataBase is the SQL Adapter, which you haven't added the parameter to.
 
Both suggestions so far are correct. With regard to what Tom said, you need to change this:
VB.NET:
DgBcs.DataSource = BreastCancer
to either this:
VB.NET:
DgBcs.DataSource = BreastCancer.Tables("usp_ReturnWomenHData")
or this:
VB.NET:
DgBcs.DataMember = "usp_ReturnWomenHData"
DgBcs.DataSource = BreastCancer
With regard to what rcombs4 said, you need to change this:
VB.NET:
Dim WomenHAdapter As SqlDataAdapter = New SqlDataAdapter("usp_ReturnWomenHData", MyConnection)
to this:
VB.NET:
Dim WomenHAdapter As SqlDataAdapter = New SqlDataAdapter(MyCommand)
 
Or use the IDE / Dataset desginer and you'd have never encountered this problem in the first place, and done all your work with a few clicks :)
 
Back
Top