Click here to Skip to main content
15,891,033 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
Line 27:         if (FileUpload1.HasFile)<br />
Line 28:             savepath = Path.Combine(Request.PhysicalApplicationPath, imagefoldr);<br />
Line 29:         savefile = Path.Combine(savefile, FileUpload1.FileName);<br />
Line 30:         FileUpload1.SaveAs(savefile);<br />
Line 31:         SqlDataSource1.Insert();



protected void Button1_Click(object sender, EventArgs e)
    {
        SqlConnection con = new SqlConnection("server=USER\\SQLEXPRESS;user id=;password=database=images");
      string imagefoldr = "images";
        string savepath;
        string savefile;
        if (FileUpload1.HasFile)
            savepath = Path.Combine(Request.PhysicalApplicationPath, imagefoldr);
        savefile = Path.Combine(savefile, FileUpload1.FileName);
        FileUpload1.SaveAs(savefile);
        SqlDataSource1.Insert();

    }
Posted
Updated 3-Feb-12 0:17am
v2
Comments
Varun Sareen 3-Feb-12 6:18am    
Dear Friend,

I think nothing is write in your code, the connection string is wrong, the data insertion into the database is wrong and please provide the error text then we can help you out.

Thanks
bbirajdar 3-Feb-12 6:19am    
You have not mentioned the error . Do you want us to guess the error first ?
rockpune 3-Feb-12 6:21am    
Error 1 Use of unassigned local variable 'savefile' C:\Documents and Settings\Administrator\My Documents\Visual Studio 2010\WebSites\user\Default2.aspx.cs 31 33 C:\...\user\
rockpune 3-Feb-12 6:23am    
Error 1 Use of unassigned local variable 'savefile' C:\Documents and Settings\Administrator\My Documents\Visual Studio 2010\WebSites\user\Default2.aspx.cs 31 33 C:\...\user\

C#
if (FileUpload1.HasFile)
{

       SqlConnection con = new SqlConnection("server=USER\\SQLEXPRESS;user id=;password=database=images");
       string imagefoldr = "images";
        string savepath;
        string savefile;
            savepath = Path.Combine(Request.PhysicalApplicationPath, imagefoldr);
        savefile = Path.Combine(savefile, FileUpload1.FileName);
        FileUpload1.SaveAs(savefile);
        SqlDataSource1.Insert();
}


I think this could be the solution. But you still get some error please provide us the Exception message and Line number
 
Share this answer
 
Comments
rockpune 3-Feb-12 6:21am    
Error 1 Use of unassigned local variable 'savefile' C:\Documents and Settings\Administrator\My Documents\Visual Studio 2010\WebSites\user\Default2.aspx.cs 31 33 C:\...\user\
[no name] 3-Feb-12 6:23am    
Use

string savefile= string.Empty;

this will work
rockpune 3-Feb-12 6:25am    
than u brother
[no name] 3-Feb-12 6:27am    
welcome
rockpune 3-Feb-12 6:29am    
this exception come

The SaveAs method is configured to require a rooted path, and the path 'Winter.jpg' is not rooted.
You need to change to this

C#
if (FileUpload1.HasFile)
{
            savepath = Path.Combine(Request.PhysicalApplicationPath, imagefoldr);
        savefile = Path.Combine(savepath , FileUpload1.FileName);
FileUpload1.SaveAs(savefile);
}


Note the use of 'savepath' and 'savefile' in this line of code

C#
savefile = Path.Combine(savepath , FileUpload1.FileName);
 
Share this answer
 
v2
Comments
rockpune 3-Feb-12 6:33am    
Cannot insert the value NULL into column 'pid', table 'images.dbo.gallery'; column does not allow nulls. INSERT fails.
The statement has been terminated.

Line 35: }
Line 36:
Line 37: SqlDataSource1.Insert();
Line 38:
Line 39: }
bbirajdar 3-Feb-12 6:37am    
You have not written any code to insert. You are just calling the .Insert() without any parameters and values.. What are you trying to insert?

Follow this link http://www.codeproject.com/Articles/18165/Using-the-Insert-feature-of-SqlDataSource-with-Gri

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