How can I use longer variables in my sql procedures?

Posted on 2007-11-23
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

Question by:BIGZIPZ1
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
LVL 18

Expert Comment

ID: 20339916

Dim nameTable As Integer = Table1

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


Author Comment

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.
LVL 18

Accepted Solution

Yveau earned 500 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

Free learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.


Author Comment

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

Error converting data type nvarchar to int.
LVL 18

Expert Comment

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

Hope this helps ...

Author Comment

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"
        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))
    End Sub

Open in new window

LVL 18

Expert Comment

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

Hope this helps ...

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Crystal reports login after closing form 3 56
VB.NET System.NullReferenceException 1 43
FTP recursive download files 5 27
Updating Printer Details in VB.Net 16 42
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
In this article we will get to know that how can we recover deleted data if it happens accidently. We really can recover deleted rows if we know the time when data is deleted by using the transaction log.
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below.…
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …

734 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