This is default featured slide 4 title

Go to Blogger edit html and find these sentences.Now replace these sentences with your own descriptions.

This is default featured slide 1 title

Go to Blogger edit html and find these sentences.Now replace these sentences with your own descriptions.

This is default featured slide 2 title

Go to Blogger edit html and find these sentences.Now replace these sentences with your own descriptions.

This is default featured slide 4 title

Go to Blogger edit html and find these sentences.Now replace these sentences with your own descriptions.

Me And My Respected Teacher Mr Kamal Sheel Mishra

Mr. K.S. Mishra is HOD of Computer Science from SMS Varanasi where I have completed my MCA

Me And My Respected Teacher Mr Udayan Maiti

Mr. Udayan Maiti is a senior .Net Expert and has guided many professionals of multi national Companies(MNC)

Me And My Best Friend Mr Ravinder Goel

Mr. Ravinder Goel is a senior Software Engineer and now he is working Wipro Technology

Tuesday, 23 September 2014

How to displaying Images in GridView with ImageFiled

How to displaying Images in GridView with ImageFiled

ImageFiled is designed for the purpose of displaying images in databound controls like GridView and DetailsView so it is better to use ImageField over Image Control with Template Field

Properties with ImageField in GridView

·         DataImageUrlField
·         DataImageUrlFormatString
·         AlternateText
·         DataAlternateTextField
·         NullDisplayText
·         NullImageUrl
·         ControlStyle-Width
·         ControlStyle-Height

AlternateText  this property to specify the text that you want to display if image is missing
DataAlternateTextField  this property to specify the data field from Sql table  that you want to display if image is missing
DataImageUrlField this property to specify the data field name of Sql table like
DataImageUrlField="Photo"
DataImageUrlFormatString this property to specify the Folder  name of Web Application where images will be saved like
DataImageUrlFormatString="/Image/{0}"

--Sql Query
create database GridConcept
use GridConcept
create table tb_image1
(
SrNo int identity(1,1) primary key,
Name varchar(50),
Photo varchar(max)
 )

Pictorial Representation with ImageField Properties in GridView



Source code with WebForm2.aspx

%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm2.aspx.cs" Inherits="ImageFieldwithGridView.WebForm2" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<table width="100%">
<tr><td  align="center">
<span color="Red"> <b><i>How to displaying Image  in GridView with ImageField</i></b> </span>   </td></tr>
<tr>
<td align="center">

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
DataKeyNames="SrNo" DataSourceID="SqlDataSource1">
<Columns>
<asp:BoundField DataField="SrNo" HeaderText="SrNo" SortExpression="SrNo" />
<asp:BoundField DataField="Name" HeaderText="Name" SortExpression="Name" />

<asp:ImageField DataImageUrlField="Photo" DataImageUrlFormatString="/Image/{0}" ControlStyle-Width="100px" ControlStyle-Height="100px"
HeaderText="Photo" NullDisplayText="Not Found"
NullImageUrl="~/Image/notfoundimage.jpg">
</asp:ImageField>

</Columns>
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:GridConceptConnectionString %>"
SelectCommand="SELECT * FROM [tb_image]"></asp:SqlDataSource>
</td></tr>
</table>

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

Result: 



How to displaying Images in GridView with ImageFiled with C# Code

First  we create  class which name is StudentDataAceessLayer.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Configuration;
using System.Data.SqlClient;

namespace ImageFieldwithGridView
{
public class Student
{
public int RollNo { get; set; }
public string Name { get; set; }
public string Photo { get; set; }
// public string AlternateText { get; set; }
public string DataAlternateText { get; set; }

}
public class StudentDataAceessLayer
{

public static List<Student> GetAllData()
{
List<Student> liststu = new List<Student>();
string constr = ConfigurationManager.ConnectionStrings["kush"].ConnectionString;
using (SqlConnection con = new SqlConnection(constr))
{
SqlCommand cmd = new SqlCommand("select * from tb_image", con);
con.Open();
SqlDataReader dr = cmd.ExecuteReader();
while (dr.Read())
{
Student ss = new Student();
ss.RollNo = Convert.ToInt32(dr["SrNo"]);
ss.Name = dr["Name"].ToString();
ss.Photo = dr["Photo"].ToString();
// ss.AlternateText = "Not found";
ss.DataAlternateText = dr["Name"]+" image not found";

liststu.Add(ss);
}
return liststu;
}
}
}
}

Source code with WebForm1.aspx

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="ImageFieldwithGridView.WebForm1" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<style type="text/css">
.style1
{
width: 100%;
}
</style>
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<table class="style1">
<tr>
<td colspan="2" align="center">
<span color="Red"> <b><i>How to displaying Image  in GridView with ImageField</i></b> </span>   </td>
</tr>
<tr><td>Name</td>
<td><asp:TextBox ID="txtname" runat="server"></asp:TextBox></td></tr>
<tr>
<td>
Photo</td>
<td>
<asp:FileUpload ID="FileUpload1" runat="server" />
</td>
</tr>
<tr>
<td>
&nbsp;</td>
<td>
<asp:Button ID="btnSave" runat="server" Text="Save" onclick="btnSave_Click" />
<asp:Label ID="lblmessage" runat="server" Text="Label" Visible="False"></asp:Label>
</td>
</tr>
<tr>
<td colspan="2" align="center">
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False">
<Columns>
<asp:BoundField DataField="RollNo" HeaderText="RollNo"
SortExpression="RollNo" />
<asp:BoundField DataField="Name" HeaderText="Name" SortExpression="Name" />
<asp:ImageField DataImageUrlField="Photo" DataImageUrlFormatString="~/Image/{0}" ControlStyle-Width="150px" ControlStyle-Height="100px" DataAlternateTextField="DataAlternateText"
HeaderText="Student Image">
</asp:ImageField>
</Columns>
</asp:GridView>
</td>
</tr>
</table>
</div>
</form>
</body>
</html>

Source code with WebForm1.aspx.cs

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

namespace ImageFieldwithGridView
{
public partial class WebForm1 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{

BindGridView();

}
//code for retrieve image .................
private void BindGridView()
{
GridView1.DataSource = StudentDataAceessLayer.GetAllData();
GridView1.DataBind();
}

//code for save image .................
string fileimg;
protected void btnSave_Click(object sender, EventArgs e)
{
if (FileUpload1.HasFile)
{
fileimg = FileUpload1.FileName;
FileUpload1.SaveAs(Server.MapPath("~/Image/") + FileUpload1.FileName);
}
else
{
fileimg = "Null";
}
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["kush"].ConnectionString);
SqlCommand cmd = new SqlCommand("insert into tb_image values('" + txtname.Text + "','" +fileimg+ "')", con);
con.Open();
cmd.ExecuteNonQuery();
con.Close();

lblmessage.Visible = true;
lblmessage.ForeColor = Color.Green;
lblmessage.Text = "Save Successfully";
BindGridView();
}
}
}

Result:







Thursday, 18 September 2014

How to Export GridView Data to Excel and Word in Asp.Net

How to Export GridView Data to Excel and Word in Asp.Net……………………….

Note :In this topic we will discuss how to export GridView Data to Excel and Word in Asp.Net

--sql query………………………………………….

create database GridConcept
use GridConcept
create table Employee
(
SrNo int identity(1,1) primary key,
Name varchar(50),
Gender varchar(50),
Address varchar(50),
MobileNo varchar(50)
)

Default.aspx ………………………………………

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title></title>
<style type="text/css">
.style1
{
width: 100%;
}
</style>
</head>
<body>
<form id="form1" runat="server">
<div>
<table class="style1">
<tr>
<td>
<font color="blue"><b><center>How to Export GridView Data to Excel and Word in Asp.Net</center></b></font> </td>
</tr>
<tr>
<td >
<asp:GridView ID="GridView1" runat="server" BackColor="White"
BorderColor="#CC9966" BorderStyle="None" BorderWidth="1px" CellPadding="4"
onrowdatabound="GridView1_RowDataBound">
<FooterStyle BackColor="#FFFFCC" ForeColor="#330099" />
<HeaderStyle BackColor="#990000" Font-Bold="True" ForeColor="#FFFFCC" />
<PagerStyle BackColor="#FFFFCC" ForeColor="#330099" HorizontalAlign="Center" />
<RowStyle BackColor="White" ForeColor="#330099" />
<SelectedRowStyle BackColor="#FFCC66" Font-Bold="True" ForeColor="#663399" />
<SortedAscendingCellStyle BackColor="#FEFCEB" />
<SortedAscendingHeaderStyle BackColor="#AF0101" />
<SortedDescendingCellStyle BackColor="#F6F0C0" />
<SortedDescendingHeaderStyle BackColor="#7E0000" />
</asp:GridView>
</td>
</tr>
<tr>
<td >
<asp:Button ID="btnexportExcel" runat="server"
Text="Export GridView to Excel" onclick="btnexportExcel_Click" />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
&nbsp;&nbsp;
<asp:Button ID="btnExportWord" runat="server"
Text="Export GridView to Word" onclick="btnExportWord_Click" />
</td></tr>
</table>
</div>
</form>
</body>
</html>


Default.aspx.cs ………………………………………

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;
//using this namespace……………………………
using System.IO;
using System.Drawing;

public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
// CODE FOR BIND GRIDVIEW ON PAGELOAD ................
if (!IsPostBack)
{
SqlConnection con = new SqlConnection("Data Source=KUSHTIWARI-PC;Initial Catalog=GridConcept;Integrated Security=True");
SqlCommand cmd = new SqlCommand("select * from Employee", con);
DataSet ds = new DataSet();
SqlDataAdapter da = new SqlDataAdapter(cmd);
da.Fill(ds);
GridView1.DataSource = ds.Tables[0];
GridView1.DataBind();
}

}

//code for rowdatabound event on gridview......................
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
String mno = DataBinder.Eval(e.Row.DataItem, "MobileNo").ToString();
if (mno.Length < 10)
{
e.Row.Cells[4].ForeColor = System.Drawing.Color.Red;
e.Row.Cells[4].Font.Bold = true;
e.Row.Cells[4].Font.Size = 12;
}
}
}

// Export GridView to Excel................

protected void btnexportExcel_Click(object sender, EventArgs e)
{

Response.ClearContent();
Response.ContentType = "application/excel";
Response.AppendHeader("content-disposition", "attachment;filename=Employee.xls");
StringWriter sw = new StringWriter();
HtmlTextWriter htw = new HtmlTextWriter(sw);
GridView1.RenderControl(htw);
Response.Write(sw.ToString());
Response.End();

}

// this code for removing this type error  on page  Control 'GridView1' of type 'GridView' must be placed inside a form tag with runat=server.

public override void VerifyRenderingInServerForm(Control control)
{

}

// Export GridView to Word..................

protected void btnExportWord_Click(object sender, EventArgs e)
{
Response.ClearContent();
Response.ContentType = "application/word";
Response.AppendHeader("content-disposition", "attachment;filename=Employee.doc");
StringWriter sw = new StringWriter();
HtmlTextWriter htw = new HtmlTextWriter(sw);

GridView1.HeaderRow.Style.Add("background-color", "#990000");
foreach (TableCell tc  in GridView1.HeaderRow.Cells)
{
tc.Style["background-color"] = "#990000";
}

foreach (GridViewRow grv in GridView1.Rows)
{
// <SelectedRowStyle BackColor="#FFCC66" Font-Bold="True" ForeColor="#663399" />
GridView1.BackColor = Color.White;
foreach (TableCell gridRowTableCell in grv.Cells)
{
gridRowTableCell.Style["background-color"] = "#FFCC66";
}
}
GridView1.RenderControl(htw);
Response.Write(sw.ToString());
Response.End();
}
}