Click here to Skip to main content
15,890,512 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
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:
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">

  Name:
<asp:TextBox ID="TextBox1" runat="server">


  City:   
<asp:TextBox ID="TextBox2" runat="server">


  Photo:
<asp:FileUpload ID="FileUpload1" runat="server" />


               
<asp:Label ID="Label1" runat="server" ForeColor="Red"
Text="Please insert your Image" Visible="False">


            
<asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Save"
Width="125px" />
   



</form>
</body>
</html>

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
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">

Welcome
<asp:Label ID="Label1" runat="server" Text="Label">
 

Your City is
<asp:Label ID="Label2" runat="server" Text="Label">




<asp:Image ID="Image1" runat="server" Height="121px" Width="108px" />




Data Bound Control

<asp:DataList ID="DataList1" runat="server" Width="280px">
<itemtemplate>
<asp:Label ID="Label3" runat="server" Text="Label">
       
<asp:Label ID="Label4" runat="server" Text="Label">
       
<asp:Image ID="Image2" runat="server" Height="70px" Width="54px" />









</form>
</body>
</html>

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
Posted
Comments
Sunasara Imdadhusen 23-Apr-14 7:22am    
So using this code what is your problem? please do not post entire code it will reduce chances to get solutions from the community.

i think you need to use Eval function on the html part in your code to bind datalist with your data. use something like below


ASP.NET
<asp:label id="Label3" runat="server" text="<%#Eval("ColumnName")%>" xmlns:asp="#unknown">


</asp:label>
 
Share this answer
 
Comments
Member 10273293 23-Apr-14 7:37am    
i have try this but this code will not display image in datalist
ravikhoda 23-Apr-14 7:39am    
for image you can again bind the img with src / imageurl tag.

<asp:Image ID="Image2" runat="server" Height="70px" Width="54px" ImageUrl='<%#Eval("ColumnName")%>' />

append the path of the image with above .
Member 10273293 23-Apr-14 7:43am    
show the error "server tage not in well formatted"
and my code looking like this
Data Bound Control<br />
<asp:DataList ID="DataList1" runat="server" Width="280px">
<itemtemplate>
<asp:Label ID="Label3" runat="server" Text="<%#Eval("sname")%>">
       
<asp:Label ID="Label4" runat="server" Text="<%#Eval("city")%>">
       
<asp:Image ID="Image2" runat="server" Height="70px" Width="54px" ImageUrl='<%#Eval("snap")%>' />

<br />
<br />

ravikhoda 23-Apr-14 8:00am    
src="Admin/images/<%#Eval ("Image") %>" this is what i used one of my code. where my images are placed inside the admin/images folder. try this and let me know if works.
Member 10273293 24-Apr-14 2:12am    
I don't understand the code can you please tell me how to modify this code , this is my datalist source code and sql query
<asp:DataList ID="DataList1" runat="server" Width="280px">
<itemtemplate>
<asp:Label ID="Label3" runat="server" Text="Label">
<asp:Label ID="Label4" runat="server" Text="Label">
<asp:Image ID="Image2" runat="server" Height="70px" Width="54px" />



SQL Query:
create database test
use test
create table fst (sname varchar(50),city varchar(50),snap varchar(100))
Now please tell me where to edit the code so that datalist display all value from sql
you need to define itemtemplate for your datalist and then bind the data.

Here are some sampels for reference.

http://www.webcodeexpert.com/2013/06/how-to-bind-datalist-using.html#.U1efpOmKDcs[^]

http://msdn.microsoft.com/en-us/library/aa719635(v=vs.71).aspx[^]
 
Share this answer
 
XML
no error
i have solve it Sir the correct code is
<asp:Label ID="Label3" runat="server" Text='<%# Eval("name") %>' ></asp:Label>
<asp:Label ID="Label4" runat="server" Text='<%# Eval("city") %>'></asp:Label>
<asp:Image ID="Image2" runat="server" ImageUrl='<%# Eval("snap") %>' Height="70px" Width="54px" />
Your advice help me a lot , thank you Sir
 
Share this answer
 
I don't understand the code can you please tell me how to modify this code , this is my datalist source code and sql query
<asp:datalist id="DataList1" runat="server" width="280px" xmlns:asp="#unknown">
<itemtemplate>
<asp:label id="Label3" runat="server" text="Label">
<asp:label id="Label4" runat="server" text="Label">
<asp:image id="Image2" runat="server" height="70px" width="54px">



SQL Query:
create database test
use test
create table fst (sname varchar(50),city varchar(50),snap varchar(100))
Now please tell me where to edit the code so that datalist display all value from sql
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900