Wednesday, 5 February 2014

Display record in gridview at the time of page load and download picture from gridview


Database Field


Table Name: tb_download 

id= int(auto increment and primary key) 
Name=varchar(50) 
Date=Datetime 
upload=varchar(50) 


Put this code in body tag

<asp:ScriptManager ID="sp" runat="server"></asp:ScriptManager> 
<table> 
<h1>Information</h1> 
<tr><td> 
&nbsp;&nbsp; 
Name:<asp:TextBox ID="txtname" runat="server"></asp:TextBox></td></tr> 
<tr><td> &nbsp;&nbsp;&nbsp; Date:<asp:TextBox ID="txtdate" runat="server"></asp:TextBox><cc1:CalendarExtender 
ID="CalendarExtender1" runat="server" TargetControlID="txtdate"> 
</cc1:CalendarExtender></td></tr> 
<tr><td>Upload:<asp:FileUpload ID="file" runat="server" /></td></tr> 
<tr><td align="center"><asp:Button ID="btn" runat="server" Text="Upload" 
onclick="btn_Click" /></td></tr> 
</table> 
<asp:GridView ID="gv" runat="server" AutoGenerateColumns="False" 
Height="188px" Width="300px" CellPadding="4" ForeColor="#333333" 
GridLines="None" > 
<AlternatingRowStyle BackColor="White" ForeColor="#284775" /> 
<Columns><asp:TemplateField HeaderText="Name"> 

<ItemTemplate> 

<%#Eval("Name") %> 
</ItemTemplate> 

</asp:TemplateField> 
<asp:TemplateField HeaderText="Date" > 
<ItemTemplate> 
<%#Eval("Date") %> 

</ItemTemplate> 
</asp:TemplateField> 
<asp:TemplateField HeaderText="Download"> 
<ItemTemplate> 
<a href='/upload/<%#Eval("Upload") %>'><%#Eval("Upload") %></a> 

</ItemTemplate> 
</asp:TemplateField> 
</Columns> 
<EditRowStyle BackColor="#999999" /> 
<FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" /> 
<HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" /> 
<PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" /> 
<RowStyle BackColor="#F7F6F3" ForeColor="#333333" /> 
<SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" /> 
<SortedAscendingCellStyle BackColor="#E9E7E2" /> 
<SortedAscendingHeaderStyle BackColor="#506C8C" /> 
<SortedDescendingCellStyle BackColor="#FFFDF8" /> 
<SortedDescendingHeaderStyle BackColor="#6F8DAE" /> 
</asp:GridView> 


</div> 

C# code

Note: Add upload folder in your project 

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Web; 
using System.Web.UI; 
using System.Web.UI.WebControls; 
using System.Data; 
using System.Data.SqlClient; 

namespace sandeep 

public partial class WebForm2 : System.Web.UI.Page 

SqlConnection con = new SqlConnection("server=SANDEEP-PC;database=Example;integrated security=true"); 

protected void Page_Load(object sender, EventArgs e) 

if (IsPostBack != true) 

BindData(); 


protected void btn_Click(object sender, EventArgs e) 



if (file.HasFile) 

String path = Server.MapPath("upload/"); 
String filename = file.FileName; 
file.SaveAs(path + filename); 
SqlCommand cmd = new SqlCommand("insert into tb_download values('" + txtname.Text + "','" + txtdate.Text + "','" +filename + "')", con); 
con.Open(); 
cmd.ExecuteNonQuery(); 
clear(); 
con.Close(); 
BindData(); 



protected void clear() 

txtdate.Text = ""; 
txtname.Text = ""; 


protected void BindData() 

SqlDataAdapter dap = new SqlDataAdapter("select * from tb_download ", con); 
DataSet ds = new DataSet(); 
dap.Fill(ds); 

gv.DataSource = ds; 
gv.DataBind(); 




}

No comments:

Post a Comment