Use the Below Functions for Get months between two dates
Create Function [dbo].[fnNumtoDate]
(
@StartDate DATETIME,
@EndDate DATETIME
)
RETURNS @MonthTable TABLE (
Mon varchar(20)
)
As
Begin
;WITH MONTHS (date)
AS
(
SELECT @StartDate
UNION ALL
SELECT DATEADD(MONTH,1,date)
FROM MONTHS
WHERE DATEADD(MONTH,1,date)<=@EndDate
)
Insert into @MonthTable (Mon)
SELECT ( CASE MONTH(date)
WHEN 1 THEN 'January'
WHEN 2 THEN 'February'
WHEN 3 THEN 'March'
WHEN 4 THEN 'April'
WHEN 5 THEN 'May'
WHEN 6 THEN 'June'
WHEN 7 THEN 'July'
WHEN 8 THEN 'August'
WHEN 9 THEN 'September'
WHEN 10 THEN 'October'
WHEN 11 THEN 'November'
WHEN 12 THEN 'December'
END ) AS MONTH FROM MONTHS
RETURN;
ENd
Select * from dbo.fnNumtoDate( '2012-03-12',GETDATE())
Sibi's Blog
Friday, March 29, 2013
Wednesday, August 1, 2012
Change Gridview Button text based upon Datbase Bit Value
Now I'm going to explain how to change the Grid View Button Text or Image (Active/Inactive) based up on the database value bit column (True/False)
step2 : Binding Database table value in Data table
Private void FillGrid() // Filling values to Gridview
{
SqlConnection con = new SqlConnection(Constring);
con.Open();
SqlDataAdapter da = new SqlDataAdapter("select Userid,Isactive from Employee_Details, con);
\\Here Isactive was Bit Value
DataTable dt = new DataTable();
da.Fill(dt);
con.Close();
Grd.DataSource = dt;
Grd.DataBind();
foreach (GridViewRow row in Grd.Rows)
{
ImageButton btn1 = (ImageButton)(Grd.Rows[row.RowIndex].FindControl("btn1"));
ImageButton btn2 = (ImageButton)(Grd.Rows[row.RowIndex].FindControl("btn2"));
string Flag = dt.Rows[0]["Isactive"].ToString();
if (dt.Rows.Count > 0)
{
dt.Rows[0].Delete();
dt.AcceptChanges();
}
if (Flag == "True")
{
btn1.Visible = true;
btn2.Visible = false;
}
else
{
btn1.Visible = false;
btn2.Visible = true;
}
}
}
step1 : Add this Template field in your inside the Grid view column
step2 : Binding Database table value in Data table
Private void FillGrid() // Filling values to Gridview{
SqlConnection con = new SqlConnection(Constring);
con.Open();
SqlDataAdapter da = new SqlDataAdapter("select Userid,Isactive from Employee_Details, con);
\\Here Isactive was Bit Value
DataTable dt = new DataTable();
da.Fill(dt);
con.Close();
Grd.DataSource = dt;
Grd.DataBind();
foreach (GridViewRow row in Grd.Rows)
{
ImageButton btn1 = (ImageButton)(Grd.Rows[row.RowIndex].FindControl("btn1"));
ImageButton btn2 = (ImageButton)(Grd.Rows[row.RowIndex].FindControl("btn2"));
string Flag = dt.Rows[0]["Isactive"].ToString();
if (dt.Rows.Count > 0)
{
dt.Rows[0].Delete();
dt.AcceptChanges();
}
if (Flag == "True")
{
btn1.Visible = true;
btn2.Visible = false;
}
else
{
btn1.Visible = false;
btn2.Visible = true;
}
}
}
step3 : Binding Grid View
protected void Page_Load(object sender, EventArgs e)
{
if(!IsPostBack)
FillGrid(); \\Binding Grid View
}
{
if(!IsPostBack)
FillGrid(); \\Binding Grid View
}
step4 : Changing the Button Text (click the row command event in grid view )
protected void Grd_RowCommand(object sender, GridViewCommandEventArgs e)
{
string x = e.CommandName;//Getting the commenad name we mentioned in Gridview BUtton
if(x=="btn1"|x=="btn2")//checking the button click event which we mentioned in the bitton ie.. command argument
{
int cmd = Convert.ToInt32(e.CommandArgument);//Getting the command Argument ie.. which row we selecting
string id = Grd.Rows[cmd].Cells[0].Text;// Getting UserId from the Gridview selected row
SqlConnection con = new SqlConnection(Constring);
con.Open();
if (x == "btn1")
{
string Query = "update Employee_Details set Isactive = 0 where Userid=" + id;
SqlDataAdapter da = new SqlDataAdapter(Query, con);
}
else
{
string Query = "update Employee_Details set Isactive = 1 where Userid=" + id;
SqlDataAdapter da = new SqlDataAdapter(Query, con);
}
con.Close();
}
FillGrid();
}
{
string x = e.CommandName;//Getting the commenad name we mentioned in Gridview BUtton
if(x=="btn1"|x=="btn2")//checking the button click event which we mentioned in the bitton ie.. command argument
{
int cmd = Convert.ToInt32(e.CommandArgument);//Getting the command Argument ie.. which row we selecting
string id = Grd.Rows[cmd].Cells[0].Text;// Getting UserId from the Gridview selected row
SqlConnection con = new SqlConnection(Constring);
con.Open();
if (x == "btn1")
{
string Query = "update Employee_Details set Isactive = 0 where Userid=" + id;
SqlDataAdapter da = new SqlDataAdapter(Query, con);
}
else
{
string Query = "update Employee_Details set Isactive = 1 where Userid=" + id;
SqlDataAdapter da = new SqlDataAdapter(Query, con);
}
con.Close();
}
FillGrid();
}
Output
Friday, May 11, 2012
Import Grid View Data to Excel or Pdf in Asp.net c#
Import Grid View Data to Excel or Pdf in Asp.net c#
// Design
1 . Create 1 Grid View .
// Code Behind
1. Bind the Gridview
Step2 :
// For Excel
// declare format
{
Response.Clear();
Response.AddHeader("content-disposition", "attachment;filename=GridView1.xls");
Response.Charset = "";
Response.ContentType = "application/vnd.xls";
HttpContext.Current.Response.Write(this.MsoFormats);
StringWriter StringWriter = new StringWriter();
HtmlTextWriter HtmlTextWriter = new HtmlTextWriter(StringWriter);
GridView1.RenderControl(HtmlTextWriter);
Response.Write(StringWriter.ToString());
Response.End();
}
// For Pdf
protected void btnPDF_Click(object sender, ImageClickEventArgs e)
{
Response.ContentType = "application/pdf";
Response.AddHeader("content-disposition", "attachment;filename=UserDetails.pdf");
Response.Cache.SetCacheability(HttpCacheability.NoCache);
StringWriter sw = new StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(sw);
gvdetails.AllowPaging = false;
gvdetails.DataBind();
gvdetails.RenderControl(hw);
gvdetails.HeaderRow.Style.Add("width", "15%");
gvdetails.HeaderRow.Style.Add("font-size", "10px");
gvdetails.Style.Add("text-decoration", "none");
gvdetails.Style.Add("font-family", "Arial, Helvetica, sans-serif;");
gvdetails.Style.Add("font-size", "8px");
StringReader sr = new StringReader(sw.ToString());
Document pdfDoc = new Document(PageSize.A2, 7f, 7f, 7f, 0f);
HTMLWorker htmlparser = new HTMLWorker(pdfDoc);
PdfWriter.GetInstance(pdfDoc, Response.OutputStream);
//string Path = Server.MapPath("~/Jaram PDF/") + "Sample.pdf";
//PdfWriter.GetInstance(pdfDoc, new FileStream(Path, FileMode.Create));
pdfDoc.Open();
htmlparser.Parse(sr);
pdfDoc.Close();
Response.Write(pdfDoc);
Response.End();
}
// add this Render Comtrol in Code Bhind
public override void VerifyRenderingInServerForm(Control control)
{
}
Thanks ..
Regards
[ Sibi Elango]
// Design
1 . Create 1 Grid View .
// Code Behind
1. Bind the Gridview
Step2 :
// For Excel
// declare format
public string MsoFormats = "";
protected void ImageButton1_Click(object sender, ImageClickEventArgs e){
Response.Clear();
Response.AddHeader("content-disposition", "attachment;filename=GridView1.xls");
Response.Charset = "";
Response.ContentType = "application/vnd.xls";
HttpContext.Current.Response.Write(this.MsoFormats);
StringWriter StringWriter = new StringWriter();
HtmlTextWriter HtmlTextWriter = new HtmlTextWriter(StringWriter);
GridView1.RenderControl(HtmlTextWriter);
Response.Write(StringWriter.ToString());
Response.End();
}
// For Pdf
protected void btnPDF_Click(object sender, ImageClickEventArgs e)
{
Response.ContentType = "application/pdf";
Response.AddHeader("content-disposition", "attachment;filename=UserDetails.pdf");
Response.Cache.SetCacheability(HttpCacheability.NoCache);
StringWriter sw = new StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(sw);
gvdetails.AllowPaging = false;
gvdetails.DataBind();
gvdetails.RenderControl(hw);
gvdetails.HeaderRow.Style.Add("width", "15%");
gvdetails.HeaderRow.Style.Add("font-size", "10px");
gvdetails.Style.Add("text-decoration", "none");
gvdetails.Style.Add("font-family", "Arial, Helvetica, sans-serif;");
gvdetails.Style.Add("font-size", "8px");
StringReader sr = new StringReader(sw.ToString());
Document pdfDoc = new Document(PageSize.A2, 7f, 7f, 7f, 0f);
HTMLWorker htmlparser = new HTMLWorker(pdfDoc);
PdfWriter.GetInstance(pdfDoc, Response.OutputStream);
//string Path = Server.MapPath("~/Jaram PDF/") + "Sample.pdf";
//PdfWriter.GetInstance(pdfDoc, new FileStream(Path, FileMode.Create));
pdfDoc.Open();
htmlparser.Parse(sr);
pdfDoc.Close();
Response.Write(pdfDoc);
Response.End();
}
// add this Render Comtrol in Code Bhind
public override void VerifyRenderingInServerForm(Control control)
{
}
Thanks ..
Regards
[ Sibi Elango]
Subscribe to:
Posts (Atom)