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
    ComboBoxPanneCentreAppel.Items.Add(reader.GetInt32(reader.GetOrdinal("Code_Cable")))
End While
reader.Close()

Answers


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

...you 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

How to show spinning progress dialogue with using separate Async Task Class

java android android-asynctask progressdialog

In my app, i'm reading text files from web server using Async Tasking from a separate class because it is used in many activities, so i've made a dedicated class.

How to prevent JScrollPane from automatically scrolling down?

java swing jscrollpane jtextarea

I am using a JScrollPane which contains inside it a JTextArea. The problem is when the JTextArea is populated with a lot of text, the JScrollPane automatically scrolls down. What property should I ...