Nitin Sharma

Nitin Sharma

  • NA
  • 154
  • 61.7k

How can i display Sql stored data into datalist

Apr 23 2014 7:01 AM
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">
<div>
&nbsp;&nbsp;Name:
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<br />
&nbsp; City:&nbsp;&nbsp;&nbsp;
<asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
<br />
&nbsp; Photo:
<asp:FileUpload ID="FileUpload1" runat="server" />
<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
<asp:Label ID="Label1" runat="server" ForeColor="Red"
Text="Please insert your Image" Visible="False"></asp:Label>
<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
<asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Save"
Width="125px" />
&nbsp;&nbsp;&nbsp;
<br />
</div>
</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">
<div>
Welcome
<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
&nbsp;<br />
Your City is
<asp:Label ID="Label2" runat="server" Text="Label"></asp:Label>
<br />
<br />
<asp:Image ID="Image1" runat="server" Height="121px" Width="108px" />
<br />
<br />
Data Bound Control<br />
<asp:DataList ID="DataList1" runat="server" Width="280px">
<ItemTemplate>
<asp:Label ID="Label3" runat="server" Text="Label"></asp:Label>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
<asp:Label ID="Label4" runat="server" Text="Label"></asp:Label>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
<asp:Image ID="Image2" runat="server" Height="70px" Width="54px" />
<br />
<br />
</ItemTemplate>
</asp:DataList>
<br />
</div>
</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
 
 
 
 
 

Answers (4)