Must declare the scalar variable Error C# ASP.NET Sql Command with Select Statement -


i running problem prompts me error says "must declare scalar variable "@id". happens when when add parameter in, when add variable in, works fine.

down below code gives me problem:

cmd.commandtext = "select * attendee id=@id"; cmd.connection = conn; cmd.parameters.addwithvalue("@id",id); conn.open(); cmd.executenonquery(); sqldataadapter da = new sqldataadapter(cmd.commandtext,cmd.connection); da.fill(dt); conn.close(); 

when add in variable such as:

cmd.commandtext = "select * attendee id=" + id; 

it works fine.

in sqldataadapter constructor pass sqlcommand.commandtext.
text (just string) not enough define parameter @id defined instead in sqlcommand.parameters collection

you have 2 options

 sqldataadapter da = new sqldataadapter(cmd.commandtext,cmd.connection);  da.selectcommand.parameters.addwithvalue("@id",id);  da.fill(dt); 

or build adapter using command

 sqldataadapter da = new sqldataadapter(cmd); 

as added advice, suggest not use addwithvalue, instead use overload of add takes type parameter

cmd.parameters.add("@id",sqldbtype.int).value = id; 

see can stop using addwithvalue already?


Comments

Popular posts from this blog

account - Script error login visual studio DefaultLogin_PCore.js -

xcode - CocoaPod Storyboard error: -