In previous post i explained how to save image in folder and image path in database now i will explain how to retrieve the image from database and display it in grid view.
create table in database:
CREATE TABLE [dbo].[photoalbum](
[Id] [int] IDENTITY(1,1) NOT NULL,
[Name] [nvarchar](50) NULL,
[Images] [nvarchar](max) NULL
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]
GO
In .aspx page:
<body>
<form id="form1" runat="server">
<div align="center">
<h2 align="center">Upload and Save</h2>
<table align="center"><tr><td>
<asp:Label ID="Label1" runat="server" Text="Name"></asp:Label></td>
<td><asp:TextBox ID="TXT_NAME" runat="server"></asp:TextBox></td></tr>
<tr><td>Image:</td><td><asp:FileUpload ID="fileupload1" runat="server" /></td></tr>
<tr><td colspan="2" align="center">
<asp:Button ID="Button1" runat="server" Text="Button" onclick="Button1_Click" /></td></tr>
<asp:Label ID="lbl_text" runat="server"></asp:Label>
</table>
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" >
<Columns>
<asp:BoundField DataField="Id" HeaderText="Id" />
<asp:BoundField DataField="Name" HeaderText="ImageName" />
<asp:ImageField DataImageUrlField="Images" HeaderText="Images">
</asp:ImageField>
</Columns>
</asp:GridView>
</div>
</form>
</body>
In .aspx.cs page:
Add Namespaces :
using system.io;
using system.data;
using system.data.sqlclient;
using system.configuration;
public partial class TouploadandView : System.Web.UI.Page
{
string strcon=ConfigurationManager.ConnectionStrings["sqlconn"].ConnectionString;
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
}
}
public void getdata()
{
SqlConnection con = new SqlConnection(strcon);
SqlCommand cmd = new SqlCommand("select * from photoalbum", con);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds);
GridView1.DataSource = ds;
GridView1.DataBind();
}
protected void Button1_Click(object sender, EventArgs e)
{
try
{
SqlConnection con=new SqlConnection(strcon);
if (fileupload1.HasFile)
{
string path = "~/Images/" + fileupload1.FileName;
fileupload1.SaveAs(Server.MapPath(path));
string getfile=path;
SqlCommand cmd = new SqlCommand("sp_insert", con);
cmd.CommandType=CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@Name",TXT_NAME.Text.Trim());
cmd.Parameters.AddWithValue("@Images",getfile);
con.Open();
cmd.ExecuteNonQuery();
lbl_text.Text="Image upload successfull";
getdata();
con.Close();
}
}
catch(Exception ex)
{
lbl_text.Text=ex.Message;
}
}
}
}
Web config file:
To set your database connection in web.config like this because we are using this connection in our datasource to get the data from database.
<configuration>
<connectionStrings>
<add name="sqlconn" connectionString="Data Source=SYSTEM-05-PC;Initial Catalog=asw;Integrated Security=True"/>
</connectionStrings>
i hope you understand this article
create table in database:
CREATE TABLE [dbo].[photoalbum](
[Id] [int] IDENTITY(1,1) NOT NULL,
[Name] [nvarchar](50) NULL,
[Images] [nvarchar](max) NULL
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]
GO
In .aspx page:
<body>
<form id="form1" runat="server">
<div align="center">
<h2 align="center">Upload and Save</h2>
<table align="center"><tr><td>
<asp:Label ID="Label1" runat="server" Text="Name"></asp:Label></td>
<td><asp:TextBox ID="TXT_NAME" runat="server"></asp:TextBox></td></tr>
<tr><td>Image:</td><td><asp:FileUpload ID="fileupload1" runat="server" /></td></tr>
<tr><td colspan="2" align="center">
<asp:Button ID="Button1" runat="server" Text="Button" onclick="Button1_Click" /></td></tr>
<asp:Label ID="lbl_text" runat="server"></asp:Label>
</table>
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" >
<Columns>
<asp:BoundField DataField="Id" HeaderText="Id" />
<asp:BoundField DataField="Name" HeaderText="ImageName" />
<asp:ImageField DataImageUrlField="Images" HeaderText="Images">
</asp:ImageField>
</Columns>
</asp:GridView>
</div>
</form>
</body>
In .aspx.cs page:
Add Namespaces :
using system.io;
using system.data;
using system.data.sqlclient;
using system.configuration;
public partial class TouploadandView : System.Web.UI.Page
{
string strcon=ConfigurationManager.ConnectionStrings["sqlconn"].ConnectionString;
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
}
}
public void getdata()
{
SqlConnection con = new SqlConnection(strcon);
SqlCommand cmd = new SqlCommand("select * from photoalbum", con);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds);
GridView1.DataSource = ds;
GridView1.DataBind();
}
protected void Button1_Click(object sender, EventArgs e)
{
try
{
SqlConnection con=new SqlConnection(strcon);
if (fileupload1.HasFile)
{
string path = "~/Images/" + fileupload1.FileName;
fileupload1.SaveAs(Server.MapPath(path));
string getfile=path;
SqlCommand cmd = new SqlCommand("sp_insert", con);
cmd.CommandType=CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@Name",TXT_NAME.Text.Trim());
cmd.Parameters.AddWithValue("@Images",getfile);
con.Open();
cmd.ExecuteNonQuery();
lbl_text.Text="Image upload successfull";
getdata();
con.Close();
}
}
catch(Exception ex)
{
lbl_text.Text=ex.Message;
}
}
}
}
Web config file:
To set your database connection in web.config like this because we are using this connection in our datasource to get the data from database.
<configuration>
<connectionStrings>
<add name="sqlconn" connectionString="Data Source=SYSTEM-05-PC;Initial Catalog=asw;Integrated Security=True"/>
</connectionStrings>
i hope you understand this article
No comments:
Post a Comment