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

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




Help with SqlParameter Error

 
Reply to this topicStart new topic

Help with SqlParameter Error, Display the data for the selected item from a combo box.

Sonia B
27 Feb, 2008 - 07:32 AM
Post #1

New D.I.C Head
*

Joined: 24 Feb, 2008
Posts: 12

Please see the following code i am trying to display the data for the selected item from a combo box.
I have filled the combo box using its datasourse and displaymember properties.
I dont know where i am going wrong please help.
The ERROR that i get is "An SqlParameter with ParameterName 'EmployeeCode' is not contained by this SqlParameterCollection."
I checked the table name hundreth time it is "EmployeeCode"


CODE

con.ConnectionString = "Data Source = Ankita; Initial Catalog = Ankita; Integrated Security =true"
        sql = "SELECT * FROM EmployeeDetails WHERE EmployeeCode='" & cboEmployeeCode.Text & "'"
        Dim da As New SqlDataAdapter(sql, con)
        ds.Clear()
        da.SelectCommand.Parameters("EmployeeCode").Value = "'cboEmployeeCode.Text'"
        da.Fill(ds, "EmployeeDetails")



i tried the sql string in this way too
sql = "SELECT * FROM EmployeeDetails WHERE (EmployeeCode= ?)"

But no use..........Plz help.




User is offlineProfile CardPM
+Quote Post

Martyr2
RE: Help With SqlParameter Error
27 Feb, 2008 - 09:43 AM
Post #2

Programming Theoretician
Group Icon

Joined: 18 Apr, 2007
Posts: 5,660



Thanked: 313 times
Expert In: C/C++, Java, VB, VB.NET, C#, PHP, Web Development, HTML & CSS, Javascript

My Contributions
The problem is not with the query or whether or not the column exists in your database, it is because you are missing a step in your setup of the parameters collection. You have to ADD it. Check out the Add method of the command.Parameters collection. Classic example...

CODE


da.SelectCommand.Parameters.Add("@EmployeeCode", SqlDbType.NVarChar, 50).value = cboEmployeeCode.Text


You have to remember to actually add the field, its type, and its length as a parameter to the Parameters collection. I hope this helps you out.

Oh and btw, you will want to be using the question mark for your query. Enjoy!

"At DIC we be command parameter adding code ninjas!" decap.gif

This post has been edited by Martyr2: 27 Feb, 2008 - 09:46 AM
User is offlineProfile CardPM
+Quote Post

PsychoCoder
RE: Help With SqlParameter Error
27 Feb, 2008 - 09:52 AM
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
Not to step on anyone's toes here and I hope no offense is taken with this reply but here goes:

The preferred method, with .Net 2.0, is to use the AddWithValue Method of the SqlParameterCollection. Also, the way your query stands right now it is a prime candidate for a SQL Injection attack. If you're not using stored procedures for your data query then you should be using Parameterized SQL. Here is how that works:


vb

con.ConnectionString = "Data Source = Ankita; Initial Catalog = Ankita; Integrated Security =true"
'Use parameterized SQL for our query
sql = "SELECT * FROM EmployeeDetails WHERE EmployeeCode= @EmployeeCode"
Dim da As New SqlDataAdapter(sql, con)
ds.Clear()
'now use AddWithValue
da.SelectCommand.Parameters.AddWithValue("@EmployeeCode", cboEmployeeCode.Text)
da.Fill(ds, "EmployeeDetails")


Try that and let me know how it works smile.gif
User is offlineProfile CardPM
+Quote Post

Sonia B
RE: Help With SqlParameter Error
27 Feb, 2008 - 11:24 AM
Post #4

New D.I.C Head
*

Joined: 24 Feb, 2008
Posts: 12

QUOTE(Martyr2 @ 27 Feb, 2008 - 10:43 AM) *

The problem is not with the query or whether or not the column exists in your database, it is because you are missing a step in your setup of the parameters collection. You have to ADD it. Check out the Add method of the command.Parameters collection. Classic example...

CODE


da.SelectCommand.Parameters.Add("@EmployeeCode", SqlDbType.NVarChar, 50).value = cboEmployeeCode.Text


You have to remember to actually add the field, its type, and its length as a parameter to the Parameters collection. I hope this helps you out.

Oh and btw, you will want to be using the question mark for your query. Enjoy!

"At DIC we be command parameter adding code ninjas!" decap.gif





Thanks a lot i will definately try that out.


QUOTE(PsychoCoder @ 27 Feb, 2008 - 10:52 AM) *

Not to step on anyone's toes here and I hope no offense is taken with this reply but here goes:

The preferred method, with .Net 2.0, is to use the AddWithValue Method of the SqlParameterCollection. Also, the way your query stands right now it is a prime candidate for a SQL Injection attack. If you're not using stored procedures for your data query then you should be using Parameterized SQL. Here is how that works:


vb

con.ConnectionString = "Data Source = Ankita; Initial Catalog = Ankita; Integrated Security =true"
'Use parameterized SQL for our query
sql = "SELECT * FROM EmployeeDetails WHERE EmployeeCode= @EmployeeCode"
Dim da As New SqlDataAdapter(sql, con)
ds.Clear()
'now use AddWithValue
da.SelectCommand.Parameters.AddWithValue("@EmployeeCode", cboEmployeeCode.Text)
da.Fill(ds, "EmployeeDetails")


Try that and let me know how it works smile.gif




Thanks a lot i tried that out its not giving me a run time error atleast but it is not giving me the desired output also. Now whats the problem???
User is offlineProfile CardPM
+Quote Post

PsychoCoder
RE: Help With SqlParameter Error
27 Feb, 2008 - 12:10 PM
Post #5

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
What output is it giving you? And now can you post your new code so we can see what it looks like smile.gif
User is offlineProfile CardPM
+Quote Post

Sonia B
RE: Help With SqlParameter Error
27 Feb, 2008 - 01:09 PM
Post #6

New D.I.C Head
*

Joined: 24 Feb, 2008
Posts: 12

QUOTE(PsychoCoder @ 27 Feb, 2008 - 01:10 PM) *

What output is it giving you? And now can you post your new code so we can see what it looks like smile.gif



It is not selecting the selected item from the combo box and in the combo box 1st item is repeated as the last item for god's sake it is a primary key.

CODE

Private Sub cboEmployeeCode_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cboEmployeeCode.SelectedIndexChanged
        con.ConnectionString = "Data Source = Ankita; Initial Catalog = Ankita; Integrated Security =true"
       sql = "SELECT * FROM EmployeeDetails WHERE EmployeeCode=@EmployeeCode"
        Dim da As New SqlDataAdapter(sql, con)
        'ds.Clear()
da.SelectCommand.Parameters.AddWithValue("@EmployeeCode", cboEmployeeCode.Text)
        da.Fill(ds, "EmployeeDetails")
    End Sub



i have commented ds.Clear() coz it empties the combo box

User is offlineProfile CardPM
+Quote Post

PsychoCoder
RE: Help With SqlParameter Error
27 Feb, 2008 - 01:18 PM
Post #7

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
Lets make some changes to your code


vb

Private Sub cboEmployeeCode_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cboEmployeeCode.SelectedIndexChanged
con.ConnectionString = "Data Source = Ankita; Initial Catalog = Ankita; Integrated Security =true"
sql = "SELECT * FROM EmployeeDetails WHERE EmployeeCode=@EmployeeCode"
'Create a SqlCommand Object
Dim cmd As New SqlCommand()
'Now we will set some properties of our SqlCommand Object
With cmd
.CommandText = sql
.CommandType = CommandType.Text
.Parameters.AddWithValue("@EmployeeCode", cboEmployeeCode.Text)
.Connection = con
End With
Dim da As New SqlDataAdapter()
'ds.Clear()
'Now set the SelectCommand property of our adapter
da.SelectCommand = cmd
'Now fill our DataSet
da.Fill(ds, "EmployeeDetails")
End Sub



You dont have to use this code, just wanted to show you how I would do it if I were solving this problem. If you've already declared a new SqlCommand Object then you can get rid of the one I created in the procedure and set it's properties like I set mine above.
User is offlineProfile CardPM
+Quote Post

Sonia B
RE: Help With SqlParameter Error
27 Feb, 2008 - 01:38 PM
Post #8

New D.I.C Head
*

Joined: 24 Feb, 2008
Posts: 12

QUOTE(PsychoCoder @ 27 Feb, 2008 - 02:18 PM) *

Lets make some changes to your code


vb

Private Sub cboEmployeeCode_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cboEmployeeCode.SelectedIndexChanged
con.ConnectionString = "Data Source = Ankita; Initial Catalog = Ankita; Integrated Security =true"
sql = "SELECT * FROM EmployeeDetails WHERE EmployeeCode=@EmployeeCode"
'Create a SqlCommand Object
Dim cmd As New SqlCommand()
'Now we will set some properties of our SqlCommand Object
With cmd
.CommandText = sql
.CommandType = CommandType.Text
.Parameters.AddWithValue("@EmployeeCode", cboEmployeeCode.Text)
.Connection = con
End With
Dim da As New SqlDataAdapter()
'ds.Clear()
'Now set the SelectCommand property of our adapter
da.SelectCommand = cmd
'Now fill our DataSet
da.Fill(ds, "EmployeeDetails")
End Sub



You dont have to use this code, just wanted to show you how I would do it if I were solving this problem. If you've already declared a new SqlCommand Object then you can get rid of the one I created in the procedure and set it's properties like I set mine above.





Actually i had tried this code before moving on to the previous one because it is showing an error near " CommandType.Text "
Tells it is ambigious, imported from the namespaces.

User is offlineProfile CardPM
+Quote Post

PsychoCoder
RE: Help With SqlParameter Error
27 Feb, 2008 - 02:36 PM
Post #9

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
You do have Imports System.Data.SqlClient at the top of your code file correct?
User is offlineProfile CardPM
+Quote Post

Sonia B
RE: Help With SqlParameter Error
27 Feb, 2008 - 07:44 PM
Post #10

New D.I.C Head
*

Joined: 24 Feb, 2008
Posts: 12

QUOTE(PsychoCoder @ 27 Feb, 2008 - 03:36 PM) *

You do have Imports System.Data.SqlClient at the top of your code file correct?



Yup, ofcourse i have........I dont know whats the problem.
I crossed checked with my colleague he has done the same things and its working for him why not me.......?????
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 1/8/09 10:58PM

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