Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 Development Tools
 ASP.NET
 Must declare the scalar variable "@Product_ID".

Author  Topic 

hima
Starting Member

7 Posts

Posted - 2007-11-10 : 02:00:02
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()

JBelthoff
Posting Yak Master

173 Posts

Posted - 2007-11-10 : 08:29:04
The error is telling you exactly what is wrong. Your SQL is this:

insert into Product_Small_Image(Product_ID,Img) values(@Product_ID ,@Img)


You must Declare those variables.

Declare @Product_ID bigint
Declare @Img varbinary(SIZE)

Then you have to set or select those variables.

JBelthoff
• Hosts Station is a Professional Asp Hosting Provider
› As far as myself... I do this for fun!
Go to Top of Page

jsmith8858
Dr. Cross Join

7423 Posts

Posted - 2007-11-10 : 08:47:18
Actually, those are declared as parameters in the sqlcommand so you should be all set. Try adding the parameters to the sqlcommand after setting the commandtext, instead of before.

- Jeff
http://weblogs.sqlteam.com/JeffS
Go to Top of Page

hima
Starting Member

7 Posts

Posted - 2007-11-12 : 04:34:28
quote:
Originally posted by hima

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()

Go to Top of Page

hima
Starting Member

7 Posts

Posted - 2007-11-12 : 04:40:37
quote:
Originally posted by hima

quote:
Originally posted by hima

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()





hi firends...
thanks for your suggestions...
i got the answer
Error was in the parameter name
here I used OLEDB connection
in OLEDB, parameter declaration is using '?' insted of '@parametername'.
and parameter.add statement should be like
cm.Parameters.Add("?p2", OleDbType.bigint)
cm.Parameters("?P2").Value =1


that was the error
Go to Top of Page

jsmith8858
Dr. Cross Join

7423 Posts

Posted - 2007-11-12 : 08:44:09
If you are connecting to SQL Server in .NET, you should be using a SqlConnection and not an OledbConnection.

- Jeff
http://weblogs.sqlteam.com/JeffS
Go to Top of Page
   

- Advertisement -