Saving and retrieving images in gridview

Posted by Bhanubysani under Regular Expressions on 7/8/2011 | Points: 10 | Views : 5770 | Status : [Member] | Replies : 5
hi,

i have a requirement in my web page i have one text box for student name,for student image file upload control and one submit button..when i click the submit button the student images will be saved in local folder studentimages if the folder does not exist create the folder and save the images in that folder...

below the submit button i have one gridview in the gridview sno,student name,image will be dispalyed..plz help in writing the code..

Regards,
Bhanu Prakash Bysani



Responses

Posted by: Ndebata on: 7/8/2011 [Member] Starter | Points: 25

Up
0
Down
Hi
Take a look at this post.
http://www.dotnetfunda.com/articles/article180.aspx
Try to add one FileUpload control in a Template Column for gridview,
While Saving check if the directory is there or not , if not create it first as below (_DataLoaderDir is a string holding your dir name)
if (!Directory.Exists(Server.MapPath(_DataLoaderDir)))

Directory.CreateDirectory(Server.MapPath(_DataLoaderDir));

Then call the SaveAs method of fileupload control to save the image to this folder.





Bhanubysani, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Bhanubysani on: 7/8/2011 [Member] Starter | Points: 25

Up
0
Down
hi i write this code for inserting images..the image is adding successfully..my problem is image is not dispolying in gridview..my code is

aspx page:--
---------------
<div>
<asp:FileUpload ID="Fileupload1" runat="server"></asp:FileUpload><br />
<asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" />
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="sno">
<Columns>
<asp:BoundField DataField="sno" HeaderText="sno" InsertVisible="False" ReadOnly="True"
SortExpression="sno" />
<asp:TemplateField>
<ItemTemplate>
<asp:Image ID="img1" runat="server" ImageUrl='<%#Eval("image") %>' />
</ItemTemplate>
</asp:TemplateField>

</Columns>
<EmptyDataTemplate>
<asp:Label ID="lblMessage" runat="server" Text="There Is No Iamges"></asp:Label>
</EmptyDataTemplate>
</asp:GridView>
</div>

aspx.cs:--
------------
SqlConnection cn = new SqlConnection("Data Source=.;Initial Catalog=bhanu;Integrated Security=True");

protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
bindgrid();
}
}
protected void Button1_Click(object sender, EventArgs e)
{
string path = Server.MapPath("Images");
string image1 = Fileupload1.FileName;
if (Fileupload1.HasFile)
{
if (!Directory.Exists(path))
{
Directory.CreateDirectory(path);
Fileupload1.SaveAs(path + "/" + Fileupload1.FileName);

SqlCommand cmd = new SqlCommand("insert into sdetails(image) values ('" + image1 + "')", cn);
cn.Open();
int result = cmd.ExecuteNonQuery();
cn.Close();
if (result > 0)
AlertMessage("Image added Successfully");
else
AlertMessage("Some Problem Occured While Adding");

}
else
{
Fileupload1.SaveAs(path + "/" + Fileupload1.FileName);

SqlCommand cmd = new SqlCommand("insert into sdetails(image) values ('" + image1 + "')", cn);
cn.Open();
int result = cmd.ExecuteNonQuery();
if (result > 0)
AlertMessage("Image added Successfully");
else
AlertMessage("Some Problem Occured While Adding");
}

}
else
{
AlertMessage("please Select image");
}
}
public void AlertMessage(string resMsg)
{
System.Text.StringBuilder sb = new System.Text.StringBuilder();
sb.Append("<script type = 'text/javascript'>");
sb.Append("window.onload=function(){");
sb.Append("alert('");
sb.Append(resMsg);
sb.Append("')};");
sb.Append("</script>");
Page.RegisterClientScriptBlock("send_mailss", sb.ToString());
}
public void bindgrid()
{
cn.Open();
SqlDataAdapter da = new SqlDataAdapter("select * from sdetails", cn);
DataSet ds = new DataSet();
da.Fill(ds);
cn.Close();
GridView1.DataSource = ds;
GridView1.DataBind();
}


Regards,
Bhanu Prakash Bysani

Bhanubysani, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Susanthampy on: 7/9/2011 [Member] [MVP] Bronze | Points: 25

Up
0
Down
Also refer this link,

http://www.dotnetcurry.com/ShowArticle.aspx?ID=129

Regards,
Susan

Bhanubysani, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Vforvijay on: 7/9/2011 [Member] Starter | Points: 25

Up
0
Down
hi..

Try this link ...
http://vbdotnetaddict.blogspot.com/2011/06/how-to-store-imageurl-in-database-and.html

Bhanubysani, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response