Reputation: 37
I have a table for translators, and each translator is capable of translating from one source language to many target languages. I am creating a Windows form application for such a table, and have tried using a listbox for such an entry, but it gives me the following error
The variable name '@lang_code' has already been declared. Variable names must be unique within a query batch or stored procedure.
Code:
List<String> SelectedItems=new List<String>();
foreach (System.Data.DataRowView s in listBox1.SelectedItems)
{
string select = s.ToString();
SelectedItems.Add(select);
myCmd.Parameters.AddWithValue("@lang_code", select);
}
Upvotes: 0
Views: 920
Reputation: 17001
You are adding a parameter named "@lang_code" multiple times to the same query. This isn't legal in any SQL language I'm aware of. This would be the equivalent of declaring multiple c# variables with the same name in the same scope.
It sounds to me like you actually want 3 separate tables with relationships between them.
A table of Languages, a table with Translators, and a table containing the languages that each Translator knows. The Languages table is just a list of all the languages that your application is aware of. The Translator table should have a private key, with one row per translator. The LanguagesKnown table should have one row per translator per language known, with a foreign key pointing to the primary key of the translator and the primary key of the language.
You will end up with a very shallow tree structure that a relational database can easily handle. It will look something like this:
Language
Translator
LanguageKnown
Upvotes: 0
Reputation: 550
Based on the error, looks like you have added the @lang_code parameter to myCmd (SqlCommand) and have not cleared that parameter collection. So the SqlCommand already has a parameter called @lang_code. You could create multiple commands within one SqlConnection, or clear the parameters collection prior to re-adding @lang_code
Upvotes: 1