Stored Procedures

B

Bonzol

Hey there,

I think this is not actually VB, but the language im using is VB. Ive
been doing all my code,, NOT using Stored procedures.. so my functions
are like.(in 2003)

visual basic
code:--------------------------------------------------------------------------------
Public Function vLookup(ByVal table As String, ByVal returnColumn As
String, ByVal checkColumn As String, ByVal checkValue As String) As
String
Dim StringToReturn As String

StringToReturn = ""
Dim SQL As String
SQL = "SELECT " + returnColumn + " from " + table + " where " +
checkColumn + " = " + checkValue + ""

Dim dataAdapter As System.Data.OleDb.OleDbDataAdapter
dataAdapter = New System.Data.OleDb.OleDbDataAdapter(SQL,
Me.OleDbConnection1)
Try
Dim dt As System.Data.DataTable
dt = New System.Data.DataTable
dataAdapter.Fill(dt)
If dt.Rows.Count > 0 Then
If dt.Rows(0).ItemArray.Length > 0 Then
StringToReturn = CStr(dt.Rows(0).Item(0))
End If
End If
Catch
StringToReturn = "-1"
End Try

Return StringToReturn
End Function

--------------------------------------------------------------------------------


MY question is how do I change this to function to us a stored
procedure that does the same thing. Stored procedures are in SQL server
2000? Now using .net 2005

lets call the stored proecedure stoCustomer

thanx in advance
 
R

rowe_newsgroups

Looking at your SQL statement, a "normal" stored procedure won't be
able to replace it. You'll have to use a stored procedure that uses
dynamic SQL. You may want to search the comp.databases.ms-sqlserver
newsgroup on what this means and how to write one. Also check out
http://support.sas.com/ctx/samples/index.jsp?sid=817 it's a great
article on using both ADO and ADO.NET to call stored procs with VB.NET
and C#. If you need further help please post again and I'll try to walk
you through whatever you need.

Thanks,

Seth Rowe
 
B

Bonzol

Thanx for your reply

Yeah I think I need some help. I had a look, but im only a beginner.

My connection string is

Me.OleDbConnection1 = New System.Data.OleDb.OleDbConnection
Me.OleDbConnection1.ConnectionString = "Integrated
Security=SSPI;Packet Size=4096;Data Source=""PIGMANIA"";Tag with " & _
"column collation when possible=False;Initial
Catalog=Northwind;Use Procedure fo" & _
"r Prepare=1;Auto Translate=True;Persist Security
Info=False;Provider=""SQLOLEDB.1" & _
""";Workstation ID=ASPNET;Use Encryption for Data=False"

that works.

Just showing me how to use that to call a storedprocedure to fill a
datatable(also an example with storeprocedure imputs would be nice),
would be really nice. then I can learn from that example. I found it
hard to find any examples that showed me exactly this.

Thanx in advance
 
R

rowe_newsgroups

Try this for the data adapter code. -- Not tested

(I would add the below imports statement)
Imports System.Data.OleDb

' Instantiate the DataAdapter and supply it the target
' stored procedure name and your connection variable
Dim DataAdapter as New OleDbDataAdapter("stoCustomer",
OleDbConnection1)
' Add the new parameter's name, type, and size
DataAdapter.SelectCommand.Parameters.Add("@CustName",
OleDbType.VarChar, 50)
' Assign the new parameter a value
DataAdapter.SelectCommand.Parameters("@CustName").Value = "Maria
Anders"

You should be ok to fill your datatable from there. If not please post
back.

Thanks,

Seth Rowe
 
B

Bonzol

oh I got it! took all night but I figure it out on my own.

thanx for your help anyway, appreciate it greatly.
 
B

Bonzol

oh wait

now im trying some other things

Dim stringtoreturn As System.Data.DataTable
' Try
Me.SqlConnection1.Open()
Me.SqlSelectCommand1.CommandText = "[CustOrdersOrders]"
Me.SqlSelectCommand1.Parameters("@CustomerID").Value = "VINET"
Me.SqlSelectCommand1.ExecuteNonQuery()
Me.SqlDataAdapter1 = New
System.Data.SqlClient.SqlDataAdapter(Me.SqlSelectCommand1.CommandText,
Me.SqlConnection1)
Dim dt As System.Data.DataTable
dt = New System.Data.DataTable
Me.SqlDataAdapter1.Fill(dt)

Me.SqlConnection1.Close()
stringtoreturn = dt
'Catch

'End Try
Return stringtoreturn

I seem unable to send anything to the stored procedure

error I get is this

{"An SqlParameter with ParameterName '@CustomerID' is not contained by
this SqlParameterCollection."}

the stored procedure defiantly needs that @customerID
 
R

rowe_newsgroups

Looks like you forgot to add the parameter first. Try changing this:
Me.SqlSelectCommand1.CommandText = "[CustOrdersOrders]"
Me.SqlSelectCommand1.Parameters("@CustomerID").Value = "VINET"
Me.SqlSelectCommand1.ExecuteNonQuery()

to this: (Note, I'm not sure of the type for the parameter.)

Me.SqlSelectCommand1.CommandText = "[CustOrdersOrders]"
---> Me.SqlSelectCommand1.Parameters.Add("@CustomerID", BigInt)
Me.SqlSelectCommand1.Parameters("@CustomerID").Value = "VINET"
Me.SqlSelectCommand1.ExecuteNonQuery()

Have Fun!

Seth Rowe
oh wait

now im trying some other things

Dim stringtoreturn As System.Data.DataTable
' Try
Me.SqlConnection1.Open()
Me.SqlSelectCommand1.CommandText = "[CustOrdersOrders]"
Me.SqlSelectCommand1.Parameters("@CustomerID").Value = "VINET"
Me.SqlSelectCommand1.ExecuteNonQuery()
Me.SqlDataAdapter1 = New
System.Data.SqlClient.SqlDataAdapter(Me.SqlSelectCommand1.CommandText,
Me.SqlConnection1)
Dim dt As System.Data.DataTable
dt = New System.Data.DataTable
Me.SqlDataAdapter1.Fill(dt)

Me.SqlConnection1.Close()
stringtoreturn = dt
'Catch

'End Try
Return stringtoreturn

I seem unable to send anything to the stored procedure

error I get is this

{"An SqlParameter with ParameterName '@CustomerID' is not contained by
this SqlParameterCollection."}

the stored procedure defiantly needs that @customerID
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top