Sir, I am doing a DataList Control program in ASP.NET
My Task is to retrieve image and name from database and display using DataList.
I did the function: uploading, saving.
But my DataList is not visible in runtime. I want your help in this.
now i want to view images in DataList Control.
here the code I using to Load Image to DataList(dlImages).
public void GalleryView()
{
if (sqlCon.State == ConnectionState.Closed)
{
sqlCon.Open();
}
SqlCommand cmd = new SqlCommand("imageViewAll", sqlCon);
SqlDataAdapter sqlda = new SqlDataAdapter(cmd);
DataTable dtbl = new DataTable();
dlImages.DataSource = dtbl;
dlImages.DataBind();
dlImages.Attributes.Add("bordercolor", "black");
}
And called it in page_Load()
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
GalleryView();
}
}
Loading

Satyapriya NayakPosted Aug 28, 2013, 4:50 AM
(
@name varchar(50),
@pic varchar(50)
)
AS
Insert into test values (@name,@pic)
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="ADD_Retrieve_IMAGES_datalist._Default" %>
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_Retrieve_IMAGES_datalist
{
public partial class _Default : 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;
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_photo", con);
com.CommandType = CommandType.StoredProcedure;
com.Connection = con;
com.Parameters.AddWithValue("@name", txt_name.Text);
com.Parameters.AddWithValue("@pic", s1);
com.ExecuteNonQuery();
com.Dispose();
binddatalist();
con.Close();
clear();
}
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
binddatalist();
}
}
private void clear()
{
txt_name.Text = "";
}
private void binddatalist()
{
SqlConnection con = new SqlConnection(strConnString);
con.Open();
sqlda = new SqlDataAdapter("SELECT * FROM test ", con);
dt = new DataTable();
sqlda.Fill(dt);
sqlda.Dispose();
DataList1.DataSource = dt;
DataList1.DataBind();
con.Close();
}
}
}
Iftikar HussainPosted Aug 28, 2013, 3:57 AM
but why you are passing datalist to the second parameter
Sanjeeb LenkaPosted Aug 28, 2013, 3:39 AM
check this line:
cmd.Parameters.Add("@employeename",txtEmployeeName.Text)
Bineesh ViswanathPosted Aug 28, 2013, 3:32 AM
style="border:1px solid gray;" Height="136px" Width="158px" />
Iftikar HussainPosted Aug 28, 2013, 3:26 AM
Bineesh ViswanathPosted Aug 28, 2013, 3:23 AM
Iftikar HussainPosted Aug 28, 2013, 3:05 AM
Modify the below code like this
public void GalleryView()
{
if (sqlCon.State == ConnectionState.Closed)
{
sqlCon.Open();
}
SqlCommand cmd = new SqlCommand("imageViewAll", sqlCon);
SqlDataAdapter sqlda = new SqlDataAdapter(cmd);
DataTable dtbl = new DataTable();
sqlda .Fill(dtbl);
dlImages.DataSource = dtbl;
dlImages.DataBind();
dlImages.Attributes.Add("bordercolor", "black");
}