Khaled Mohamed
Khaled Mohamed

Reputation: 217

Error at inserting in sql

I have created some variables and I want to insert them into a database, I used a for loop to insert some values in array but I got that this error

Incorrect syntax near 'nvarchar'.
Must declare the scalar variable "@finalwords".

Here it is my code

string []finalwords=new string[13000];

for (int h=0;h<wordsbeforesoundex.Length;h++)
{
   if (wordsbeforesoundex[h] == "")
      continue;

   finalwords[indexer] = wordsbeforesoundex[h];
   indexer++;
}

for (int l = 0; l < words.Length; l++)
{
   con.Open();

   SqlCommand cmd = new SqlCommand("INSERT INTO TableFFF (Data) VALUES (@finalwords[l])", con);

   cmd.Parameters.Add(new SqlParameter("@finalwords[l]", finalwords[l]));

   cmd.ExecuteNonQuery();
}

Upvotes: 0

Views: 73

Answers (2)

Rahul
Rahul

Reputation: 77846

Rather change it like

SqlCommand cmd = 
new SqlCommand("INSERT INTO TableFFF (Data) VALUES (@finalwords_idx)", con);

cmd.Parameters.Add(new SqlParameter("@finalwords_idx", finalwords[l]));

Upvotes: 4

Saroop Trivedi
Saroop Trivedi

Reputation: 2265

your insert query is not proper, change the query

"INSERT INTO TableFFF (Data) VALUES (@finalwords["+l+"])"

Upvotes: -1

Related Questions