How to pass values from a Windows Application form button or textbox to a stored procedure, which th

EDN Admin

Well-known member
Joined
Aug 7, 2010
Messages
12,794
Location
In the Machine
<p style="line-height:normal; margin:0in 0in 0pt <span style="font-family:Consolas; font-size:9.5pt I have tried many times, but I cannot get the command button, Button3, (or a textbox for that matter) to pass the values to the stored procedure to insert
into the table.<span style=" I get no error messages either.
<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 The data grid worked, and I was able to use stored procedure as the method to update, insert, ect.<span style="
However, I need to pass values from a button (sample below) or a textbox, etc.<span style="
The eventual goal is to pass a scanned barcode as a value to the stored procedure to update or insert or etc. into the database.
<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 I can execute the stored procedure with values from Management Studio on SQL Server 2008.

<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 What am I missing?
<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 The server name is SERVER1
<p style="line-height:normal; margin:0in 0in 0pt <span style="font-family:Consolas; font-size:9.5pt The database name is dbTest
<p style="line-height:normal; margin:0in 0in 0pt <span style="font-family:Consolas; font-size:9.5pt The authentication method is WINDOWS
<p style="line-height:normal; margin:0in 0in 0pt <span style="font-family:Consolas; font-size:9.5pt SQL Server is 2008
<p style="line-height:normal; margin:0in 0in 0pt <span style="font-family:Consolas; font-size:9.5pt Windows Server 2008
<p style="line-height:normal; margin:0in 0in 0pt <span style="font-family:Consolas; font-size:9.5pt VS 2010 Pro using Visual Basic
<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="text-decoration:underline <span style="font-family:Consolas; font-size:9.5pt CODE:
<p style="line-height:normal; margin:0in 0in 0pt <span style="font-family:Consolas; color:blue; font-size:9.5pt
<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
<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; font-size:9.5pt
<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 <span style="
<span style="color:blue Private <span style="color:blue Sub Form1_Load(<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 <span style="color:blue MyBase.Load
<p style="line-height:normal; margin:0in 0in 0pt <span style="font-family:Consolas; font-size:9.5pt <span style="
<span style="color:green TODO: This line of code loads data into the dbTESTDataSet.tblTest table. You can move, or remove it, as needed.
<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.TblTestTableAdapter.Fill(<span style="color:blue Me.dbTESTDataSet.tblTest)
<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 End <span style="color:blue Sub
<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 Button3_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 Button3.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 ConnectionString <span style="color:blue
As <span style="color:blue String = <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="
<span style="color:blue Dim conn <span style="color:blue As
<span style="color:blue New <span style="color:#2b91af SqlConnection(ConnectionString)
<p style="line-height:normal; margin:0in 0in 0pt <span style="font-family:Consolas; font-size:9.5pt <span style="
conn.Open()
<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 StoredProcedureCommand <span style="color:blue
As <span style="color:blue New <span style="color:#2b91af SqlCommand(<span style="color:#a31515 "uspTestInsert", conn)
<p style="line-height:normal; margin:0in 0in 0pt <span style="font-family:Consolas; font-size:9.5pt <span style="
StoredProcedureCommand.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 param <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 <span style="
<span style="color:blue Dim param3 <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="
param = StoredProcedureCommand.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="
param.Direction = <span style="color:#2b91af ParameterDirection.Input
<p style="line-height:normal; margin:0in 0in 0pt <span style="font-family:Consolas; font-size:9.5pt <span style="
param.Value = <span style="color:#a31515 "1111FFF"
<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="
param3 = StoredProcedureCommand.Parameters.Add(<span style="color:#a31515 "@LastName",
<span style="color:#2b91af SqlDbType.VarChar, 50)
<p style="line-height:normal; margin:0in 0in 0pt <span style="font-family:Consolas; font-size:9.5pt <span style="
param3.Direction = <span style="color:#2b91af ParameterDirection.Input
<p style="line-height:normal; margin:0in 0in 0pt <span style="font-family:Consolas; font-size:9.5pt <span style="
param3.Value = <span style="color:#a31515 "3333FFF"
<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="
conn.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: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
<p style="margin:0in 0in 10pt <span style="font-family:Calibri; font-size:small
<p style="margin:0in 0in 10pt <span style="font-family:Calibri; font-size:small
<p style="margin:0in 0in 10pt <span style="text-decoration:underline <span style="font-size:small <span style="font-family:Calibri STORED PROCEDURE on the SQL SERVER:
<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:blue; font-size:10pt ALTER<span style="font-family:"Courier New"; font-size:10pt
<span style="color:blue PROCEDURE [dbo]<span style="color:gray .[uspTestInsert]

<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:gray = <span style="color:red reed<span style="color:gray ,

<p style="line-height:normal; margin:0in 0in 0pt <span style="font-family:"Courier New"; font-size:10pt <span style="
@LastName <span style="color:blue varchar<span style="color:gray (50<span style="color:gray )
<span style="color:gray = <span style="color:red freed
<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 INSERT <span style="color:blue INTO dbo<span style="color:gray .tblTest<span style="color:blue
<span style="color:gray (Part<span style="color:gray , LastName<span style="color:gray )
<span style="color:blue VALUES<span style="color:gray (@Part<span style="color:gray ,@LastName<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="line-height:115%; font-family:"Courier New"; color:blue; font-size:10pt
<p style="margin:0in 0in 10pt <span style="text-decoration:underline <span style="line-height:115%; font-family:"Courier New"; font-size:10pt TABLE INFORMATION (The table is actually already created with data):
<p style="line-height:normal; margin:0in 0in 0pt <span style="font-family:"Courier New"; color:blue; font-size:10pt CREATE<span style="font-family:"Courier New"; font-size:10pt
<span style="color:blue TABLE [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 <span style="
[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 <span style="
[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 <span style="
[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 <span style="
[LastName] [varchar]<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 <span style=" <span style="color:blue CONSTRAINT [PK_tblTest]
<span style="color:blue PRIMARY <span style="color:blue KEY <span style="color:blue
CLUSTERED
<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="
[TestID] <span style="color:blue ASC
<p style="line-height:normal; margin:0in 0in 0pt <span style="font-family:"Courier New"; color:gray; font-size:10pt )<span style="font-family:"Courier New"; color:blue; font-size:10pt WITH
<span style="font-family:"Courier New"; color:gray; font-size:10pt (<span style="font-family:"Courier New"; color:blue; font-size:10pt PAD_INDEX<span style="font-family:"Courier New"; font-size:10pt <span style="
<span style="color:gray = <span style="color:blue OFF<span style="color:gray ,
<span style="color:blue STATISTICS_NORECOMPUTE<span style="
<span style="color:gray = <span style="color:blue OFF<span style="color:gray ,
<span style="color:blue IGNORE_DUP_KEY <span style="color:gray = <span style="color:blue
OFF<span style="color:gray , <span style="color:blue ALLOW_ROW_LOCKS<span style="
<span style="color:gray = <span style="color:blue ON<span style="color:gray ,
<span style="color:blue ALLOW_PAGE_LOCKS<span style=" <span style="color:gray =
<span style="color:blue ON<span style="color:gray ) <span style="color:blue
ON [PRIMARY]
<p style="margin:0in 0in 10pt <span style="line-height:115%; font-family:"Courier New"; color:gray; font-size:10pt )<span style="line-height:115%; font-family:"Courier New"; font-size:10pt
<span style="color:blue ON [PRIMARY]
<p style="margin:0in 0in 10pt <span style="line-height:115%; font-family:"Courier New"; font-size:10pt
<p style="margin:0in 0in 10pt <span style="text-decoration:underline <span style="line-height:115%; font-family:"Courier New"; font-size:10pt Data from the table:
<p style="margin:0in 0in 10pt <span style="font-size:small <span style="font-family:Calibri TestID<span style="
Part<span style=" <span style="
Desc<span style=" LastName
<p style="margin:0in 0in 10pt <span style="font-size:small <span style="font-family:Calibri 1<span style="
WERD<span style=" <span style="
hello<span style=" DERD
<p style="margin:0in 0in 10pt <span style="font-size:small <span style="font-family:Calibri 2<span style="
ddd<span style=" <span style=" pinky<span style="
ned
<p style="margin:0in 0in 10pt <span style="font-size:small <span style="font-family:Calibri 3<span style="
opopopop<span style="
here<span style=" ye
<p style="margin:0in 0in 10pt <span style="font-size:small <span style="font-family:Calibri 5<span style="
red<span style=" <span style=" fred<span style="
rrrrr
<p style="margin:0in 0in 10pt <span style="font-size:small <span style="font-family:Calibri 6<span style="
reed<span style=" <span style=" NULL<span style="
freed

View the full article
 
Back
Top