Parameter @code1 has no default value

I'm creating this code to use two comboboxes, one that depends on the first, but when I'm trying to select the "code_Zone" from the Table "Zone" I have this exception:

Parameter @code1 has no default value

This is my code:

Dim cmd3 = New OleDbCommand("select [Code_Zone] from [Zone] where [Nom_Zone]= '@code';", connection)
cmd3.Parameters.AddWithValue("@code", ComboBoxNomZoneDeclaration.SelectedText.ToString)
Dim valeur = cmd3.ExecuteScalar
Dim commande = New OleDbCommand("select [Code_Cable] from [CableEnFibre] where [Code_Zone]=@code1;", connection)
commande.Parameters.AddWithValue("@code1", valeur)
Dim reader = commande.ExecuteReader   'there is the exception 
While reader.Read
End While


Well the first problem are the quotes around the @code parameter placeholder. In this way the placeholder is treated as a literal string. Now when you execute the cmd3 command the database engine search the column Nom_Zone for the string "@code" and of course nothing is found. So the value of the variable valeur is nothing. Whatever is the selected text in the combobox is simply ignored.

And now the second problem. As you can read in MSDN about SelectedText this property is not the correct one to use if you want to retrieve the content of the current SelectedItem when the combo has not the focus should be aware that the selection can change automatically because of user interaction. For example, if you retrieve the SelectedText value in a button Click event handler, the value will be an empty string. This is because the selection is automatically cleared when the input focus moves from the combo box to the button.

So you should use the SelectedItem property or the SelectedValue depending on how you have filled the ComboBox. Please use the debugger setting a breakpoint at the point where you are trying to read the value of the combo and check the value of your properties at that point

Need Your Help

Serving files with ember data

ember.js ember-data ember-cli pdfobject

I have an app that allows a user to view embedded PDFs. Those PDFs are generated by the server, and are displayed on the page using PDFObject. What is the proper way to serve these files? Right now...

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.