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.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
Satyapriya NayakPosted Feb 14, 2013, 10:19 AM
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();
}
}
}
divyaPosted Feb 15, 2013, 1:54 AM
Pradip PandeyPosted Feb 14, 2013, 10:35 PM
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.