473,811 Members | 3,402 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Passing values from a VB app to a stored procedure

347 Contributor
This may not be the place to ask this, if not, you can move this post to the SQL forum but I have an application that I'm building which pulls dates from a database. The start value is stored as payPeriodStart date and I add 7 days to get the end date. What I need to know is, is it possible to store those values and pass them to a Stored Procedure? Currently my stored procedures have "static" dates in them ie:

where dateadd (n, Timestamp, '12/31/1899') - ([LoggedIn]/1000)/60/1440+1 Between '10/3/2010' and '10/10/2010'

and I'd like those values to be

Between payPeriodStartD ate and payPeriodEndDat e.

Any help would be greatly appreciated.

Thank you
Jan 6 '11 #1
5 4378
Joseph Martell
198 Recognized Expert New Member
Yes, it is possible to create a stored procedure that takes parameters from a VB app.

Your stored procedure needs to be defined to take the parameters that you need. Also, your code to execute the stored procedure needs to set the parameters.

If you are using SQL Server, the correct syntax to create a stored procedure can be found here. I am not well versed in creating stored procedures though, so that part of this question will be better resolved in the SQL forum.

As far as passing a parameter to a stored procedure in VB, the code looks something like this:

Expand|Select|Wrap|Line Numbers
  1.         Dim payPeriodStartDate As Date = Date.Now.Date
  2.         Dim payPeriodEndDate As Date = payPeriodStartDate.AddDays(14)
  3.  
  4.         Dim sqlConn As New SqlConnection("")
  5.         Dim sqlCmd As New SqlCommand("StoredProcedureName")
  6.         Dim param As SqlParameter
  7.  
  8.         sqlCmd.Connection = sqlConn
  9.         sqlCmd.CommandType = CommandType.StoredProcedure
  10.  
  11.         param = sqlCmd.CreateParameter()
  12.         param.ParameterName = "@payPeriodStartDate"
  13.         param.Value = payPeriodStartDate
  14.         param.SqlDbType = SqlDbType.Date
  15.         sqlCmd.Parameters.Add(param)
  16.  
  17.         param = sqlCmd.CreateParameter()
  18.         param.ParameterName = "@payPeriodEndDate"
  19.         param.Value = payPeriodEndDate
  20.         param.SqlDbType = SqlDbType.Date
  21.         sqlCmd.Parameters.Add(param)
  22.  
  23.         Dim dataReader As SqlDataReader = Nothing
  24.  
  25.         Try
  26.             dataReader = sqlCmd.ExecuteReader()
  27.             'access your data
  28.         Catch ex As Exception
  29.             'whatever your exception handling is
  30.         End Try
  31.  
Forgive me if this isn't 100% accurate. Its been a while sense I wrote this code in a production environment. I just quickly threw this together. The idea is correct though. Notice that the SqlCommand text is the stored procedure name and the SqlCommand object actually has a property that is set to explicitly state what type of command you are executing.
Jan 7 '11 #2
dougancil
347 Contributor
Joseph,

Thank you for your response. I was looking through my app and discovered that I already had parameters set for the values that I need to pass to my sql stored procedure. My code looks like this:

Expand|Select|Wrap|Line Numbers
  1. Private Sub Button1_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles startpayrollButton.Click
  2.         Dim ssql As String = "select MAX(payrolldate) AS [payrolldate], " & _
  3.                  "dateadd(dd, ((datediff(dd, '17530107', MAX(payrolldate))/7)*7)+7, '17530107') AS [Sunday]" & _
  4.                   "from dbo.payroll" & _
  5.                   " where payrollran = 'no'"
  6.         Dim oCmd As System.Data.SqlClient.SqlCommand
  7.         Dim oDr As System.Data.SqlClient.SqlDataReader
  8.  
  9.         oCmd = New System.Data.SqlClient.SqlCommand
  10.         Try
  11.             With oCmd
  12.                 .Connection = New System.Data.SqlClient.SqlConnection("Initial Catalog=mdr;Data Source=xxxxx;uid=xxxxx;password=xxxxxx")
  13.                 .Connection.Open()
  14.                 .CommandType = CommandType.Text
  15.                 .CommandText = ssql
  16.                 oDr = .ExecuteReader()
  17.             End With
  18.             If oDr.Read Then
  19.                 payPeriodStartDate = oDr.GetDateTime(1)
  20.                 payPeriodEndDate = payPeriodStartDate.AddDays(7)
  21.                 Dim ButtonDialogResult As DialogResult
  22.                 ButtonDialogResult = MessageBox.Show("      The Next Payroll Start Date is: " & payPeriodStartDate.ToString() & System.Environment.NewLine & "            Through End Date: " & payPeriodEndDate.ToString())
  23.                 If ButtonDialogResult = Windows.Forms.DialogResult.OK Then
  24.  
  25.                     exceptionsButton.Enabled = True
  26.                     startpayrollButton.Enabled = False
  27.  
  28.                 End If
  29.             End If
  30.             oDr.Close()
  31.             oCmd.Connection.Close()
  32.         Catch ex As Exception
  33.             MessageBox.Show(ex.Message)
  34.             oCmd.Connection.Close()
  35.         End Try
  36.  
  37.     End Sub
  38.  
So I have two questions in regards to this. Should I declare the values outside of this sub? Secondly, since I'm calling the parameters payPeriodStartD ate and payPeriodEndDat e, I'm assuming that in my sql SP, I'll just need to write my query like this:

where dateadd (n, Timestamp, '12/31/1899') - ([LoggedIn]/1000)/60/1440+1 Between payPeriodStartD ate and payPeriodEndDat e

Is that correct?

Thanks

Doug
Jan 10 '11 #3
Joseph Martell
198 Recognized Expert New Member
I'm not sure what you mean by "should I declare the values outside of my sub". Could you elaborate?

Secondly, the a stored procedure is something that is contained completely on the SQL Server. The variable names that you decide to use in your program have no relationship to the parameters of a stored procedure.
Jan 18 '11 #4
dougancil
347 Contributor
joseph,

What I'm asking for is because I'm declaring values that need to be passed on each form, I am thinking that I need to either declare "global" variables or something within a module.

I am aware of the fact that the SP is contained on the SQL server, but my variables (in this case payrollstartdat e, payrollenddate) are pulled from the sql server at the start of this process and I'll need to save them as values to pass to my stored procedures. The way that I have my SP set up is that @payrollstartda te and @payrollenddate replace payrollstartdat e, payrollenddate simply as a cleaner naming convention. I hope that makes sense and I can post my code if necessary for my sp's.
Jan 18 '11 #5
dougancil
347 Contributor
I think I solved this problem. I just created a module:

Module Module1
Public payperiodstartd ate As Date
Public payperiodenddat e As Date
End Module

That seems to have fixed the issue.
Jan 19 '11 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

4
6104
by: Mike | last post by:
Hello, I'm currently working on debugging a very large DTS package that was created by someone else for the purpose of importing data into my company's database. The data is mainly user/contact-related data for our customer base. We ran into problems when one import, of about 40,000 rows, took upwards of six hours to complete. Many of the stored procedures used by this package were written using XML. I've re-written many of them
0
6707
by: Nashat Wanly | last post by:
HOW TO: Call a Parameterized Stored Procedure by Using ADO.NET and Visual C# .NET View products that this article applies to. This article was previously published under Q310070 For a Microsoft Visual Basic .NET version of this article, see 308049. For a Microsoft Visual C++ .NET version of this article, see 310071. For a Microsoft Visual J# .NET version of this article, see 320627. This article refers to the following Microsoft .NET...
4
9563
by: shyner | last post by:
Hi Everyone, I've been battling this for two days with no luck. I'm using SQL Server 2000. Here's the mystery: I've got a stored procedure that takes a single varchar parameter to determine how the result set is sorted. Here it is: CREATE PROCEDURE spDemo @SortField varchar(30)
3
14208
by: mhk | last post by:
Hi, i have "req_date" column of "datetime" type in Database table besides other columns. From my Web page, i am calling the Stored Procedure with variable parameter "Search_Date" of Varchar(60) type. the value, i am passing to Stored procedure through "Search_Date" is compared to req_date column of table.
4
1816
by: erich93063 | last post by:
I have a stored procedure which is performing a search against a "task" table. I would like to pass in a variable called @strAssignedTo which will be a comma delimeted list of employee ID's ie: "5,6,10". So my SQL without this variable would be something like: WHERE intAssignedTo IN (5,6,10) but when I try to do: WHERE intAssignedTo IN (@strAssignedTo)
2
5464
by: Dino L. | last post by:
How can I run stored procedure (MSSQL) ?
5
5038
by: purushneel | last post by:
Hi, I work primarily on Oracle databases. I am trying to convert a recursive stored procedure written in Oracle to DB2. Does DB2 UDB v8.2 (Windows/AIX) supports recursive stored procedures ?? After some research, I found out that to call recursively in DB2, the stored procedure should be CALLed using dynamic SQL. I am not sure whether it is the right way. Am I missing something ?? Please let me know...
4
3718
by: meenu_susi | last post by:
i have different colors for shirt... When I click the blue color the shirt color will change to blue color and Near to that shirt I will have a button as design now when I click that button it get posted To next page where I want the changed shirt color to be displayed(as I told when I click blue the shirt will change to blue that blue shirt has to be displayed in next page when I click the button) Then the script is as follows ...
1
14963
by: chariclark | last post by:
This may be a quick fix post... ---------------------------- I am having trouble passing multiple values into stored procedure. Here it is below: CREATE Procedure spGetAssociateds ( @PDSI int ) AS
1
9722
by: udaypawar | last post by:
Hi All, I have one problem here with mysql stored procedures. I have a list of ids seperated by comma e.g., (" 'A', 'B', 'C' "). I am passing the same to mysql stored procedure as a parameter. I want to use these values in where clause of select statement using IN. I can't use prepared statements because queries written in stored procedure are very complex.
0
9726
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9605
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10647
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10384
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10130
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9204
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7667
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6887
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5553
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.