Welcome to Dream.In.Code
Become a VB.NET Expert!

Join 150,098 VB.NET Programmers for FREE! Get instant access to thousands of VB.NET experts, tutorials, code snippets, and more! There are 1,864 people online right now. Registration is fast and FREE... Join Now!




an sqlparameter with parameter error

 
Reply to this topicStart new topic

an sqlparameter with parameter error, an sqlparameter with parameter name is not contained

RudyVB.net
9 May, 2008 - 09:33 AM
Post #1

D.I.C Head
**

Joined: 3 May, 2008
Posts: 61

Hello all!

I'm getting this error, "an sqlparameter with parameter name '@VisID' is not contained by this sqlParameterCollection.

I have a datagrid on my form, i'm just updating my datagrid with a SP. Again, I have done this before, but it's been awhile. Here is my code.

CODE
Private Sub updateVisTable()
        Dim constr As New SqlConnection(PVDBConn)
        ' Try
        cmdUpd = New SqlCommand("updVisName", constr)
        cmdUpd.CommandType = CommandType.StoredProcedure
        cmdUpd.Parameters.Add("@VisName", SqlDbType.NVarChar).Value = (Me.dgVisTbl.SelectedCells(0).Value.ToString)
        cmdUpd.Parameters("@VisID").Value = (Me.dgVisTbl.SelectedCells(1).Value.ToString)


        constr.Open()
        cmdUpd.ExecuteNonQuery()
        constr.Close()

         Catch ex As Exception
        MsgBox(ex.Message.ToString)
        End Try
    End Sub


my grid has 4 col. The first is VisID, that is hidden, then there is visboo, visnam, visport.

What am I missing?!

TIA!!

Rudy
User is offlineProfile CardPM
+Quote Post

Jayman
RE: An Sqlparameter With Parameter Error
9 May, 2008 - 11:55 AM
Post #2

Student of Life
Group Icon

Joined: 26 Dec, 2005
Posts: 7,319



Thanked: 66 times
Dream Kudos: 500
Expert In: Everything

My Contributions
You need to ADD it to the collection before you can assign a value to it. You did it just fine on the first parameter "@VisName", all you needed to do is repeat the same procedure with both of them.

CODE

cmdUpd.Parameters.Add("@VisID", SqlDbType.NVarChar).Value = (Me.dgVisTbl.SelectedCells(1).Value.ToString)

User is offlineProfile CardPM
+Quote Post

PsychoCoder
RE: An Sqlparameter With Parameter Error
9 May, 2008 - 12:03 PM
Post #3

using DIC.Core;
Group Icon

Joined: 26 Jul, 2007
Posts: 9,483



Thanked: 161 times
Dream Kudos: 9075
Expert In: VB, VB.Net, C#, SQL, ASP, ASP.Net, Web Development, HTML, CSS, Win32 API, Javascript, mySQL, J#, Boo.Net

My Contributions
An even better way of doing this is to use AddWithValue instead of Add. Here is an example


vb

Private Sub updateVisTable()
Dim constr As New SqlConnection(PVDBConn)
' Try
cmdUpd = New SqlCommand("updVisName", constr)
cmdUpd.CommandType = CommandType.StoredProcedure
cmdUpd.Parameters.AddWithValue("@VisName", Me.dgVisTbl.SelectedCells(0).Value.ToString())
cmdUpd.Parameters.AddWithValue("@VisID" , Me.dgVisTbl.SelectedCells(1).Value.ToString())

constr.Open()
cmdUpd.ExecuteNonQuery()
constr.Close()

Catch ex As Exception
MsgBox(ex.Message.ToString)
End Try
End Sub



When using AddWithValue you don't have to declare it's type when the procedure is executed it takes care of this for you. Just my 2¢
User is offlineProfile CardPM
+Quote Post

RudyVB.net
RE: An Sqlparameter With Parameter Error
9 May, 2008 - 12:47 PM
Post #4

D.I.C Head
**

Joined: 3 May, 2008
Posts: 61

Thanks guys!

I actually tried both ways. But I'm still getting an error, although a diffrent error. And that is "Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: index"

So, .SelectedCells(2) would mean the selected cells in Column 2. Is that right? It's like it's not grabbing my new value I'm typing in the datagrid. So if I type a new value at row 4, column 2. That should go over. But I don't think it is. I know it's something I'm doing wrong.

Any thoughts? I'll keep plugging!

Thanks!

I want to add on to this. So here is my new code.
CODE
Dim constr As New SqlConnection(PVDBConn)
        MsgBox(Me.dgVisTbl.SelectedCells(0).Value.ToString)
        Try
            cmdUpd = New SqlCommand("updVisName", constr)
            cmdUpd.CommandType = CommandType.StoredProcedure
            cmdUpd.Parameters.Add("@VisName", SqlDbType.NVarChar).Value = (Me.dgVisTbl.SelectedCells(0).Value.ToString)
            cmdUpd.Parameters.Add("@VisID", SqlDbType.Int).Value = (Me.dgVisTbl.SelectedCells(0).Value.ToString)
          
            constr.Open()

            cmdUpd.ExecuteNonQuery()
            constr.Close()

        Catch ex As Exception
            MsgBox(ex.Message.ToString)
        End Try


The other I got was because I had .SlectedCells(2) on the visname. Nowif I run it with that MsgBox(Me.dgVisTbl.SelectedCells(0).Value.ToString). That tells me I'm reading the value properly. But then I get this error,

"Failed to convert parameter from a string to a int32" First of all, I'm changing the vis name, which is a nvarchar type, not a int.
Second of all, I'm still confused what the (0) stands for in selected cells.

Thanks again!

Rudy

This post has been edited by RudyVB.net: 9 May, 2008 - 01:30 PM
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 1/9/09 12:26AM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live VB.NET Help!

VB.NET Tutorials

Reference Sheets

VB.NET Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month