Click here to Skip to main content
15,911,327 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi All,

I'm beginner to ASP.NET with C# code behind.
i'm working on my course assignment which part of it need to upload and retrieve photo from sql server, below is my code.

C#
try
            {
                FileUpload img = (FileUpload)imgUpload;
                if (imgUpload.HasFile)
                {
                    string contentType = img.PostedFile.ContentType;
                    byte[] fileData = new byte[img.PostedFile.InputStream.Length];
                    img.PostedFile.InputStream.Read(fileData, 0, fileData.Length);
                    SqlConnection con = new SqlConnection();
                    con.ConnectionString = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
                    con.Open();
                    string sql = "INSERT INTO image(img_name,img_photo) VALUES(@enm, @eimg) SELECT @@IDENTITY";
                    SqlCommand cmd = new SqlCommand(sql, con);
                    cmd.Parameters.AddWithValue("@enm", txtEName.Text.Trim());
                    cmd.Parameters.AddWithValue("@eimg", fileData);
                    int id = Convert.ToInt32(cmd.ExecuteScalar());
                    lblResult.Text = String.Format("Product ID is {0}", id);
                }
            }
            catch
            {
                lblResult.Text = "There was an error";
            }


But this having some problem while the code goes until i click on the button, it will keep on going over to "there was an error". and no data being upload to the db..

thousand thanks to anyone that could help
Posted
Updated 6-Apr-13 1:20am
v2
Comments
Zoltán Zörgő 6-Apr-13 5:48am    
This is not fully your code. You have it from here: http://www.dotnetcurry.com/ShowArticle.aspx?ID=129 Right?
Please provide a more detailed exception description than "there was an error".
Is your table the same as in the article?
GrAp3 6-Apr-13 5:51am    
not all, i'm referencing it but keep on changing till i don't know where's the problem now...
i believe there's no problem on the parameter that saving the data to db ..
Zoltán Zörgő 6-Apr-13 5:57am    
And the exception text is....?
GrAp3 6-Apr-13 5:59am    
what did i show is

catch
{
lblResult.Text = "There was an error";
}

when i click on the button, it try and catch, thinks there's some problem which unable to upload the photo so it goes to show me "There was an error" at the lblResult.Text, there's not exception or anything that stop my debugging..
Zoltán Zörgő 6-Apr-13 6:03am    
Awesome! There is an exception but you handle it in the wrong way.
What about catching the exception itself, so you can see the concrete error?
catch (Exception ex)
{
lblResult.Text = ex.ToString();
}

Actually you should do some logging here, but at least we see the exception.

And by the way: you better use FILESTREAM instead of storing the file directly in the database.

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