Hai,,
im uploading and displaying images using datalist,so how to save these images path to database in asp.net???
Loading
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
Satyapriya NayakRank #47 · 31.5K reputationPosted Feb 14, 2013, 5:19 AM EST
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;
using System.IO;
namespace Add_update_delete_gridview
{
public partial class WebForm1 : System.Web.UI.Page
{
string strConnString = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
string s1;
string path;
SqlConnection cnn = new SqlConnection();
SqlCommand com = new SqlCommand();
SqlDataAdapter sqlda;
DataTable dt;
int id;
string name;
string address;
string image;
protected void btn_insert_Click(object sender, EventArgs e)
{
if (FileUpload1.PostedFile.ContentLength > 0)
{
s1 = Path.GetFileName(FileUpload1.FileName);
path = Server.MapPath("images") + "/" + s1;
FileUpload1.SaveAs(path);
}
SqlConnection con = new SqlConnection(strConnString);
con.Open();
SqlCommand com = new SqlCommand("insert_employee", con);
com.CommandType = CommandType.StoredProcedure;
com.Connection = con;
com.Parameters.AddWithValue("@UserName", txt_name.Text);
com.Parameters.AddWithValue("@Password", txt_password.Text);
com.Parameters.AddWithValue("@address", txt_address.Text);
com.Parameters.AddWithValue("@image", s1);
com.ExecuteNonQuery();
com.Dispose();
bindgrid();
con.Close();
clear();
}
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
bindgrid();
}
}
private void clear()
{
txt_name.Text = "";
txt_password.Text = "";
txt_address.Text = "";
}
private void bindgrid()
{
SqlConnection con = new SqlConnection(strConnString);
con.Open();
sqlda = new SqlDataAdapter("SELECT * FROM employee ", con);
dt = new DataTable();
sqlda.Fill(dt);
sqlda.Dispose();
DataList1.DataSource = dt;
DataList1.DataBind();
con.Close();
}
}
}
Pradip PandeyRank #452 · 5.6K reputationPosted Feb 14, 2013, 5:35 PM EST
Refer these articles to get more idea about uploading & displaying images. In this example, gridview is used but you can also datalist with few changes.
http://www.c-sharpcorner.com/UploadFile/225bcd/how-to-upload-images-to-databases-or-how-to-store-images-to-database/
http://www.c-sharpcorner.com/UploadFile/225bcd/how-to-retrieve-images-from-database-in-layer-architecture/
Hope it will help.
divya146 reputationPosted Feb 14, 2013, 8:54 PM EST