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!
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= ?)"
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...
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")
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...
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!"
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
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???
What output is it giving you? And now can you post your new code so we can see what it looks like
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
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.
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.
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.......?????