Error:Must declare the scalar variable "@Product_ID".

716 views
Skip to first unread message

Annie

unread,
Nov 10, 2007, 3:26:19 AM11/10/07
to DotNetDevelopment, VB.NET, C# .NET, ADO.NET, ASP.NET, XML, XML Web Services,.NET Remoting
hi friends.....
i'm using the following code to save an image to SQLSERVER2005using
asp.net.
i got the error message Must declare the scalar variable
"@Product_ID". while running the code
.
please help me..
its urgent

cn = New System.Data.OleDb.OleDbConnection("
Provider=SQLOLEDB;SERVER=SERVER\SQLEXPRESS;UID=sa;
PASSWORD=kadavan2486;DATABASE=Hard1;Persist Security Info=True;")
cn.Open()
cm.Connection = cn
cm.Parameters.Clear()
cm.Parameters.Add("@Product_ID", OleDbType.BigInt, 4)
cm.Parameters.Add("@Img", OleDbType.VarBinary, img.Length)
cm.Parameters("@Product_ID").Value = 1
cm.Parameters("@Img").Value = img

cm.CommandText = "insert into Product_Small_Image(Product_ID,Img)
values(@Product_ID ,@Img)"
cm.ExecuteNonQuery()
cn.Close()

Cerebrus

unread,
Nov 11, 2007, 2:29:11 AM11/11/07
to DotNetDevelopment, VB.NET, C# .NET, ADO.NET, ASP.NET, XML, XML Web Services,.NET Remoting
You usually get this sort of error when there's a syntax error with
your SQL statement. I cannot see any error, but you might try running
the statement in SQL server itself to check if the field names are
correct.

If I were you, I'd use a Stored Proc instead of dynamic SQL, which
defeats the purpose of using parameters.

Annie

unread,
Nov 12, 2007, 4:54:15 AM11/12/07
to DotNetDevelopment, VB.NET, C# .NET, ADO.NET, ASP.NET, XML, XML Web Services,.NET Remoting

> > cn.Close()- Hide quoted text -
>
> - Show quoted text -

hi friend ..

thanks for all your suggestions..
i got the answer
error was in the declaration of oledbcommand pararmeters.
correct declaration is like as follows..
cm.CommandText = "insert into Test(ID,Img) values(?,?)"
cm.Parameters.Add("?P1", OleDbType.BigInt)
cm.Parameters.Add("?p2", OleDbType.VarBinary)
cm.Parameters("?P1").Value =1
cm.Parameters("?P2").Value = img

Reply all
Reply to author
Forward
0 new messages