Click here to Skip to main content
15,890,506 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i got the error converting data type nvarchar to int. i have coded that code by seeing video of asp.net about How to save image in the database.


C#
//ASP.NET CODE

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                Label1.Visible = false;
                HyperLink1.Visible = false;
            }

        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            HttpPostedFile postedFile = FileUpload1.PostedFile;
            string fileName = Path.GetFileName(postedFile.FileName);
            string fileExtension = Path.GetExtension(fileName);
            int fileSize = postedFile.ContentLength;

                if (fileExtension.ToLower() == ".jpg" || fileExtension.ToLower() == ".png"
                    || fileExtension.ToLower() == ".gif" || fileExtension.ToLower() == ".bmp")
                    
                {
                    Stream stream = postedFile.InputStream;
                    BinaryReader binaryreader = new BinaryReader(stream);
                    byte[] bytes = binaryreader.ReadBytes((int)stream.Length);

                    string connstring = ConfigurationManager.ConnectionStrings["DBCS"].ConnectionString;
                    //  string connstring = "data source=AMOD; initial catalog=DATA; integrated security=SSPI;";
                    using (SqlConnection con = new SqlConnection(connstring))
                    {
                        SqlCommand cmd = new SqlCommand("spUploadImage", con);
                        cmd.CommandType = CommandType.StoredProcedure;

                        SqlParameter paramName = new SqlParameter()
                           {
                               ParameterName = "@Name",
                               Value = fileName
                           };
                        cmd.Parameters.Add(paramName);

                        SqlParameter paramSize = new SqlParameter()
                        {
                            ParameterName = "@size",
                            Value = fileName
                        };
                        cmd.Parameters.Add(paramSize);

                        SqlParameter paramImageData = new SqlParameter()
                        {
                            ParameterName = "@ImageData",
                            Value = fileName
                        };
                        cmd.Parameters.Add(paramImageData);

                        SqlParameter paramNewId = new SqlParameter()
                        {
                            ParameterName = "@NewId",
                            Value = -1,
                            Direction = ParameterDirection.Output
                        };
                        cmd.Parameters.Add(paramNewId);

                        con.Open();
                        cmd.ExecuteNonQuery();
                        con.Close();

                        Label1.Visible = true;
                        Label1.Text = "UpLoaded Successfully";
                        Label1.ForeColor = System.Drawing.Color.Green;
                        HyperLink1.Visible = true;
                        HyperLink1.NavigateUrl = "~/WebForm2.aspx?Id=" + cmd.Parameters["@NewId"].Value.ToString();
                    }
                }
                else
                {
                    Label1.Visible = true;
                    Label1.Text = "Only Images( .jpg, .png, .bmp, gif) can be uploaded!!!!!!!!";
                    Label1.ForeColor = System.Drawing.Color.Red;
                    HyperLink1.Visible = false;
                }

            }



///DATABASE CODE

SQL
alter proc spUploadImage
@Name nvarchar(250),
@Size int,
@ImageData varbinary(MAX),
@NewId int output
as begin
insert into tblImage values(@Name,@Size,@ImageData)

select @NewId=SCOPE_IDENTITY()
end

select * from tblImage


What I have tried:

i have tried to save image to the database. using ASP.NET,C# & SQL server
Posted
Updated 12-Aug-16 1:09am
v2
Comments
F-ES Sitecore 12-Aug-16 6:50am    
What line throws the error?
Member 12682896 12-Aug-16 7:49am    
on line cmd.executenonquery

Hello ,
Just pass proper value for proper parameters .
C#
SqlParameter paramSize = new SqlParameter()
                        {
                            ParameterName = "@size",
                            Value = fileName
                        };


Here you are declaring filesize paramter and assigning the fileName .Thats why error comes .
Change above code and try below one
C#
SqlParameter paramSize = new SqlParameter()
                        {
                            ParameterName = "@size",
                            Value = fileSize; //change here
                        };

and also change the Image Data

C#
SqlParameter paramImageData = new SqlParameter()
                      {
                          ParameterName = "@ImageData",
                          Value = bytes ;
                      };

Thanks
 
Share this answer
 
You are setting the value of 3 of your parameters to fileName, including the size variable. The error is telling you that you are trying to convert a string to an int and something is in that string that is not an int.

Fix your parameters and then it will work fine.
 
Share this answer
 
Comments
Member 12682896 12-Aug-16 7:50am    
actually . i have written that code by seeing the video. in video his code work but mine don't
ZurdoDev 12-Aug-16 7:53am    
Because of what I told you and because of what the error told you. The error is not lying.

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