Dear all
In my first page i am inserting values (name,city and image) into sql database , and second page fatch all those values from database and display it ,
but my datalist will not display the data . can anybody tell me how can datalist show data from database. I have use two label and one image control into datalist
default.aspx page:
Name:
City:
Photo:
Text="Please insert your Image" Visible="False">
Width="125px" />
Default.aspx.cs page:
public partial class _Default : System.Web.UI.Page
{
SqlConnection con = new SqlConnection("Data Source=NITIN;Initial Catalog=test;Integrated Security=True");
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
if (FileUpload1.FileName == "")
{
Label1.Visible = true;
}
else
{
string fpath = "~\\Upload\\" + FileUpload1.FileName;
FileUpload1.PostedFile.SaveAs(Server.MapPath(fpath));
string query = "insert into fst values('" + TextBox1.Text + "','" + TextBox2.Text + "','" + fpath + "')";
SqlDataAdapter da = new SqlDataAdapter(query, con);
DataSet ds = new DataSet();
da.Fill(ds);
Response.Write("Values Inserted");
DisplayNextPage();
}
}
public void DisplayNextPage()
{
con.Open();
SqlCommand cmd = new SqlCommand("select*from fst", con);
SqlDataReader dr = cmd.ExecuteReader();
while (dr.Read())
{
if (TextBox1.Text == dr[0].ToString())
{
Session["name"] = dr[0].ToString();
Session["city"] = dr[1].ToString();
Session["img"] = dr[2].ToString();
Response.Redirect("Default2.aspx");
}
}
con.Close();
}
}
default2.aspx page
Welcome
Your City is
Data Bound Control
default2.aspx.cs
public partial class Default2 : System.Web.UI.Page
{
SqlConnection con = new SqlConnection("Data Source=NITIN;Initial Catalog=test;Integrated Security=True");
protected void Page_Load(object sender, EventArgs e)
{
Label1.Text = Session["name"].ToString();
Label2.Text = Session["city"].ToString();
Image1.ImageUrl = Session["img"].ToString();
display();
}
public void display()
{
SqlDataAdapter da = new SqlDataAdapter("Select * from fst", con);
DataSet ds = new DataSet();
da.Fill(ds);
DataList1.DataSource = ds;
DataList1.DataBind();
}
}
My Sql data query
create database test
use test
create table fst (sname varchar(50),city varchar(50),snap varchar(100))
select*from fst
4 Replies
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.
sudipta sanyalPosted Apr 24, 2014, 3:38 AM
'<%# Eval("sname") %>' this sname is your table column
Nitin SharmaPosted Apr 24, 2014, 6:27 AM
Nitin SharmaPosted Apr 24, 2014, 2:12 AM
sudipta sanyalPosted Apr 24, 2014, 1:34 AM
http://www.codeproject.com/Questions/419906/how-to-bind-datalist-in-asp-net-csharp-to-show-ima
http://www.dotnetfox.com/articles/how-to-display-images-in-datalist-control-using-Asp-Net-with-C-Sharp-1037.aspx
your code is perfect. but u miss one portion, u have to define Text='<%# Eval("name") %>'