Click here to Skip to main content
15,886,579 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
why path is not passed while variables have correct values stored in them?
how to solve it?

C#
string FilePath;
protected void BtnUpload_Click(object sender, EventArgs e)
        {
            if (FileUpload1.PostedFile != null)
            {
                File_Name = Path.GetFileName(FileUpload1.PostedFile.FileName);
                FileExt = System.IO.Path.GetExtension(FileUpload1.FileName);
                Response.Write(File_Name);
                ss = Server.MapPath("~/Images/");
                FilePath = ss + File_Name;
                
                

                if (FileExt.ToLower() == ".jpg" || FileExt.ToLower() == ".jpeg" || FileExt.ToLower() == ".png" || FileExt.ToLower() == ".gif")
                {
                    FileUpload1.SaveAs(ss + FileUpload1.FileName);
                    Image1.ImageUrl = "~/Images/"+ FileUpload1.FileName;
                }
                else
                {
                    Response.Write("Please Select an Image File..!");
                }
            }
protected void BtnSubmit_Click(object sender, EventArgs e)
        {
                SqlCommand cmd1 = new SqlCommand("Usp_UploadImage1", con);
                cmd1.CommandType = CommandType.StoredProcedure;
                cmd1.Parameters.Add(new SqlParameter("@User_Id", SqlDbType.VarChar, 100));
                cmd1.Parameters["@User_Id"].Value = id;
                cmd1.Parameters.Add(new SqlParameter("@Name", SqlDbType.VarChar, 100));
                cmd1.Parameters["@Name"].Value = TxtImageName.Text.Trim();
                cmd1.Parameters.Add(new SqlParameter("@Descrip", SqlDbType.VarChar, 100));
                cmd1.Parameters["@Descrip"].Value = TxtImageDescription.Text.Trim();
                cmd1.Parameters.Add(new SqlParameter("@Imagecategory_Id", SqlDbType.Int));
                cmd1.Parameters["@Imagecategory_Id"].Value = DrpImageCatagory.SelectedIndex;
                cmd1.Parameters.Add(new SqlParameter("@Date", SqlDbType.DateTime));
                cmd1.Parameters["@Date"].Value = System.DateTime.Now;
                cmd1.Parameters.Add(new SqlParameter("@Path", SqlDbType.VarChar,200));
                cmd1.Parameters["@Path"].Value = FilePath;
                cmd1.Parameters.Add(new SqlParameter("@Status", SqlDbType.Int));
                cmd1.Parameters["@Status"].Value = status;
                cmd1.Parameters.Add(new SqlParameter("@ImageType_Id", SqlDbType.Int));
                cmd1.Parameters["@ImageType_Id"].Value = RadioImageType.SelectedValue;

                int rowcount = cmd1.ExecuteNonQuery();
                con.Close();
                if (rowcount > 0)
                {
                LblStatus.Text = "Data Inserted Successful..!";
                }
       }


Database: (stored procedure)

SQL
Create proc Usp_UploadImage1(@User_Id varchar(100), @Name varchar(50), @Descrip varchar(100), @Imagecategory_Id int, @Date datetime, @Path varchar(max), @Status bit, @ImageType_Id int)
as begin
insert into ImageRepository values(@User_Id, @Name, @Descrip, @Imagecategory_Id, @Date, @Path, @Status, @ImageType_Id)
end



Every thing works fine. but execution gives stated error:
Procedure or function 'Usp_UploadImage1' expects parameter '@Path', which was not supplied

when i debug that, it shows that Filepath1 has correct path
but is was not passed..!! why? and how can i solve it?
Posted
Updated 8-Mar-15 21:21pm
v5
Comments
Can you simply try sending the parameter with some text, but not the file path?
Satpal Lakhera 8-Mar-15 3:45am    
i tried with simple sql query, this works but stores only till folder path but not filename with extension.
as
d:\projects\images\
or
~/Images/
That means the file path you are getting from code is not complete. Debug and see what is the file path it is showing in code.
Satpal Lakhera 8-Mar-15 12:56pm    
as i told you, at debugging it shows correct value(c:\images\tech.jpg)
but not stored in database correctly. why?
Subramanyam Shankar 7-Mar-15 10:09am    
I tried your code it did work fine. Please check once again.

I guess your code works fine as per your code. Now the second possibility is while inserting your data means insert query. Make sure your database schema has same fields in order i.e User_Id, Name, Descrip, Imagecategory_Id, Date, Path, Status, ImageType_Id (possibility of mismatching columns) else change your query by specifying table columns as follows:
SQL
insert into ImageRepository (User_Id, Name, Descrip, Imagecategory_Id, Date, Path, Status, ImageType_Id) values (@User_Id, @Name, @Descrip, @Imagecategory_Id, @Date, @Path, @Status, @ImageType_Id)
 
Share this answer
 
v2
Comments
Satpal Lakhera 8-Mar-15 3:23am    
Error : "Path " variable have Invalid type [SQL SERVER 2012]
Modi Mayank 8-Mar-15 3:24am    
can you post your table with schema here and update your question?
Satpal Lakhera 8-Mar-15 3:40am    
Create table ImageRepository
(Id int Identity(1,1) primary key,
User_Id varchar(100) foreign key references UserDetails(EmailId),
Name varchar(50),
Descrip varchar(Max),
Imagecategory_Id int foreign key references Imagecategory(Id),
Date datetime,
Path varchar(max),
Status bit,
ImageType_Id int foreign key references ImageType(Id)
)
Modi Mayank 8-Mar-15 3:57am    
Looks good, Did you debug code? Have you checked the what value comes in "Path"?
Modi Mayank 8-Mar-15 4:03am    
It may be because you declared varchar(max) datatype to @path in your Store Procedure and Database Schema and in your code you had limited to varchar 200 in line cmd1.Parameters.Add(new SqlParameter("@Path", SqlDbType.VarChar,200));. You can give it try by changing this. It might solve your issue, not sure yet.
Generally when you call click event, it'll resets all the values due to postbacks and thus such problem arise. So, try it without postbacks.

try storing and passing by use of following methods :

hiddenfields
session variable.
viewstate
or try without postback.

Solution using session variable -
in function hold path as:
Session["NewPath"] = Server.MapPath("~/images/") + fileupload1.filename;
Pass in database as:
FilePath = Session["NewPath"].toString(); then pass Filepath in Stored procedures.
 
Share this answer
 

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