locked
Problem with declaration of parameter RRS feed

  • Question

  • Hi,

    I have this code

    string insertString="insert into " + line + " values(";
                for (int i = 1; i <= number; i++)
                {
                    insertString += "@inputs" + i; // +",";
                    if (i != number)
                    {
                        insertString += ", ";
                    }
                    else
                    {
                        insertString += ")";
                    }
                }
    
    ...
    ...
    
     {
                         Console.WriteLine(@"Please enter" + fields[j]);
    
                         switch (types[j])
                         {
                            case "int": cmd.Parameters.Add("@inputs" + j, SqlDbType.Int);
                                 cmd.Parameters["@inputs" + j].Value = int.Parse(fields[j]);
                                 break;     
                             case "float": cmd.Parameters.Add("@inputs" + j, SqlDbType.Float);
                                 cmd.Parameters["@inputs" + j].Value = float.Parse(fields[j]);
                                 break;
                             default: cmd.Parameters.Add("@inputs" + j, SqlDbType.VarChar,20);
                                 cmd.Parameters["@inputs" + j].Value = fields[j];
                                 break;                     }
    
                     }
                             
                cmd.Prepare();
    
                cmd.ExecuteNonQuery();
    
    

    but I get an error  "must declare the scalar variable @input1 " and the program stops after the first try.

    Can anybody help me please?

    Thanks

    • Changed type Kalman Toth Wednesday, April 2, 2014 6:57 PM
    • Moved by Kalman Toth Wednesday, April 2, 2014 7:04 PM Not database design
    Wednesday, April 2, 2014 3:16 PM

All replies