?
Solved

How can I use longer variables in my sql procedures?

Posted on 2007-11-23
7
Medium Priority
?
165 Views
Last Modified: 2010-04-23
I have a stored procedure which operates with an @parameter which I call using the attached code. The variable nameTable is is the name of the current database table that I want the procedure to use. When I run the code I get an error saying  "Conversion from string "Table1" to type 'Integer' is not valid."

I tried editing the procedure from "@parameter1 int" to "@parameter1 str" but it causes an error.
Dim nameTable As Integer = Table1
cmdProcedure.Parameters.Add(New SqlParameter("@parameter1", nameTable))

Open in new window

0
Comment
Question by:BIGZIPZ1
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
7 Comments
 
LVL 18

Expert Comment

by:DarrenD
ID: 20339916
Hi,

Dim nameTable As Integer = Table1

If Table1 variable is a string then you will get the error above...

Darren
0
 

Author Comment

by:BIGZIPZ1
ID: 20340050
I don't understand, you just stated one of the lines of code I said caused an error and told me it would cause an error.
0
 
LVL 18

Accepted Solution

by:
Yveau earned 2000 total points
ID: 20340158
you are trying to put the value 'Table1' into a integer parameter nameTable ... that won't work.
you have to declare the parameter nameTable as a string.

... and I think you need to put quotes around Table1, but I'm not sure ... ("Table1" in stead of Table1)

Hope this helps ...

Dim nameTable As String = Table1   ' or "Table1" ?
cmdProcedure.Parameters.Add(New SqlParameter("@parameter1", nameTable))

Open in new window

0
10 Questions to Ask when Buying Backup Software

Choosing the right backup solution for your organization can be a daunting task. To make the selection process easier, ask solution providers these 10 key questions.

 

Author Comment

by:BIGZIPZ1
ID: 20340204
Hey, the above code gives the following error:

Error converting data type nvarchar to int.
0
 
LVL 18

Expert Comment

by:Yveau
ID: 20340224
on line 1 or 2 ?
Is @parameter1 an integer ? Should be a varchar(128) ...

Hope this helps ...
0
 

Author Comment

by:BIGZIPZ1
ID: 20340238
Hey, I changed to vchar128 like you said and I now get this error:

Conversion from string "Table1" to type 'Integer' is not valid.

I have attached the full code below for you to see. I appreciate you trying to help!

P.S in the procedure editor, the data type is "@parameter1 varchar(128)," as you told me to put.
Public Class RateBox
    Private Sub OK_Button_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OK_Button.Click
        Dim scoreRating As Integer = ListBox1.SelectedItem 'this is the rating score out of 5'
        Dim cnPubs As New SqlConnection("Server=MARK-HOME\SQLEXPRESS;Database=Library;Integrated Security=SSPI;")
        Dim nameTable As Integer = "Table1"
        cnPubs.Open()
        Dim cmdProcedure As New SqlCommand("dbo.StoredProcedure1", cnPubs)
        cmdProcedure.CommandType = CommandType.StoredProcedure
        cmdProcedure.Parameters.Add(New SqlParameter("@parameter2", scoreRating))
        cmdProcedure.Parameters.Add(New SqlParameter("@parameter1", nameTable))
        cmdProcedure.ExecuteNonQuery()
        Form1.JackBennyTableAdapter.Fill(Form1.LibraryDataSet.JackBenny)
        Me.Close()
    End Sub

Open in new window

0
 
LVL 18

Expert Comment

by:Yveau
ID: 20346288
Try switching lines 9 and 10 ...
What does the header of the stored procedure look like ?
My guess would be:
    create procedure dbo.StoredProcedure1
        @parameter1 varchar(128)
    ,   @parameter2 int
    as
    ...

Hope this helps ...
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

INTRODUCTION: While tying your database objects into builds and your enterprise source control system takes a third-party product (like Visual Studio Database Edition or Red-Gate's SQL Source Control), you can achieve some protection using a sing…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question