Dear Sir,
while i have make pdf export beteen two date then i have getting error.
Server Error in '/Mobile_details_verifiction' Application.
Incorrect syntax near '10-11-2021 '.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. Exception Details: System.Data.SqlClient.SqlException: Incorrect syntax near '10-11-2021 '. Source Error:
Line 492: using (DataTable dt = new DataTable())
Line 493: {
Line 494: sda.Fill(dt);
Line 495: return dt;
Line 496: }
|
Source File: c:\inetpub\wwwroot\Mobile_detals\Mob_del_dtls\Mob_del_dtls\admn.aspx.cs Line: 494
namespace Mob_del_dtls
{
public partial class admn : System.Web.UI.Page
{
protected void Button2_Click_Export(object sender, EventArgs e)
{
DataTable dt = GetData("SELECT emp_id Emp_ID, emp_nm Emp_Name, mob_nm Mobile_No, Bill_mob_nm Bill_Mobile_No, frm_dt From_Date, to_dt To_Date, invc_no Invoice_No, invc_dt Invoice_Dt,lctn Location,vrfd_by Verified_By,vrfd Verified FROM mbl_usg_dtl where frm_dt = '" + Textbox1.Text + "' and to_dt '" + Textbox2.Text + "'");
//DataTable dt = GetData("SELECT emp_id Emp_ID, emp_nm Emp_Name, mob_nm Mobile_No, Bill_mob_nm Bill_Mobile_No, frm_dt From_Date, to_dt To_Date, invc_no Invoice_No, invc_dt Invoice_Dt,lctn Location,vrfd_by Verified_By,vrfd Verified FROM mbl_usg_dtl where Clsd_flag='N' ");
Document document = new Document(PageSize.A3, 25f, 15f, 15f, 15f);
iTextSharp.text.Font NormalFont = FontFactory.GetFont("Arial", 12, Font.NORMAL, Color.BLACK);
using (System.IO.MemoryStream memoryStream = new System.IO.MemoryStream())
{
PdfWriter writer = PdfWriter.GetInstance(document, memoryStream);
Phrase phrase = null;
PdfPCell cell = null;
PdfPTable table = null;
//Color color = null;
document.Open();
table = new PdfPTable(1);
table.TotalWidth = 800f;
table.LockedWidth = true;
table.SetWidths(new float[] {1f });
cell = ImageCell(@"~/img/munpl logo.png", 40f, PdfPCell.ALIGN_CENTER);
table.AddCell(cell);
document.Add(table);
table = new PdfPTable(1);
table.TotalWidth = 800f;
table.LockedWidth = true;
table.SetWidths(new float[] { 1f });
phrase = new Phrase();
phrase.Add(new Chunk("Meja Thermal Power Project", FontFactory.GetFont("Arial", 16, Font.BOLD, Color.BLACK)));
cell = PhraseCell(phrase, PdfPCell.ALIGN_CENTER);
cell.VerticalAlignment = PdfPCell.ALIGN_TOP;
table.AddCell(cell);
document.Add(table);
table = new PdfPTable(1);
table.TotalWidth = 800f;
table.LockedWidth = true;
table.SetWidths(new float[] { 1f });
phrase = new Phrase();
phrase.Add(new Chunk("A Joint Venture of NTPC Ltd & UPRVUNL", FontFactory.GetFont("Arial", 10, Color.BLACK)));
cell = PhraseCell(phrase, PdfPCell.ALIGN_CENTER);
cell.VerticalAlignment = PdfPCell.ALIGN_TOP;
table.AddCell(cell);
document.Add(table);
table = new PdfPTable(11);
table.TotalWidth = 800f;
table.LockedWidth = true;
table.HorizontalAlignment = Element.ALIGN_LEFT;
table.SetWidths(new float[] { 0.7f, 1f, 1f, 1f, 1f, 0.7f, 0.7f, 0.7f, 0.7f, 1f, 1f });
table.SpacingBefore = 20f;
for (int j = 0; j < dt.Columns.Count; j++)
{
phrase = new Phrase();
phrase.Add(new Chunk(dt.Columns[j].ToString(), FontFactory.GetFont("Arial", 15, Font.BOLD, Color.BLACK)));
cell = PhraseCell(phrase, PdfPCell.ALIGN_LEFT);
cell.VerticalAlignment = PdfPCell.ALIGN_MIDDLE;
table.AddCell(cell);
}
for (int i = 0; i < dt.Rows.Count; i++)
{
DataRow dr = dt.Rows[i];
phrase = new Phrase();
phrase.Add(new Chunk(dr[0].ToString(), FontFactory.GetFont("Arial", 15, Font.NORMAL, Color.BLACK)));
cell = PhraseCell(phrase, PdfPCell.ALIGN_LEFT);
cell.VerticalAlignment = PdfPCell.ALIGN_MIDDLE;
table.AddCell(cell);
phrase = new Phrase();
phrase.Add(new Chunk(dr[1].ToString(), FontFactory.GetFont("Arial", 15, Font.NORMAL, Color.BLACK)));
cell = PhraseCell(phrase, PdfPCell.ALIGN_LEFT);
cell.VerticalAlignment = PdfPCell.ALIGN_MIDDLE;
table.AddCell(cell);
phrase = new Phrase();
phrase.Add(new Chunk(dr[2].ToString(), FontFactory.GetFont("Arial", 15, Font.NORMAL, Color.BLACK)));
cell = PhraseCell(phrase, PdfPCell.ALIGN_LEFT);
cell.VerticalAlignment = PdfPCell.ALIGN_MIDDLE;
table.AddCell(cell);
phrase = new Phrase();
phrase.Add(new Chunk(dr[3].ToString(), FontFactory.GetFont("Arial", 15, Font.NORMAL, Color.BLACK)));
cell = PhraseCell(phrase, PdfPCell.ALIGN_LEFT);
cell.VerticalAlignment = PdfPCell.ALIGN_MIDDLE;
table.AddCell(cell);
phrase = new Phrase();
phrase.Add(new Chunk(dr[4].ToString(), FontFactory.GetFont("Arial", 15, Font.NORMAL, Color.BLACK)));
cell = PhraseCell(phrase, PdfPCell.ALIGN_LEFT);
cell.VerticalAlignment = PdfPCell.ALIGN_MIDDLE;
table.AddCell(cell);
phrase = new Phrase();
phrase.Add(new Chunk(dr[5].ToString(), FontFactory.GetFont("Arial", 15, Font.NORMAL, Color.BLACK)));
cell = PhraseCell(phrase, PdfPCell.ALIGN_LEFT);
cell.VerticalAlignment = PdfPCell.ALIGN_MIDDLE;
table.AddCell(cell);
phrase = new Phrase();
phrase.Add(new Chunk(dr[6].ToString(), FontFactory.GetFont("Arial", 15, Font.NORMAL, Color.BLACK)));
cell = PhraseCell(phrase, PdfPCell.ALIGN_LEFT);
cell.VerticalAlignment = PdfPCell.ALIGN_MIDDLE;
table.AddCell(cell);
phrase = new Phrase();
phrase.Add(new Chunk(dr[7].ToString(), FontFactory.GetFont("Arial", 15, Font.NORMAL, Color.BLACK)));
cell = PhraseCell(phrase, PdfPCell.ALIGN_LEFT);
cell.VerticalAlignment = PdfPCell.ALIGN_MIDDLE;
table.AddCell(cell);
phrase = new Phrase();
phrase.Add(new Chunk(dr[8].ToString(), FontFactory.GetFont("Arial", 15, Font.NORMAL, Color.BLACK)));
cell = PhraseCell(phrase, PdfPCell.ALIGN_LEFT);
cell.VerticalAlignment = PdfPCell.ALIGN_MIDDLE;
table.AddCell(cell);
phrase = new Phrase();
phrase.Add(new Chunk(dr[9].ToString(), FontFactory.GetFont("Arial", 15, Font.NORMAL, Color.BLACK)));
cell = PhraseCell(phrase, PdfPCell.ALIGN_LEFT);
cell.VerticalAlignment = PdfPCell.ALIGN_MIDDLE;
table.AddCell(cell);
phrase = new Phrase();
phrase.Add(new Chunk(dr[10].ToString(), FontFactory.GetFont("Arial", 15, Font.NORMAL, Color.BLACK)));
cell = PhraseCell(phrase, PdfPCell.ALIGN_LEFT);
cell.VerticalAlignment = PdfPCell.ALIGN_MIDDLE;
table.AddCell(cell);
}
document.Add(table);
table = new PdfPTable(1);
table.TotalWidth = 800f;
table.LockedWidth = true;
table.SetWidths(new float[] { 1f });
phrase = new Phrase();
phrase.Add(new Chunk("\n\n************************************************************************************This is an electronically generated report, hence does not require a signature*****************************************************************************", FontFactory.GetFont("Arial", 8, Font.BOLD, Color.BLACK)));
cell = PhraseCell(phrase, PdfPCell.ALIGN_MIDDLE);
cell.VerticalAlignment = PdfPCell.ALIGN_RIGHT;
table.AddCell(cell);
document.Add(table);
document.Close();
byte[] bytes = memoryStream.ToArray();
memoryStream.Close();
Response.Clear();
Response.ContentType = "application/pdf";
Response.AddHeader("Content-Disposition", "attachment; filename=Employee.pdf");
Response.ContentType = "application/pdf";
Response.Buffer = true;
Response.Cache.SetCacheability(HttpCacheability.NoCache);
Response.BinaryWrite(bytes);
Response.End();
Response.Close();
}
}
private DataTable GetData(string query)
{
string conString = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
SqlCommand cmd = new SqlCommand(query);
using (SqlConnection con = new SqlConnection(conString))
{
using (SqlDataAdapter sda = new SqlDataAdapter())
{
cmd.Connection = con;
sda.SelectCommand = cmd;
using (DataTable dt = new DataTable())
{
sda.Fill(dt);
return dt;
}
}
}
}
}
}