How do I pass data from a SQL Server 2008 to a label on a form in a windows application using a stor

EDN Admin

Well-known member
Joined
Aug 7, 2010
Messages
12,794
Location
In the Machine
<p style="margin:0in 0in 10pt <span style="font-family:Calibri <span style="font-size:small How do I pass data from a SQL Server 2008 to a label on a form in a windows application using a stored procedure?
<p style="margin:0in 0in 10pt <span style="font-family:Calibri <span style="font-size:small I am trying to pass SQL SERVER 2008 data from one field from one row from one table through a stored procedure to a LABEL on my FORM when a button is clicked using
VB 2010 Pro. <span style=" The way I have the stored procedure setup and the way I have the VB code setup, I expect the text in LABEL1 to read âdddâ instead of just disappearing like it is doing now when I click BUTTON2.<span style="
I have tried various things, and some of the things I tried in the code are commented out.
<p style="text-indent:-0.25in; margin:0in 0in 0pt 0.5in <span style="font-family:Symbol <span style=" <span style="font-size:small <span style="font:7pt "Times New Roman"
<span style="font-family:Calibri <span style="font-size:small The table name is tblTest.
<p style="text-indent:-0.25in; margin:0in 0in 0pt 0.5in <span style="font-family:Symbol <span style=" <span style="font-size:small <span style="font:7pt "Times New Roman"
<span style="font-family:Calibri <span style="font-size:small The data source is SERVER1
<p style="text-indent:-0.25in; margin:0in 0in 10pt 0.5in <span style="font-family:Symbol <span style=" <span style="font-size:small <span style="font:7pt "Times New Roman"
<span style="font-size:small <span style="font-family:Calibri Initial Catalog is dbTEST
<p style="margin:0in 0in 10pt <span style="text-decoration:underline <span style="text-decoration:none <span style="font-family:Calibri; font-size:small
<p style="margin:0in 0in 10pt <span style="font-size:small <span style="font-family:Calibri <span style="text-decoration:underline Below is the data from the table:
<table border="0" cellspacing="0" cellpadding="0" width="267" style="margin:auto auto auto 4.65pt; width:200pt; border-collapse:collapse
<tbody>
<tr style="height:15pt
<td width="64" valign="bottom" style="
<p style="line-height:normal; margin:0in 0in 0pt <span style="color:black <span style="font-family:Calibri TestID
</td>
<td width="71" valign="bottom" style="
<p style="line-height:normal; margin:0in 0in 0pt <span style="color:black <span style="font-family:Calibri Part
</td>
<td width="64" valign="bottom" style="
<p style="line-height:normal; margin:0in 0in 0pt <span style="color:black <span style="font-family:Calibri Desc
</td>
<td width="68" valign="bottom" style="
<p style="line-height:normal; margin:0in 0in 0pt <span style="color:black <span style="font-family:Calibri LastName
</td>
</tr>
<tr style="height:15pt
<td width="64" valign="bottom" style="
<p style="line-height:normal; margin:0in 0in 0pt <span style="color:black <span style="font-family:Calibri 1
</td>
<td width="71" valign="bottom" style="
<p style="line-height:normal; margin:0in 0in 0pt <span style="color:black <span style="font-family:Calibri WERD
</td>
<td width="64" valign="bottom" style="
<p style="line-height:normal; margin:0in 0in 0pt <span style="color:black <span style="font-family:Calibri hello
</td>
<td width="68" valign="bottom" style="
<p style="line-height:normal; margin:0in 0in 0pt <span style="color:black <span style="font-family:Calibri DERD
</td>
</tr>
<tr style="height:15pt
<td width="64" valign="bottom" style="
<p style="line-height:normal; margin:0in 0in 0pt <span style="color:black <span style="font-family:Calibri 2
</td>
<td width="71" valign="bottom" style="
<p style="line-height:normal; margin:0in 0in 0pt <span style="color:black <span style="font-family:Calibri ddd
</td>
<td width="64" valign="bottom" style="
<p style="line-height:normal; margin:0in 0in 0pt <span style="color:black <span style="font-family:Calibri pinky
</td>
<td width="68" valign="bottom" style="
<p style="line-height:normal; margin:0in 0in 0pt <span style="color:black <span style="font-family:Calibri ned
</td>
</tr>
<tr style="height:15pt
<td width="64" valign="bottom" style="
<p style="line-height:normal; margin:0in 0in 0pt <span style="color:black <span style="font-family:Calibri 3
</td>
<td width="71" valign="bottom" style="
<p style="line-height:normal; margin:0in 0in 0pt <span style="color:black <span style="font-family:Calibri opopopop
</td>
<td width="64" valign="bottom" style="
<p style="line-height:normal; margin:0in 0in 0pt <span style="color:black <span style="font-family:Calibri here
</td>
<td width="68" valign="bottom" style="
<p style="line-height:normal; margin:0in 0in 0pt <span style="color:black <span style="font-family:Calibri ye
</td>
</tr>
</tbody>
</table>
<p style="margin:0in 0in 10pt <span style="font-family:Calibri; font-size:small
<p style="margin:0in 0in 10pt <span style="font-size:small <span style="font-family:Calibri <span style="text-decoration:underline Below is the table:
<p style="line-height:normal; margin:0in 0in 0pt <span style="font-family:"Courier New"; font-size:10pt [dbo]<span style="color:gray .[tblTest]<span style="color:gray (
<p style="line-height:normal; margin:0in 0in 0pt <span style="font-family:"Courier New"; font-size:10pt [TestID] [int]
<span style="color:blue IDENTITY<span style="color:gray (1<span style="color:gray ,1<span style="color:gray )
<span style="color:gray NOT <span style="color:gray NULL,
<p style="line-height:normal; margin:0in 0in 0pt <span style="font-family:"Courier New"; font-size:10pt [Part] [varchar]<span style="color:gray (20<span style="color:gray )
<span style="color:gray NULL,
<p style="line-height:normal; margin:0in 0in 0pt <span style="font-family:"Courier New"; font-size:10pt [Desc] [nvarchar]<span style="color:gray (50<span style="color:gray )
<span style="color:gray NULL,
<p style="line-height:normal; margin:0in 0in 0pt <span style="font-family:"Courier New"; font-size:10pt [LastName] [varchar]<span style="color:gray (50<span style="color:gray )
<span style="color:gray NULL,
<p style="margin:0in 0in 10pt <span style="line-height:115%; font-family:"Courier New"; color:blue; font-size:10pt CONSTRAINT<span style="line-height:115%; font-family:"Courier New"; font-size:10pt [PK_tblTest]
<span style="color:blue PRIMARY <span style="color:blue KEY <span style="color:blue
CLUSTERED
<p style="margin:0in 0in 10pt <span style="text-decoration:underline <span style="text-decoration:none
<p style="margin:0in 0in 10pt <span style="font-size:small <span style="font-family:Calibri
<p style="margin:0in 0in 10pt <span style="font-size:small <span style="font-family:Calibri <span style="text-decoration:underline Below is the stored procedure:
<p style="line-height:normal; margin:0in 0in 0pt <span style="font-family:"Courier New"; color:blue; font-size:10pt USE<span style="font-family:"Courier New"; font-size:10pt [dbTEST]
<p style="line-height:normal; margin:0in 0in 0pt <span style="font-family:"Courier New"; color:blue; font-size:10pt GO
<p style="line-height:normal; margin:0in 0in 0pt <span style="font-family:"Courier New"; color:blue; font-size:10pt
<p style="line-height:normal; margin:0in 0in 0pt <span style="font-family:"Courier New"; color:blue; font-size:10pt SET<span style="font-family:"Courier New"; font-size:10pt
<span style="color:blue ANSI_NULLS <span style="color:blue ON
<p style="line-height:normal; margin:0in 0in 0pt <span style="font-family:"Courier New"; color:blue; font-size:10pt GO
<p style="line-height:normal; margin:0in 0in 0pt <span style="font-family:"Courier New"; color:blue; font-size:10pt SET<span style="font-family:"Courier New"; font-size:10pt
<span style="color:blue QUOTED_IDENTIFIER <span style="color:blue ON
<p style="line-height:normal; margin:0in 0in 0pt <span style="font-family:"Courier New"; color:blue; font-size:10pt GO
<p style="line-height:normal; margin:0in 0in 0pt <span style="font-family:"Courier New"; color:green; font-size:10pt -- =============================================
<p style="line-height:normal; margin:0in 0in 0pt <span style="font-family:"Courier New"; color:green; font-size:10pt -- Author:<span style="

<p style="line-height:normal; margin:0in 0in 0pt <span style="font-family:"Courier New"; color:green; font-size:10pt -- Create date:

<p style="line-height:normal; margin:0in 0in 0pt <span style="font-family:"Courier New"; color:green; font-size:10pt -- Description:<span style="

<p style="line-height:normal; margin:0in 0in 0pt <span style="font-family:"Courier New"; color:green; font-size:10pt -- =============================================
<p style="line-height:normal; margin:0in 0in 0pt <span style="font-family:"Courier New"; color:blue; font-size:10pt ALTER<span style="font-family:"Courier New"; font-size:10pt
<span style="color:blue PROCEDURE [dbo]<span style="color:gray .[uspTest]

<p style="line-height:normal; margin:0in 0in 0pt <span style="font-family:"Courier New"; font-size:10pt <span style="
<span style="color:green -- Add the parameters for the stored procedure here
<p style="line-height:normal; margin:0in 0in 0pt <span style="font-family:"Courier New"; font-size:10pt <span style="
@Part <span style="color:blue varchar<span style="color:gray (20<span style="color:gray )
<span style="color:blue OUTPUT
<p style="line-height:normal; margin:0in 0in 0pt <span style="font-family:"Courier New"; font-size:10pt <span style="

<p style="line-height:normal; margin:0in 0in 0pt <span style="font-family:"Courier New"; color:blue; font-size:10pt AS
<p style="line-height:normal; margin:0in 0in 0pt <span style="font-family:"Courier New"; color:blue; font-size:10pt BEGIN
<p style="line-height:normal; margin:0in 0in 0pt <span style="font-family:"Courier New"; font-size:10pt <span style="
<span style="color:green -- SET NOCOUNT ON added to prevent extra result sets from
<p style="line-height:normal; margin:0in 0in 0pt <span style="font-family:"Courier New"; font-size:10pt <span style="
<span style="color:green -- interfering with SELECT statements.
<p style="line-height:normal; margin:0in 0in 0pt <span style="font-family:"Courier New"; font-size:10pt <span style="
<span style="color:blue SET <span style="color:blue NOCOUNT
<span style="color:blue ON<span style="color:gray ;
<p style="line-height:normal; margin:0in 0in 0pt <span style="font-family:"Courier New"; color:gray; font-size:10pt
<p style="line-height:normal; margin:0in 0in 0pt <span style="font-family:"Courier New"; font-size:10pt <span style="
<span style="color:green -- Insert statements for procedure here
<p style="line-height:normal; margin:0in 0in 0pt <span style="font-family:"Courier New"; font-size:10pt <span style="
<span style="color:blue SELECT @Part <span style="color:gray =<span style="color:blue
<span style="color:gray (<span style="color:blue SELECT Part
<span style="color:blue FROM dbo<span style="color:gray .tblTest <span style="color:blue
WHERE TestID<span style="color:gray = 2<span style="color:gray )
<p style="margin:0in 0in 10pt <span style="line-height:115%; font-family:"Courier New"; color:blue; font-size:10pt END
<p style="margin:0in 0in 10pt <span style="font-family:Calibri; font-size:small
<p style="margin:0in 0in 10pt <span style="font-size:small <span style="font-family:Calibri <span style="text-decoration:underline Below is the VB code:
<p style="line-height:normal; margin:0in 0in 0pt <span style="font-family:Consolas; color:blue; font-size:9.5pt Imports<span style="font-family:Consolas; font-size:9.5pt System.Data.SqlClient
<p style="line-height:normal; margin:0in 0in 0pt <span style="font-family:Consolas; color:blue; font-size:9.5pt Public<span style="font-family:Consolas; font-size:9.5pt
<span style="color:blue Class <span style="color:#2b91af Form1
<p style="line-height:normal; margin:0in 0in 0pt <span style="font-family:Consolas; font-size:9.5pt
<p style="line-height:normal; margin:0in 0in 0pt <span style="font-family:Consolas; font-size:9.5pt
<p style="line-height:normal; margin:0in 0in 0pt <span style="font-family:Consolas; font-size:9.5pt <span style="
<span style="color:blue Private <span style="color:blue Sub Button2_Click(<span style="color:blue ByVal sender
<span style="color:blue As System.<span style="color:#2b91af Object,
<span style="color:blue ByVal e <span style="color:blue As System.<span style="color:#2b91af EventArgs)
<span style="color:blue Handles Button2.Click
<p style="line-height:normal; margin:0in 0in 0pt <span style="font-family:Consolas; font-size:9.5pt <span style="
<span style="color:blue Dim myConnection <span style="color:blue
As <span style="color:#2b91af SqlConnection
<p style="line-height:normal; margin:0in 0in 0pt <span style="font-family:Consolas; font-size:9.5pt <span style="
<span style="color:green Dim stuff As Integer
<p style="line-height:normal; margin:0in 0in 0pt <span style="font-family:Consolas; font-size:9.5pt <span style="
myConnection = <span style="color:blue New <span style="color:#2b91af
SqlConnection(<span style="color:#a31515 "Data Source=SERVER1; Initial Catalog=dbTEST; Integrated Security=true")
<p style="line-height:normal; margin:0in 0in 0pt <span style="font-family:Consolas; font-size:9.5pt <span style="
myConnection.Open()
<p style="line-height:normal; margin:0in 0in 0pt <span style="font-family:Consolas; font-size:9.5pt
<p style="line-height:normal; margin:0in 0in 0pt <span style="font-family:Consolas; font-size:9.5pt <span style="
<span style="color:blue Dim myStoredProcedureCommand <span style="color:blue
As <span style="color:blue New <span style="color:#2b91af SqlCommand(<span style="color:#a31515 "uspTest", myConnection)
<p style="line-height:normal; margin:0in 0in 0pt <span style="font-family:Consolas; font-size:9.5pt <span style="
myStoredProcedureCommand.CommandType = <span style="color:#2b91af CommandType.StoredProcedure
<p style="line-height:normal; margin:0in 0in 0pt <span style="font-family:Consolas; font-size:9.5pt
<p style="line-height:normal; margin:0in 0in 0pt <span style="font-family:Consolas; font-size:9.5pt <span style="
<span style="color:blue Dim myParameter <span style="color:blue As
<span style="color:blue New <span style="color:#2b91af SqlParameter()
<p style="line-height:normal; margin:0in 0in 0pt <span style="font-family:Consolas; font-size:9.5pt
<p style="line-height:normal; margin:0in 0in 0pt <span style="font-family:Consolas; font-size:9.5pt <span style="
myParameter = myStoredProcedureCommand.Parameters.Add(<span style="color:#a31515 "@Part",
<span style="color:#2b91af SqlDbType.VarChar, 20)
<p style="line-height:normal; margin:0in 0in 0pt <span style="font-family:Consolas; font-size:9.5pt <span style="
myParameter.Direction = <span style="color:#2b91af ParameterDirection.Output
<p style="line-height:normal; margin:0in 0in 0pt <span style="font-family:Consolas; font-size:9.5pt <span style="
<span style="color:blue Me.Label1.Text = myParameter.Value
<p style="line-height:normal; margin:0in 0in 0pt <span style="font-family:Consolas; font-size:9.5pt
<p style="line-height:normal; margin:0in 0in 0pt <span style="font-family:Consolas; font-size:9.5pt <span style="
<span style="color:green Dim reader As SqlDataReader = myStoredProcedureCommand.ExecuteReader()
<p style="line-height:normal; margin:0in 0in 0pt <span style="font-family:Consolas; font-size:9.5pt <span style="
<span style="color:green While reader.Read()
<p style="line-height:normal; margin:0in 0in 0pt <span style="font-family:Consolas; font-size:9.5pt <span style="
<span style=" <span style="color:green Me.Label1.Text = "reeeed"
<p style="line-height:normal; margin:0in 0in 0pt <span style="font-family:Consolas; font-size:9.5pt <span style="
<span style="color:green End While
<p style="line-height:normal; margin:0in 0in 0pt <span style="font-family:Consolas; font-size:9.5pt <span style="
<span style="color:green reader.Close()
<p style="line-height:normal; margin:0in 0in 0pt <span style="font-family:Consolas; font-size:9.5pt
<p style="line-height:normal; margin:0in 0in 0pt <span style="font-family:Consolas; font-size:9.5pt <span style="
<span style="color:green stuff = myStoredProcedureCommand.ExecuteNonQuery()
<p style="line-height:normal; margin:0in 0in 0pt <span style="font-family:Consolas; font-size:9.5pt
<p style="line-height:normal; margin:0in 0in 0pt <span style="font-family:Consolas; font-size:9.5pt <span style="
myConnection.Close()
<p style="line-height:normal; margin:0in 0in 0pt <span style="font-family:Consolas; font-size:9.5pt <span style="
<span style="color:blue End <span style="color:blue Sub
<p style="line-height:normal; margin:0in 0in 0pt <span style="font-family:Consolas; color:blue; font-size:9.5pt End<span style="font-family:Consolas; font-size:9.5pt
<span style="color:blue Class

View the full article
 
Back
Top