Click here to Skip to main content
15,895,667 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more: , +
I am working on a website where I am facing a problem, I am beginner so please don't get angry on me. My question is: I have uploaded an image file into database and displayed it in the box simultaneously. Now what I want is when I jump to next page the same picture will appear in the box there so I can do some resizing of it.

What I have done so far is following piece of code but no success it is giving the error

HTML
Must declare the scalar variable "@id"


I have google for solution but there is no solution I'd found that working for me, any help will be appreciated, thanks in advance

ASPX Part is:

C#
<asp:Image ID="image" runat="server" ImageUrl='<%# Eval("image") %>' width="450" />


CS Part is:

C#
SqlConnection con = new SqlConnection("Data Source=Tim-PC\\SQLEXPRESS;Initial Catalog=AP_Data;Integrated Security=True");
        protected void Page_Load(object sender, EventArgs e)
        {
            SqlDataAdapter sda = new SqlDataAdapter("SELECT image FROM photo WHERE id=@id", con);
            DataTable dt = new DataTable();
            sda.Fill(dt);
            GridView1.DataSource = dt;
            DataBind();
        }
Posted

1 solution

1.The error is generated by the error in your SQL string. You have a parameter "@id" that was not linked with a value before to use the SQL in adapter.

2.The solution for the error is to create a SQL command and to set the value for the @id param, before to create the adapter:
C#
SqlCommand command = new SqlCommand(commandText, con);
command.Parameters.Add("@id", SqlDbType.Int);
command.Parameters["@id"].Value = imageID; //here must be the image ID!
SqlDataAdapter sda = new SqlDataAdapter(command); //Create the adapter based on the command!
//... your code...


3.The answer to your 2nd error from your comment to my solution is: you have to correct your code from your page like below:
ASP.NET
<asp:Image ID="image" runat="server" ImageUrl='<%# string.Format("image-size.aspx?id={0}", Eval("id"))%>' width="450" /> 
 
Share this answer
 
v2
Comments
Member 10922838 7-Jul-14 4:30am    
Thank you for your reply Sir, It make sense, but there is another problem comes out, the aspx part is saying the following thing

The server tag is not well formed.

Line 55: <asp:Image ID="image" runat="server" ImageUrl="<%# Eval("id", "image-size.aspx?id={0}") %>'><%# Eval("image") %>" width="450" />
Raul Iloc 7-Jul-14 6:47am    
See my update in 3rd point of my solution above!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900