473,385 Members | 1,593 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Passing values from a VB app to a stored procedure

347 100+
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 payPeriodStartDate and payPeriodEndDate.

Any help would be greatly appreciated.

Thank you
Jan 6 '11 #1
5 4361
Joseph Martell
198 Expert 128KB
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 100+
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 payPeriodStartDate and payPeriodEndDate, 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 payPeriodStartDate and payPeriodEndDate

Is that correct?

Thanks

Doug
Jan 10 '11 #3
Joseph Martell
198 Expert 128KB
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 100+
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 payrollstartdate, 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 @payrollstartdate and @payrollenddate replace payrollstartdate, 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 100+
I think I solved this problem. I just created a module:

Module Module1
Public payperiodstartdate As Date
Public payperiodenddate 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
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...
0
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...
4
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...
3
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...
4
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:...
2
by: Dino L. | last post by:
How can I run stored procedure (MSSQL) ?
5
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 ??...
4
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...
1
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...
1
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....
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

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.