Home All Groups Group Topic Archive Search About
Author
12 Sep 2006 7:11 AM
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

Author
12 Sep 2006 10:57 AM
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


Bonzol wrote:
Show quoteHide quote
> 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
Author
12 Sep 2006 3:55 PM
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
Author
12 Sep 2006 6:41 PM
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

Bonzol wrote:
Show quoteHide quote
> 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
Author
12 Sep 2006 8:09 PM
Bonzol
oh I got it! took all night but I figure it out on my own.

thanx for your help anyway, appreciate it greatly.
rowe_newsgroups wrote:

Show quoteHide quote
> 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
>
> Bonzol wrote:
> > 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
Author
12 Sep 2006 9:05 PM
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
Author
12 Sep 2006 11:11 PM
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

Bonzol wrote:
Show quoteHide quote
> 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