Asked by:
Problem with declaration of parameter

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
-
This isn't design related - and it is particularly sql server related. As been suggested many times, post your question to the appropriate forum.Wednesday, April 2, 2014 3:17 PM
-
This is apparently a C# question. Please post your question in the C# forum:
http://social.msdn.microsoft.com/Forums/vstudio/en-US/home?forum=csharpgeneral
Wednesday, April 2, 2014 3:30 PM -
ok
Wednesday, April 2, 2014 3:32 PM -
I am moving it to C#.
Kalman Toth Database & OLAP Architect SELECT Video Tutorials 4 Hours
New Book / Kindle: Exam 70-461 Bootcamp: Querying Microsoft SQL Server 2012
Wednesday, April 2, 2014 7:04 PM -
Hi pet06,
It seems like there is code error in our insertstring, try to add breakpoint into your code and debug it. You also can provide a project here and I will fix it for you.
Regards,
We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
Click HERE to participate the survey.Monday, April 21, 2014 6:53 AM -
Hi Pet06,
Please check your local variable"j", does this variable include the value "1" in your code?
Lisa Zhu [MSFT]
Monday, April 21, 2014 7:58 AM