Click here to Skip to main content
15,918,109 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi,

can you send me a code for retrieving an uploading image from sql using c#.net?
Posted
Updated 18-Jan-11 21:57pm
v2
Comments
R. Giskard Reventlov 19-Jan-11 3:40am    
No: what have you already tried? Search through Code Project and then Google: don't expect other people to just hand over code when you have done nothing to help yourself.

THIS[^] would help you quick.
 
Share this answer
 
Comments
Manfred Rudolf Bihy 19-Jan-11 16:03pm    
Somebody doesn't like you today it seems! 5+
 
Share this answer
 
make a folder in your application named as Album
on button click write this query

C#
if (FileUpload1.HasFile)
             {
                 string SFileName = Path.GetFileName(FileUpload1.PostedFile.FileName);
                 int fileAppent = 0;
                 while (File.Exists(Server.MapPath("~/Album/" + SFileName)))
                 {
                     fileAppent++;
                     SFileName = Path.GetFileNameWithoutExtension(FileUpload1.PostedFile.FileName)
                         + fileAppent.ToString() + Path.GetExtension(FileUpload1.PostedFile.FileName).ToLower();
                 }
                 path = Server.MapPath("~/Album/" + SFileName);
                 FileUpload1.PostedFile.SaveAs(path);
             string    ab = "~/Album/" + SFileName;


Sqlcommand cmd=new Sqlcommand ("insert into tablename (image)values(ab)",con);
con.Open();
cmd.ExecutenonQuety();
con.Close();


to reterive take datalist or gridview

I have shown in datalist

XML
<asp:DataList ID="DataLstEmployee" Width="100%" runat="server"
            Font-Names="Verdana" Font-Size="Small" ForeColor="#000099">
        <ItemTemplate>
<asp:Image ID="Image1" Width="75%" Height="75%" runat="server" ImageUrl='<%# DataBinder.Eval(Container.DataItem, "image")%>'/>
</ItemTemplate>
</asp:DataList>


and in Gridview

XML
<asp:GridView ID="GridView1" Runat="server"
  DataSource='<%# GetData() %>' AutoGenerateColumns="False"
  BorderWidth="1px" BackColor="White" CellPadding="3" BorderStyle="None"
  BorderColor="#CCCCCC" Font-Names="Arial">
    <FooterStyle ForeColor="#000066" BackColor="White"></FooterStyle>
    <PagerStyle ForeColor="#000066" HorizontalAlign="Left"
      BackColor="White"></PagerStyle>
    <HeaderStyle ForeColor="White" Font-Bold="True"
      BackColor="#006699"></HeaderStyle>
    <Columns>

        <asp:ImageField  DataImageUrlField="image"></asp:ImageField>
    </Columns>
    <SelectedRowStyle ForeColor="White" Font-Bold="True"
       BackColor="#669999"></SelectedRowStyle>
    <RowStyle ForeColor="#000066"></RowStyle>
</asp:GridView>




and bind this datalist from code behind
 
Share this answer
 
v3
Comments
sampath1750 19-Jan-11 4:13am    
How can i use above code in Data Grid view
Mohd Wasif 19-Jan-11 4:30am    
or go to this link

http://www.codeproject.com/KB/aspnet/GridImage.aspx

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