pauldonson
asked on
Populating a Windows form with results of a SQL Stored Procedure in VB 2005
Hi,
This one is driving me mad! I am quite new to VB, my expertise being with Access Data Projects.
I have a form that has a person_ID on it, I want the user to be able to click a button and a new form opens with the data for that person_ID on the new form. This data is retrieved via a stored procedure: ProcPersonFetchPersonal.
I have tried doing this using the following code:
'Open people maintenance
Me.Cursor = Cursors.WaitCursor
Dim conn As SqlClient.SqlConnection = New SqlClient.SqlConnection("D ata Source=" & frmLogOn.txtServer.Text.To String & ";Integrated Security=SSPI;Initial Catalog=" & frmLogOn.cboDatabase.Text. ToString)
Dim cmd As SqlClient.SqlCommand = New SqlClient.SqlCommand
Dim data As New DataSet
cmd.CommandType = CommandType.StoredProcedur e
cmd.CommandText = "procPersonFetchPersonal"
cmd.Connection = conn
cmd.Parameters.Add(New SqlClient.SqlParameter("@P erson_ID", Me.lstResults.SelectedValu e))
Dim adapter As New SqlClient.SqlDataAdapter(c md)
adapter.Fill(data, "procPersonFetchPersonal")
frmPeopleMaintenance.cboOf ficeID.Dat aBindings. Add(New System.Windows.Forms.Bindi ng("Text", adapter, "Office_ID"))
frmPeopleMaintenance.txtPe rsonName.D ataBinding s.Add("Tex t", data, "Person_name") ************This is the problem line
frmPeopleMaintenance.MdiPa rent = frmMain
frmPeopleMaintenance.Show( )
conn = Nothing
cmd = Nothing
data = Nothing
adapter = Nothing
Me.Cursor = Cursors.Default
The difficult bit is getting the data onto the form (I don't want it bound to the form if possible) , Ideally, when the form opens I want the text property of each field to be populated with the results of the stored procedure. This would allow free editing by the user and a "commit" button would save any changes with a different stored procedure.
I have experimented with using system-generated datasets but this seems to offer a too simplistic result, requiring that the person_id is input onto the form and the Fill button pressed to retrieve the data, which works but is not professional enough for the results I need.
Can anyone point me in the right direction please?
This one is driving me mad! I am quite new to VB, my expertise being with Access Data Projects.
I have a form that has a person_ID on it, I want the user to be able to click a button and a new form opens with the data for that person_ID on the new form. This data is retrieved via a stored procedure: ProcPersonFetchPersonal.
I have tried doing this using the following code:
'Open people maintenance
Me.Cursor = Cursors.WaitCursor
Dim conn As SqlClient.SqlConnection = New SqlClient.SqlConnection("D
Dim cmd As SqlClient.SqlCommand = New SqlClient.SqlCommand
Dim data As New DataSet
cmd.CommandType = CommandType.StoredProcedur
cmd.CommandText = "procPersonFetchPersonal"
cmd.Connection = conn
cmd.Parameters.Add(New SqlClient.SqlParameter("@P
Dim adapter As New SqlClient.SqlDataAdapter(c
adapter.Fill(data, "procPersonFetchPersonal")
frmPeopleMaintenance.cboOf
frmPeopleMaintenance.txtPe
frmPeopleMaintenance.MdiPa
frmPeopleMaintenance.Show(
conn = Nothing
cmd = Nothing
data = Nothing
adapter = Nothing
Me.Cursor = Cursors.Default
The difficult bit is getting the data onto the form (I don't want it bound to the form if possible) , Ideally, when the form opens I want the text property of each field to be populated with the results of the stored procedure. This would allow free editing by the user and a "commit" button would save any changes with a different stored procedure.
I have experimented with using system-generated datasets but this seems to offer a too simplistic result, requiring that the person_id is input onto the form and the Fill button pressed to retrieve the data, which works but is not professional enough for the results I need.
Can anyone point me in the right direction please?
Just a note on your design. OO wise you should be doing the load of the data in the child form not in the parent. I would suggest passing personID to the child via a property and moving all of the stored prcocedure related code to the child. Part of your problem may be that you're trying to bind a control in one class to the dataset in another.
ASKER
Thanks for the tip BriCrowe, do I need to put the code in the load or activate event? I guess I need to be careful of the order of events, i.e. the parent needs to have passed the ID field and the child received it before the stored procedure runs.
you could also opt to edit the constructor to accept an ID value in your child form. This would take care of any order of event issues.
public class frmChild
inherits system.windows.forms.form
private m_iID as integer
public sub new(iID as integer)
InitializeComponent()
m_iID = iID
end sub
public class frmChild
inherits system.windows.forms.form
private m_iID as integer
public sub new(iID as integer)
InitializeComponent()
m_iID = iID
end sub
You can put the code or call a separate procedure with the stored procedure from the load event.
ASKER
OK, I have a subroutine that is based the tmpPerson_ID field for the form to open with. Still not sure how to populate the .text property of the textbox fields in the form:
Sub subOpenPeopleMaintenance(B yVal tmpPersonID As Integer)
'Open people maintenance
Dim conn As SqlClient.SqlConnection = New SqlClient.SqlConnection("D ata Source = " & frmLogOn.txtServer.Text.To String & ";Integrated Security=SSPI;Initial Catalog=" & frmLogOn.cboDatabase.Text. ToString)
Dim cmd As SqlClient.SqlCommand = New SqlClient.SqlCommand
Dim data As New DataSet
cmd.CommandType = CommandType.StoredProcedur e
cmd.CommandText = "procPersonFetchPersonal"
cmd.Connection = conn
cmd.Parameters.Add(New SqlClient.SqlParameter("@P erson_ID", tmpPersonID))
Dim adapter As New SqlClient.SqlDataAdapter(c md)
adapter.Fill(data, "procPersonFetchPersonal")
frmPeopleMaintenance.cboOf ficeID.Dat aBindings. Add(New System.Windows.Forms.Bindi ng("Text", adapter, "Office_ID"))
frmPeopleMaintenance.txtPe rsonName.D ataBinding s.Add("Tex t", adapter, "Person_name")
'frmPeopleMaintenance.txtP ersonName. Text= cmd.
frmPeopleMaintenance.MdiPa rent = frmMain
frmPeopleMaintenance.Show( )
conn = Nothing
cmd = Nothing
data = Nothing
adapter = Nothing
End Sub
When I call this subroutine with the following line:
Call subOpenPeopleMaintenance(M e.lstResul ts.Selecte dValue)
I get the following error at the line "frmPeopleMaintenance.Show ()":
Cannot bind to the property or column Person_name on the DataSource.
Parameter name: dataMember
Any ideas?
Sub subOpenPeopleMaintenance(B
'Open people maintenance
Dim conn As SqlClient.SqlConnection = New SqlClient.SqlConnection("D
Dim cmd As SqlClient.SqlCommand = New SqlClient.SqlCommand
Dim data As New DataSet
cmd.CommandType = CommandType.StoredProcedur
cmd.CommandText = "procPersonFetchPersonal"
cmd.Connection = conn
cmd.Parameters.Add(New SqlClient.SqlParameter("@P
Dim adapter As New SqlClient.SqlDataAdapter(c
adapter.Fill(data, "procPersonFetchPersonal")
frmPeopleMaintenance.cboOf
frmPeopleMaintenance.txtPe
'frmPeopleMaintenance.txtP
frmPeopleMaintenance.MdiPa
frmPeopleMaintenance.Show(
conn = Nothing
cmd = Nothing
data = Nothing
adapter = Nothing
End Sub
When I call this subroutine with the following line:
Call subOpenPeopleMaintenance(M
I get the following error at the line "frmPeopleMaintenance.Show
Cannot bind to the property or column Person_name on the DataSource.
Parameter name: dataMember
Any ideas?
ASKER
Do I even need a data adapter? The results from the Stored Procedure will only ever be one row.
No, but if you're writing back to your database then it is often simpler to use the dataadapter but you could use the datareader to pull in the data and either store it in a table or add the data directly to your controls. When you go to write the data back to your database you could use a command and just use an update query using the values in your controls.
ASKER
That sounds like what I want to do, what is the syntax for setting the text property of a textbox with a field from a datareader?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
dropped off in the middle of that second sentence there...what I was trying to say is that you will probably have to change the column/control names and the parameter type to match yours.
https://www.experts-exchange.com/questions/21415723/Executing-SQL-Server-stored-procedure-using-VB-NET.html