顯示具有 PDF 標籤的文章。 顯示所有文章
顯示具有 PDF 標籤的文章。 顯示所有文章

2014年7月13日 星期日

[RESOLVED] Exporting datalist and gridview to excel


Hello Experts,


I am having one datalist and gridview within datalist control. I need to export both of them as report in excel sheet. Like below


sr.  Class  CreatedDate ModifiedDate


1     Class1   25/5/2013   25/5/2013


Students


Sr.  Name Address Bdate


1    Abc    test add  25/5/1990


2    DEF   test add 2  25/3/1991


sr.  Class  CreatedDate ModifiedDate


2     Class2   25/6/2013   25/6/2013


Students


Sr.  Name Address Bdate


1    Abc2    test add22  25/5/1990


2    DEF2   test add 22  25/3/1991


and so on.....


Here the class display is datalist and student display is gridview.


Please suggest how to achive that?


Thank you.




Refer bellow url


http://www.aspsnippets.com/Articles/Export-GridView-To-WordExcelPDFCSV-in-ASP.Net.aspx



Hi,


Please try to refer to the following code about export data to excel (use the gridview as an example):


protected void fillGrid()
{
string str = "SELECT [UNo], [EmpName], [Age],
convert(char,[dob],103) dob FROM [tbl_EmpDetails]";

myConnection = new SqlConnection(conn);
myConnection.Open();
myCommand = new SqlCommand(str, myConnection);
SqlDataAdapter mySQLDataAdapter;
myDataSet = new DataTable();
mySQLDataAdapter = new SqlDataAdapter(myCommand);
mySQLDataAdapter.Fill(myDataSet);
GridView1.DataSource = myDataSet;
GridView1.DataBind();
ViewState["dtList"] = myDataSet;
}

protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
fillGrid();
}
}

Now, after binding a Gridview, data is ready to get exported to an Excel file. Click on a button named Export to Excel. Used FileInfo to get the information related to the file.


FileInfo FI = new FileInfo(Path);
StringWriter stringWriter = new StringWriter();
HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWriter);
DataGrid DataGrd = new DataGrid();
DataGrd.DataSource = dt1;
DataGrd.DataBind();

DataGrd.RenderControl(htmlWrite);
string directory = Path.Substring(0, Path.LastIndexOf("\\"));// GetDirectory(Path);
if (!Directory.Exists(directory))
{
Directory.CreateDirectory(directory);
}

System.IO.StreamWriter vw = new System.IO.StreamWriter(Path, true);
stringWriter.ToString().Normalize();
vw.Write(stringWriter.ToString());
vw.Flush();
vw.Close();
WriteAttachment(FI.Name, "application/vnd.ms-excel", stringWriter.ToString());

The above code uses a WriteAttachment function which pushes the attachment to the user in the Response object. The following code shows the implementation of WriteAttachment:


public static void WriteAttachment(string FileName, string FileType, string content)
{
HttpResponse Response = System.Web.HttpContext.Current.Response;
Response.ClearHeaders();
Response.AppendHeader("Content-Disposition", "attachment; filename=" + FileName);
Response.ContentType = FileType;
Response.Write(content);
Response.End();
}

Hope it can help you.


Best Regards,

Amy Peng 


[RESOLVED] file download link within the gridview and perform download


hi,


 am having a gridview with path to the  file.name , where file is  physically stored in the filesystem and am storing the filepath in my db table column.so, i need to pull that path and show it in grdiview column. and when user clicks on the document link,
user shud be able to download the file .


i was  able to show the  document name as link in the gridview , but i am unable to download properly.also, the document extension can be anything/ docx, xlsx,txt,ppt, pdf etc. how to implement this download.


or


should i store the file name and data/content in the  sql db itself as  varbinary(max) datatype.? and in that case, how to retrieve the content to download



Since it's just part of the filesystem, you can't link to it directly as you would need to have a virtual path, ie: a web-path and not a server-side path (as in: C:\myfiles\mydirectory\myfile.doc). If you use a LinkButton, you can set the commandargument
for it to be the file path, then for the command event try the following


string path = e.CommandArgument.ToString();
if(File.Exists(path))
{
Response.Clear();
Response.TransmitFile(path);
Response.Flush();
}

You can add custom logic for adding an appropriate content type so the browser will understand what to do with the file.





BenjaminKNR


i was  able to show the  document name as link in the gridview , but i am unable to download properly.also, the document extension can be anything/ docx, xlsx,txt,ppt, pdf etc. how to implement this download.


Leave file in the filesystem.


Your files should be within your website directory


Suppose there is folder Name Files in your website



Set link in your gridview like "www.yoursite.com/Files/YourFile.txt" for files


Make sure permissions are set to read and download files from that folder


Regards




if (e.CommandName == "Download")
{
try
{
string filename = e.CommandArgument.ToString();
Response.ContentType = "application/pdf";
Response.AddHeader("Content-Disposition", "attachment;filename=Archiving.pdf");
Response.TransmitFile(Server.MapPath(filename ));
Response.End();
}
catch (Exception ex)
{
}
}

Im using LinkButton inside gridview where Command Argument =File Path 


Up Code For Download Any file .pdf and you can use Switch Statement for download any type


regards



If you want add download link column to gridview, save virtual path to filepath field. Virtual path like : filename.ext or folder/filename.ext or ~/folder/subfolder/filename.ext

[RESOLVED] Exporting datalist and gridview to excel


Hello Experts,


I am having one datalist and gridview within datalist control. I need to export both of them as report in excel sheet. Like below


sr.  Class  CreatedDate ModifiedDate


1     Class1   25/5/2013   25/5/2013


Students


Sr.  Name Address Bdate


1    Abc    test add  25/5/1990


2    DEF   test add 2  25/3/1991


sr.  Class  CreatedDate ModifiedDate


2     Class2   25/6/2013   25/6/2013


Students


Sr.  Name Address Bdate


1    Abc2    test add22  25/5/1990


2    DEF2   test add 22  25/3/1991


and so on.....


Here the class display is datalist and student display is gridview.


Please suggest how to achive that?


Thank you.




Refer bellow url


http://www.aspsnippets.com/Articles/Export-GridView-To-WordExcelPDFCSV-in-ASP.Net.aspx



Hi,


Please try to refer to the following code about export data to excel (use the gridview as an example):


protected void fillGrid()
{
string str = "SELECT [UNo], [EmpName], [Age],
convert(char,[dob],103) dob FROM [tbl_EmpDetails]";

myConnection = new SqlConnection(conn);
myConnection.Open();
myCommand = new SqlCommand(str, myConnection);
SqlDataAdapter mySQLDataAdapter;
myDataSet = new DataTable();
mySQLDataAdapter = new SqlDataAdapter(myCommand);
mySQLDataAdapter.Fill(myDataSet);
GridView1.DataSource = myDataSet;
GridView1.DataBind();
ViewState["dtList"] = myDataSet;
}

protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
fillGrid();
}
}

Now, after binding a Gridview, data is ready to get exported to an Excel file. Click on a button named Export to Excel. Used FileInfo to get the information related to the file.


FileInfo FI = new FileInfo(Path);
StringWriter stringWriter = new StringWriter();
HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWriter);
DataGrid DataGrd = new DataGrid();
DataGrd.DataSource = dt1;
DataGrd.DataBind();

DataGrd.RenderControl(htmlWrite);
string directory = Path.Substring(0, Path.LastIndexOf("\\"));// GetDirectory(Path);
if (!Directory.Exists(directory))
{
Directory.CreateDirectory(directory);
}

System.IO.StreamWriter vw = new System.IO.StreamWriter(Path, true);
stringWriter.ToString().Normalize();
vw.Write(stringWriter.ToString());
vw.Flush();
vw.Close();
WriteAttachment(FI.Name, "application/vnd.ms-excel", stringWriter.ToString());

The above code uses a WriteAttachment function which pushes the attachment to the user in the Response object. The following code shows the implementation of WriteAttachment:


public static void WriteAttachment(string FileName, string FileType, string content)
{
HttpResponse Response = System.Web.HttpContext.Current.Response;
Response.ClearHeaders();
Response.AppendHeader("Content-Disposition", "attachment; filename=" + FileName);
Response.ContentType = FileType;
Response.Write(content);
Response.End();
}

Hope it can help you.


Best Regards,

Amy Peng 


[RESOLVED] file download link within the gridview and perform download


hi,


 am having a gridview with path to the  file.name , where file is  physically stored in the filesystem and am storing the filepath in my db table column.so, i need to pull that path and show it in grdiview column. and when user clicks on the document link,
user shud be able to download the file .


i was  able to show the  document name as link in the gridview , but i am unable to download properly.also, the document extension can be anything/ docx, xlsx,txt,ppt, pdf etc. how to implement this download.


or


should i store the file name and data/content in the  sql db itself as  varbinary(max) datatype.? and in that case, how to retrieve the content to download



Since it's just part of the filesystem, you can't link to it directly as you would need to have a virtual path, ie: a web-path and not a server-side path (as in: C:\myfiles\mydirectory\myfile.doc). If you use a LinkButton, you can set the commandargument
for it to be the file path, then for the command event try the following


string path = e.CommandArgument.ToString();
if(File.Exists(path))
{
Response.Clear();
Response.TransmitFile(path);
Response.Flush();
}

You can add custom logic for adding an appropriate content type so the browser will understand what to do with the file.





BenjaminKNR


i was  able to show the  document name as link in the gridview , but i am unable to download properly.also, the document extension can be anything/ docx, xlsx,txt,ppt, pdf etc. how to implement this download.


Leave file in the filesystem.


Your files should be within your website directory


Suppose there is folder Name Files in your website



Set link in your gridview like "www.yoursite.com/Files/YourFile.txt" for files


Make sure permissions are set to read and download files from that folder


Regards




if (e.CommandName == "Download")
{
try
{
string filename = e.CommandArgument.ToString();
Response.ContentType = "application/pdf";
Response.AddHeader("Content-Disposition", "attachment;filename=Archiving.pdf");
Response.TransmitFile(Server.MapPath(filename ));
Response.End();
}
catch (Exception ex)
{
}
}

Im using LinkButton inside gridview where Command Argument =File Path 


Up Code For Download Any file .pdf and you can use Switch Statement for download any type


regards



If you want add download link column to gridview, save virtual path to filepath field. Virtual path like : filename.ext or folder/filename.ext or ~/folder/subfolder/filename.ext

[RESOLVED] ListItem is an ambigious refence


Hi, 


I am trying to create a pdf document using iTextSharp, as soon as I added the using iTextSharp references I got error messages (ListItem is an ambigious reference) pertaining my ListItem shown below. Please help me sort this error out.


Below is a portion of the code


using System;

using System.Collections.Generic;

using System.Linq;

using System.Web;

using System.Web.UI;

using System.Web.UI.WebControls;

using BusinessLayer;

using System.Data;

using System.Data.SqlClient;

using System.Data.SqlTypes;

using System.IO;

using iTextSharp.text;

using iTextSharp.text.pdf;



if (lstExistingTopics.Items.Count > 0)

{



foreach (ListItem item in lstExistingTopics.Items)

{

if (item.Selected == true)

{

lstNewTopics.Items.Add(item);

}

}


}


Your help will be greatly appreciated.




You have to fully qualify the type of ListItem you are creating. I don't know what type lstExistingTopics.Items is, but you can use var like this:


foreach (var item in lstExistingTopics.Items)

EDIT:


The reason is because both System.Web.UI.WebControls and iTextSharp have a class called "ListItem" - thus the ambiguous reference when you just type "ListItem". 



[RESOLVED] is this possible to create Excel using itextsharp as PDF


hi,


i have code to write an pdf doc using itextsharp..i need same for excel...i tried to pass it to excel but its not working.?


any solution ??



No - iTextSharp cannot convert Excel to PDF format. 


[RESOLVED] How do I reset Dropdownlist to default or lose focus after SelectedIndexChanged


I am using a dropdownlists to display and on selectedindexchange download specific PDF files that are stored in my SQL Server table.   I have 6 dropdownlists that display different PDF's groups, If I select the first dropdownlist box it downloads the appropriate
PDF.  If I go to one of my other dropdownlist boxes and select a PDF file from it it downloads the file from the first dropdownlist.


So my thinking is that I have to reset each dropdownlist to default upon selectedindexchanged or tell the page to lose focus on the dropdownlist upon completion for the selectedindexchanged event.


Any suggestions would be greatly appreciated. 



Can you post the code ?



Here is the code for my first dropdownlist:


Protected Sub BM_DDL_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles BM_DDL.SelectedIndexChanged
Try

Dim fileName As String = ""
Dim fileExtention As String = ".pdf"
SQLSelectStr = "Select Left(docname,LEN(docname)-4) as DocName,DocData FROM Documents WHERE DocName = '" & BM_DDL.SelectedValue + ".pdf'"
Dim cmd As New SqlCommand(SQLSelectStr, Gov_conn)
Gov_conn.Close()
Gov_conn.Open()
Dim dReader As SqlDataReader = cmd.ExecuteReader()
Dim dt As DataTable = New DataTable
dt.Load(dReader)
Dim bytes() As Byte = CType(dt.Rows(0)("DocData"), Byte())
Response.Buffer = True
Response.Charset = ""
Response.Cache.SetCacheability(HttpCacheability.NoCache)
Response.ContentType = ReturnExtension(fileExtention)
Response.AddHeader("Content-Disposition", "attachment; filename=" + fileName)
Response.BinaryWrite(bytes)
Response.Flush()
Response.Close()
' Response.End()

Gov_conn.Close()
Catch generatedExceptionName As System.Threading.ThreadAbortException
Console.WriteLine(generatedExceptionName)
End Try

End Sub

Any help would be greatly appreciated.



Hi,


set drop down index to  zero after selected index event(if you want to reset the value of drop down)


for ex :      BM_DDL.SelectedIndex = 0 ( please correct me if I understood wrongly).



I've tried that, it isn't changing anything and the focus is still on the first dropdownlist.  This is why I posted the question on here.  I've fought for the last few days trying to get this to work.



Unfortunaltely i've already tried that approach.  It should be making a difference or at least do something however it is not.



You could follow the steps below to simplify your steps,







Protected Sub BM_DDL_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles BM_DDL.SelectedIndexChanged
Try
Dim SelectedItemValue As String = CType(sender,DropDownList).SelectedValue

Dim fileName As String = ""
Dim fileExtention As String = ".pdf"
SQLSelectStr = "Select Left(docname,LEN(docname)-4) as DocName,DocData FROM Documents WHERE DocName = '" & SelectedItemValue + ".pdf'"
.........
.........
End Try
End Sub



That seems like a good option.  However it keeps on giving me this error.


Compiler Error Message: BC30577: 'AddressOf' operand must be the name of a method (without parentheses).



I have updated my post above. Please have a look.


Thank you.



Unfortunately the focus still remains on the first dropdownlist, even after I select a different dropdownlist.


Any suggestions?



I think you might be setting the focus somewhere down the line in your code Anyway, just set the focus before the "End Sub".


Protected Sub BM_DDL_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles BM_DDL.SelectedIndexChanged
..............
..............
End Try
CType(sender,DropDownList).focus() End Sub

If above does not work. Could you post your aspx code.



That didn't work so here is my aspx page:


<%@ Page Language="VB" MasterPageFile="~/BOC.master" AutoEventWireup="True" CodeFile="GovernanceDocuments.aspx.vb" Inherits="_GovernanceDocuments" title="Governance Documents" %>
<%@ Register assembly="AjaxControlToolkit" namespace="AjaxControlToolkit" tagprefix="cc1" %>



Text="All Documents are 10MB or Less">







onclick="btnUpload_Click"
Text="Upload"/>
























AppendDataBoundItems="True" OnSelectedIndexChanged="DDL_SelectedIndexChanged">
AppendDataBoundItems="True" OnSelectedIndexChanged="DDL_SelectedIndexChanged">

AppendDataBoundItems="True" OnSelectedIndexChanged="DDL_SelectedIndexChanged">

AppendDataBoundItems="True" OnSelectedIndexChanged="DDL_SelectedIndexChanged">


AutoGenerateColumns="False"
onrowcommand="GridView1_RowCommand"
DataKeyNames="DocID" Width="300px">

InsertVisible="False"
ReadOnly="True"
SortExpression="DocID" Visible="False" />
HeaderText="Document Name"
SortExpression="DocName" ItemStyle-Wrap="False" />
HeaderText="Last Update"
SortExpression="Loaded" DataFormatString="{0:d}" />
HeaderText="Document Size in KB"
SortExpression="DocData" DataFormatString="{0:F2}" >




SortExpression="Type" Visible="False" />

ImageUrl="~/Images/download.png"
CommandName="Download"
HeaderText="Download File" >


ImageUrl="~/Images/Delete.jpg"
CommandName="DeleteRow"
HeaderText="Delete" />



AutoGenerateColumns="False"
onrowcommand="DM_Gridview_RowCommand"
DataKeyNames="DocID" Width="300px">

InsertVisible="False"
ReadOnly="True"
SortExpression="DocID" Visible="False" />
HeaderText="Document Name"
SortExpression="DocName" ItemStyle-Wrap="False" />
HeaderText="Last Update"
SortExpression="Loaded" DataFormatString="{0:d}" />
HeaderText="Document Size in KB"
SortExpression="DocData" DataFormatString="{0:F2}" >




SortExpression="Type" Visible="False" />

ImageUrl="~/Images/download.png"
CommandName="Download"
HeaderText="Download File" >


ImageUrl="~/Images/Delete.jpg"
CommandName="DeleteRow"
HeaderText="Delete" />



AutoGenerateColumns="False"
onrowcommand="G_Gridview_RowCommand"
DataKeyNames="DocID" Width="300px">

InsertVisible="False"
ReadOnly="True"
SortExpression="DocID" Visible="False" />
HeaderText="Document Name"
SortExpression="DocName" ItemStyle-Wrap="False" />
HeaderText="Last Update"
SortExpression="Loaded" DataFormatString="{0:d}" />
HeaderText="Document Size in KB"
SortExpression="DocData" DataFormatString="{0:F2}" >




SortExpression="Type" Visible="False" />

ImageUrl="~/Images/download.png"
CommandName="Download"
HeaderText="Download File" >


ImageUrl="~/Images/Delete.jpg"
CommandName="DeleteRow"
HeaderText="Delete" />



AutoGenerateColumns="False"
onrowcommand="R_Gridview_RowCommand"
DataKeyNames="DocID" Width="300px">

InsertVisible="False"
ReadOnly="True"
SortExpression="DocID" Visible="False" />
HeaderText="Document Name"
SortExpression="DocName" ItemStyle-Wrap="False" />
HeaderText="Last Update"
SortExpression="Loaded" DataFormatString="{0:d}" />
HeaderText="Document Size in KB"
SortExpression="DocData" DataFormatString="{0:F2}" >




SortExpression="Type" Visible="False" />

ImageUrl="~/Images/download.png"
CommandName="Download"
HeaderText="Download File" >


ImageUrl="~/Images/Delete.jpg"
CommandName="DeleteRow"
HeaderText="Delete" />

































AppendDataBoundItems="True" OnSelectedIndexChanged="DDL_SelectedIndexChanged">


AppendDataBoundItems="True" OnSelectedIndexChanged="DDL_SelectedIndexChanged">



AutoGenerateColumns="False"
onrowcommand="Po_Gridview_RowCommand"
DataKeyNames="DocID" Width="300px">

InsertVisible="False"
ReadOnly="True"
SortExpression="DocID" Visible="False" />
HeaderText="Document Name"
SortExpression="DocName" ItemStyle-Wrap="False" />
HeaderText="Last Update"
SortExpression="Loaded" DataFormatString="{0:d}" />
HeaderText="Document Size in KB"
SortExpression="DocData" DataFormatString="{0:F2}" >




SortExpression="Type" Visible="False" />

ImageUrl="~/Images/download.png"
CommandName="Download"
HeaderText="Download File" >


ImageUrl="~/Images/Delete.jpg"
CommandName="DeleteRow"
HeaderText="Delete" />



AutoGenerateColumns="False"
onrowcommand="MM_Gridview_RowCommand"
DataKeyNames="DocID" Width="300px">

InsertVisible="False"
ReadOnly="True"
SortExpression="DocID" Visible="False" />
HeaderText="Document Name"
SortExpression="DocName" ItemStyle-Wrap="False" />
HeaderText="Last Update"
SortExpression="Loaded" DataFormatString="{0:d}" />
HeaderText="Document Size in KB"
SortExpression="DocData" DataFormatString="{0:F2}" >




SortExpression="Type" Visible="False" />

ImageUrl="~/Images/download.png"
CommandName="Download"
HeaderText="Download File" >


ImageUrl="~/Images/Delete.jpg"
CommandName="DeleteRow"
HeaderText="Delete" />







HI,


On which method to use bind dropdownlist?


Please put your bind code.


I think you have not check Page.IsPostBack. at a binding time.




I have a sub-routine called initializedatasource() upone Page_Load.


Here is the dropdownlists databind code:



Private sub InitializeDatasource()

'Populate Board Management (BM_DDL) dropdownlist.
SQLSelectStr = "Select Left(docname,LEN(docname)-4) as BM_Name from Governance_Documents where DocName like 'BMC%' order by BM_Name"
Sqlcommand.CommandType = CommandType.Text
Sqlcommand.CommandText = SQLSelectStr
Sqlcommand.Connection = Gov_conn

Gov_conn.Close()
Gov_conn.Open()
BM_DDL.DataSource = Sqlcommand.ExecuteReader()
BM_DDL.DataTextField = "BM_Name"
BM_DDL.DataValueField = "BM_Name"
BM_DDL.Items.Insert(0, New ListItem("---Select Board Management---", String.Empty))
BM_DDL.DataBind()
Gov_conn.Close()

'Populate Delegations to Management (DM_DDL) dropdownlist.
SQLSelectStr = "Select Left(docname,LEN(docname)-4) as DM_Name,cast(substring(Left(docname,LEN(docname)-4),5,2) as integer) as DM_Order from Governance_Documents where DocName like 'DM%' order by DM_Order"
Sqlcommand.CommandType = CommandType.Text
Sqlcommand.CommandText = SQLSelectStr
Sqlcommand.Connection = Gov_conn

Gov_conn.Close()
Gov_conn.Open()
DM_DDL.DataSource = Sqlcommand.ExecuteReader()
DM_DDL.DataTextField = "DM_Name"
DM_DDL.DataValueField = "DM_Name"
DM_DDL.Items.Insert(0, New ListItem("---Select Delegation to Management---", String.Empty))
DM_DDL.DataBind()
Gov_conn.Close()

'Populate Governance (G_DDL) dropdownlist.
SQLSelectStr = "Select Left(docname,LEN(docname)-4) as G_Name,cast(substring(Left(docname,LEN(docname)-4),5,1) as integer) as G_Order from Governance_Documents where DocName like 'G%' order by G_Order"
Sqlcommand.CommandType = CommandType.Text
Sqlcommand.CommandText = SQLSelectStr
Sqlcommand.Connection = Gov_conn

Gov_conn.Close()
Gov_conn.Open()
G_DDL.DataSource = Sqlcommand.ExecuteReader()
G_DDL.DataTextField = "G_Name"
G_DDL.DataValueField = "G_Name"
G_DDL.Items.Insert(0, New ListItem("---Select Governance---", String.Empty))
G_DDL.DataBind()
Gov_conn.Close()

'Populate Results (R_DDL) dropdownlist.
SQLSelectStr = "Select Left(docname,LEN(docname)-4) as R_Name,cast(substring(Left(docname,LEN(docname)-4),5,1) as integer) as R_Order from Governance_Documents where DocName like 'R%' order by R_Order"
Sqlcommand.CommandType = CommandType.Text
Sqlcommand.CommandText = SQLSelectStr
Sqlcommand.Connection = Gov_conn

Gov_conn.Close()
Gov_conn.Open()
R_DDL.DataSource = Sqlcommand.ExecuteReader()
R_DDL.DataTextField = "R_Name"
R_DDL.DataValueField = "R_Name"
R_DDL.Items.Insert(0, New ListItem("---Select Result---", String.Empty))
R_DDL.DataBind()
Gov_conn.Close()

'Populate Policies (Policies_DDL) dropdownlist.
SQLSelectStr = "Select DocName from Governance_Documents where DocName like 'A%' or DocName like 'E%' or DocName like 'M%' order by DocName"
Sqlcommand.CommandType = CommandType.Text
Sqlcommand.CommandText = SQLSelectStr
Sqlcommand.Connection = Gov_conn

Gov_conn.Close()
Gov_conn.Open()
Policies_DDL.DataSource = Sqlcommand.ExecuteReader()
Policies_DDL.DataTextField = "DocName"
Policies_DDL.DataValueField = "DocName"
Policies_DDL.Items.Insert(0, New ListItem("---Select Policy---", String.Empty))
Policies_DDL.DataBind()
Gov_conn.Close()

currentYear = Now.AddYears(0).ToString("yy")
Lastyear = Now.AddYears(-1).ToString("yy")
'Populate Policies (Policies_DDL) dropdownlist.
SQLSelectStr = "Select DocID,DocName, DocData = datalength(docData)*.001, Loaded from Governance_Documents where "
SQLSelectStr = SQLSelectStr + "DocName like '" + Lastyear + "%' or DocName like '" + currentYear + "%' order by DocName desc"
Sqlcommand.CommandType = CommandType.Text
Sqlcommand.CommandText = SQLSelectStr
Sqlcommand.Connection = Gov_conn

Gov_conn.Close()
Gov_conn.Open()
MeetingMinutes_DDL.DataSource = Sqlcommand.ExecuteReader()
MeetingMinutes_DDL.DataTextField = "DocName"
MeetingMinutes_DDL.DataValueField = "DocName"
MeetingMinutes_DDL.Items.Insert(0, New ListItem("---Select Meeting Minute---", String.Empty))
MeetingMinutes_DDL.DataBind()
Gov_conn.Close()

End sub



Could you confirm that you are calling a initializedatasource() function as below,


If not Page.IsPostBack() Then
initializedatasource()
End If

Could you post your DDL_SelectedIndexChanged function code.


Also, where are you setting the filename property value in your function (I can't find it in your code)?



I am calling the Initializedatasource(), If not page.IsPostBack.


Here is the DDL_SelectedIndexChanged function.


Protected Sub DDL_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs)
Try
Dim SIV As String = CType(sender, DropDownList).SelectedValue
Dim fileName As String = ""
Dim fileExtention As String = ".pdf"
SQLSelectStr = "Select Left(docname,LEN(docname)-4) as DocName,DocData FROM Governance_Documents WHERE DocName = '" & SIV + ".pdf'"
Dim cmd As New SqlCommand(SQLSelectStr, Gov_conn)
Gov_conn.Close()
Gov_conn.Open()
Dim dReader As SqlDataReader = cmd.ExecuteReader()
Dim dt As DataTable = New DataTable
dt.Load(dReader)
Dim bytes() As Byte = CType(dt.Rows(0)("DocData"), Byte())
Response.Buffer = True
Response.Charset = ""
Response.Cache.SetCacheability(HttpCacheability.NoCache)
Response.ContentType = ReturnExtension(fileExtention)
Response.AddHeader("Content-Disposition", "attachment; filename=" + fileName)
Response.BinaryWrite(bytes)
Response.Flush()
Response.Close()
' Response.End()
Catch ex As Exception
End Try

CType(sender, DropDownList).Focus()
End Sub



You have missed out "Handles" part (Please check my previous post),


From:


Protected Sub DDL_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs)

To:


Protected Sub DDL_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles BM_DDL.SelectedIndexChanged



That is basically taking me back to where we started from.  With each DropDownlist having it's own SelectedIndexChanged. 


I tried your code exactly as you put it and it made no difference.



Anyone else got any ideas as to how I can do what I am asking?


[RESOLVED] is this possible to create Excel using itextsharp as PDF


hi,


i have code to write an pdf doc using itextsharp..i need same for excel...i tried to pass it to excel but its not working.?


any solution ??



No - iTextSharp cannot convert Excel to PDF format. 


[RESOLVED] How do I reset Dropdownlist to default or lose focus after SelectedIndexChanged


I am using a dropdownlists to display and on selectedindexchange download specific PDF files that are stored in my SQL Server table.   I have 6 dropdownlists that display different PDF's groups, If I select the first dropdownlist box it downloads the appropriate
PDF.  If I go to one of my other dropdownlist boxes and select a PDF file from it it downloads the file from the first dropdownlist.


So my thinking is that I have to reset each dropdownlist to default upon selectedindexchanged or tell the page to lose focus on the dropdownlist upon completion for the selectedindexchanged event.


Any suggestions would be greatly appreciated. 



Can you post the code ?



Here is the code for my first dropdownlist:


Protected Sub BM_DDL_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles BM_DDL.SelectedIndexChanged
Try

Dim fileName As String = ""
Dim fileExtention As String = ".pdf"
SQLSelectStr = "Select Left(docname,LEN(docname)-4) as DocName,DocData FROM Documents WHERE DocName = '" & BM_DDL.SelectedValue + ".pdf'"
Dim cmd As New SqlCommand(SQLSelectStr, Gov_conn)
Gov_conn.Close()
Gov_conn.Open()
Dim dReader As SqlDataReader = cmd.ExecuteReader()
Dim dt As DataTable = New DataTable
dt.Load(dReader)
Dim bytes() As Byte = CType(dt.Rows(0)("DocData"), Byte())
Response.Buffer = True
Response.Charset = ""
Response.Cache.SetCacheability(HttpCacheability.NoCache)
Response.ContentType = ReturnExtension(fileExtention)
Response.AddHeader("Content-Disposition", "attachment; filename=" + fileName)
Response.BinaryWrite(bytes)
Response.Flush()
Response.Close()
' Response.End()

Gov_conn.Close()
Catch generatedExceptionName As System.Threading.ThreadAbortException
Console.WriteLine(generatedExceptionName)
End Try

End Sub

Any help would be greatly appreciated.



Hi,


set drop down index to  zero after selected index event(if you want to reset the value of drop down)


for ex :      BM_DDL.SelectedIndex = 0 ( please correct me if I understood wrongly).



I've tried that, it isn't changing anything and the focus is still on the first dropdownlist.  This is why I posted the question on here.  I've fought for the last few days trying to get this to work.



Unfortunaltely i've already tried that approach.  It should be making a difference or at least do something however it is not.



You could follow the steps below to simplify your steps,







Protected Sub BM_DDL_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles BM_DDL.SelectedIndexChanged
Try
Dim SelectedItemValue As String = CType(sender,DropDownList).SelectedValue

Dim fileName As String = ""
Dim fileExtention As String = ".pdf"
SQLSelectStr = "Select Left(docname,LEN(docname)-4) as DocName,DocData FROM Documents WHERE DocName = '" & SelectedItemValue + ".pdf'"
.........
.........
End Try
End Sub



That seems like a good option.  However it keeps on giving me this error.


Compiler Error Message: BC30577: 'AddressOf' operand must be the name of a method (without parentheses).



I have updated my post above. Please have a look.


Thank you.



Unfortunately the focus still remains on the first dropdownlist, even after I select a different dropdownlist.


Any suggestions?



I think you might be setting the focus somewhere down the line in your code Anyway, just set the focus before the "End Sub".


Protected Sub BM_DDL_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles BM_DDL.SelectedIndexChanged
..............
..............
End Try
CType(sender,DropDownList).focus() End Sub

If above does not work. Could you post your aspx code.



That didn't work so here is my aspx page:


<%@ Page Language="VB" MasterPageFile="~/BOC.master" AutoEventWireup="True" CodeFile="GovernanceDocuments.aspx.vb" Inherits="_GovernanceDocuments" title="Governance Documents" %>
<%@ Register assembly="AjaxControlToolkit" namespace="AjaxControlToolkit" tagprefix="cc1" %>



Text="All Documents are 10MB or Less">







onclick="btnUpload_Click"
Text="Upload"/>
























AppendDataBoundItems="True" OnSelectedIndexChanged="DDL_SelectedIndexChanged">
AppendDataBoundItems="True" OnSelectedIndexChanged="DDL_SelectedIndexChanged">

AppendDataBoundItems="True" OnSelectedIndexChanged="DDL_SelectedIndexChanged">

AppendDataBoundItems="True" OnSelectedIndexChanged="DDL_SelectedIndexChanged">


AutoGenerateColumns="False"
onrowcommand="GridView1_RowCommand"
DataKeyNames="DocID" Width="300px">

InsertVisible="False"
ReadOnly="True"
SortExpression="DocID" Visible="False" />
HeaderText="Document Name"
SortExpression="DocName" ItemStyle-Wrap="False" />
HeaderText="Last Update"
SortExpression="Loaded" DataFormatString="{0:d}" />
HeaderText="Document Size in KB"
SortExpression="DocData" DataFormatString="{0:F2}" >




SortExpression="Type" Visible="False" />

ImageUrl="~/Images/download.png"
CommandName="Download"
HeaderText="Download File" >


ImageUrl="~/Images/Delete.jpg"
CommandName="DeleteRow"
HeaderText="Delete" />



AutoGenerateColumns="False"
onrowcommand="DM_Gridview_RowCommand"
DataKeyNames="DocID" Width="300px">

InsertVisible="False"
ReadOnly="True"
SortExpression="DocID" Visible="False" />
HeaderText="Document Name"
SortExpression="DocName" ItemStyle-Wrap="False" />
HeaderText="Last Update"
SortExpression="Loaded" DataFormatString="{0:d}" />
HeaderText="Document Size in KB"
SortExpression="DocData" DataFormatString="{0:F2}" >




SortExpression="Type" Visible="False" />

ImageUrl="~/Images/download.png"
CommandName="Download"
HeaderText="Download File" >


ImageUrl="~/Images/Delete.jpg"
CommandName="DeleteRow"
HeaderText="Delete" />



AutoGenerateColumns="False"
onrowcommand="G_Gridview_RowCommand"
DataKeyNames="DocID" Width="300px">

InsertVisible="False"
ReadOnly="True"
SortExpression="DocID" Visible="False" />
HeaderText="Document Name"
SortExpression="DocName" ItemStyle-Wrap="False" />
HeaderText="Last Update"
SortExpression="Loaded" DataFormatString="{0:d}" />
HeaderText="Document Size in KB"
SortExpression="DocData" DataFormatString="{0:F2}" >




SortExpression="Type" Visible="False" />

ImageUrl="~/Images/download.png"
CommandName="Download"
HeaderText="Download File" >


ImageUrl="~/Images/Delete.jpg"
CommandName="DeleteRow"
HeaderText="Delete" />



AutoGenerateColumns="False"
onrowcommand="R_Gridview_RowCommand"
DataKeyNames="DocID" Width="300px">

InsertVisible="False"
ReadOnly="True"
SortExpression="DocID" Visible="False" />
HeaderText="Document Name"
SortExpression="DocName" ItemStyle-Wrap="False" />
HeaderText="Last Update"
SortExpression="Loaded" DataFormatString="{0:d}" />
HeaderText="Document Size in KB"
SortExpression="DocData" DataFormatString="{0:F2}" >




SortExpression="Type" Visible="False" />

ImageUrl="~/Images/download.png"
CommandName="Download"
HeaderText="Download File" >


ImageUrl="~/Images/Delete.jpg"
CommandName="DeleteRow"
HeaderText="Delete" />

































AppendDataBoundItems="True" OnSelectedIndexChanged="DDL_SelectedIndexChanged">


AppendDataBoundItems="True" OnSelectedIndexChanged="DDL_SelectedIndexChanged">



AutoGenerateColumns="False"
onrowcommand="Po_Gridview_RowCommand"
DataKeyNames="DocID" Width="300px">

InsertVisible="False"
ReadOnly="True"
SortExpression="DocID" Visible="False" />
HeaderText="Document Name"
SortExpression="DocName" ItemStyle-Wrap="False" />
HeaderText="Last Update"
SortExpression="Loaded" DataFormatString="{0:d}" />
HeaderText="Document Size in KB"
SortExpression="DocData" DataFormatString="{0:F2}" >




SortExpression="Type" Visible="False" />

ImageUrl="~/Images/download.png"
CommandName="Download"
HeaderText="Download File" >


ImageUrl="~/Images/Delete.jpg"
CommandName="DeleteRow"
HeaderText="Delete" />



AutoGenerateColumns="False"
onrowcommand="MM_Gridview_RowCommand"
DataKeyNames="DocID" Width="300px">

InsertVisible="False"
ReadOnly="True"
SortExpression="DocID" Visible="False" />
HeaderText="Document Name"
SortExpression="DocName" ItemStyle-Wrap="False" />
HeaderText="Last Update"
SortExpression="Loaded" DataFormatString="{0:d}" />
HeaderText="Document Size in KB"
SortExpression="DocData" DataFormatString="{0:F2}" >




SortExpression="Type" Visible="False" />

ImageUrl="~/Images/download.png"
CommandName="Download"
HeaderText="Download File" >


ImageUrl="~/Images/Delete.jpg"
CommandName="DeleteRow"
HeaderText="Delete" />







HI,


On which method to use bind dropdownlist?


Please put your bind code.


I think you have not check Page.IsPostBack. at a binding time.




I have a sub-routine called initializedatasource() upone Page_Load.


Here is the dropdownlists databind code:



Private sub InitializeDatasource()

'Populate Board Management (BM_DDL) dropdownlist.
SQLSelectStr = "Select Left(docname,LEN(docname)-4) as BM_Name from Governance_Documents where DocName like 'BMC%' order by BM_Name"
Sqlcommand.CommandType = CommandType.Text
Sqlcommand.CommandText = SQLSelectStr
Sqlcommand.Connection = Gov_conn

Gov_conn.Close()
Gov_conn.Open()
BM_DDL.DataSource = Sqlcommand.ExecuteReader()
BM_DDL.DataTextField = "BM_Name"
BM_DDL.DataValueField = "BM_Name"
BM_DDL.Items.Insert(0, New ListItem("---Select Board Management---", String.Empty))
BM_DDL.DataBind()
Gov_conn.Close()

'Populate Delegations to Management (DM_DDL) dropdownlist.
SQLSelectStr = "Select Left(docname,LEN(docname)-4) as DM_Name,cast(substring(Left(docname,LEN(docname)-4),5,2) as integer) as DM_Order from Governance_Documents where DocName like 'DM%' order by DM_Order"
Sqlcommand.CommandType = CommandType.Text
Sqlcommand.CommandText = SQLSelectStr
Sqlcommand.Connection = Gov_conn

Gov_conn.Close()
Gov_conn.Open()
DM_DDL.DataSource = Sqlcommand.ExecuteReader()
DM_DDL.DataTextField = "DM_Name"
DM_DDL.DataValueField = "DM_Name"
DM_DDL.Items.Insert(0, New ListItem("---Select Delegation to Management---", String.Empty))
DM_DDL.DataBind()
Gov_conn.Close()

'Populate Governance (G_DDL) dropdownlist.
SQLSelectStr = "Select Left(docname,LEN(docname)-4) as G_Name,cast(substring(Left(docname,LEN(docname)-4),5,1) as integer) as G_Order from Governance_Documents where DocName like 'G%' order by G_Order"
Sqlcommand.CommandType = CommandType.Text
Sqlcommand.CommandText = SQLSelectStr
Sqlcommand.Connection = Gov_conn

Gov_conn.Close()
Gov_conn.Open()
G_DDL.DataSource = Sqlcommand.ExecuteReader()
G_DDL.DataTextField = "G_Name"
G_DDL.DataValueField = "G_Name"
G_DDL.Items.Insert(0, New ListItem("---Select Governance---", String.Empty))
G_DDL.DataBind()
Gov_conn.Close()

'Populate Results (R_DDL) dropdownlist.
SQLSelectStr = "Select Left(docname,LEN(docname)-4) as R_Name,cast(substring(Left(docname,LEN(docname)-4),5,1) as integer) as R_Order from Governance_Documents where DocName like 'R%' order by R_Order"
Sqlcommand.CommandType = CommandType.Text
Sqlcommand.CommandText = SQLSelectStr
Sqlcommand.Connection = Gov_conn

Gov_conn.Close()
Gov_conn.Open()
R_DDL.DataSource = Sqlcommand.ExecuteReader()
R_DDL.DataTextField = "R_Name"
R_DDL.DataValueField = "R_Name"
R_DDL.Items.Insert(0, New ListItem("---Select Result---", String.Empty))
R_DDL.DataBind()
Gov_conn.Close()

'Populate Policies (Policies_DDL) dropdownlist.
SQLSelectStr = "Select DocName from Governance_Documents where DocName like 'A%' or DocName like 'E%' or DocName like 'M%' order by DocName"
Sqlcommand.CommandType = CommandType.Text
Sqlcommand.CommandText = SQLSelectStr
Sqlcommand.Connection = Gov_conn

Gov_conn.Close()
Gov_conn.Open()
Policies_DDL.DataSource = Sqlcommand.ExecuteReader()
Policies_DDL.DataTextField = "DocName"
Policies_DDL.DataValueField = "DocName"
Policies_DDL.Items.Insert(0, New ListItem("---Select Policy---", String.Empty))
Policies_DDL.DataBind()
Gov_conn.Close()

currentYear = Now.AddYears(0).ToString("yy")
Lastyear = Now.AddYears(-1).ToString("yy")
'Populate Policies (Policies_DDL) dropdownlist.
SQLSelectStr = "Select DocID,DocName, DocData = datalength(docData)*.001, Loaded from Governance_Documents where "
SQLSelectStr = SQLSelectStr + "DocName like '" + Lastyear + "%' or DocName like '" + currentYear + "%' order by DocName desc"
Sqlcommand.CommandType = CommandType.Text
Sqlcommand.CommandText = SQLSelectStr
Sqlcommand.Connection = Gov_conn

Gov_conn.Close()
Gov_conn.Open()
MeetingMinutes_DDL.DataSource = Sqlcommand.ExecuteReader()
MeetingMinutes_DDL.DataTextField = "DocName"
MeetingMinutes_DDL.DataValueField = "DocName"
MeetingMinutes_DDL.Items.Insert(0, New ListItem("---Select Meeting Minute---", String.Empty))
MeetingMinutes_DDL.DataBind()
Gov_conn.Close()

End sub



Could you confirm that you are calling a initializedatasource() function as below,


If not Page.IsPostBack() Then
initializedatasource()
End If

Could you post your DDL_SelectedIndexChanged function code.


Also, where are you setting the filename property value in your function (I can't find it in your code)?



I am calling the Initializedatasource(), If not page.IsPostBack.


Here is the DDL_SelectedIndexChanged function.


Protected Sub DDL_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs)
Try
Dim SIV As String = CType(sender, DropDownList).SelectedValue
Dim fileName As String = ""
Dim fileExtention As String = ".pdf"
SQLSelectStr = "Select Left(docname,LEN(docname)-4) as DocName,DocData FROM Governance_Documents WHERE DocName = '" & SIV + ".pdf'"
Dim cmd As New SqlCommand(SQLSelectStr, Gov_conn)
Gov_conn.Close()
Gov_conn.Open()
Dim dReader As SqlDataReader = cmd.ExecuteReader()
Dim dt As DataTable = New DataTable
dt.Load(dReader)
Dim bytes() As Byte = CType(dt.Rows(0)("DocData"), Byte())
Response.Buffer = True
Response.Charset = ""
Response.Cache.SetCacheability(HttpCacheability.NoCache)
Response.ContentType = ReturnExtension(fileExtention)
Response.AddHeader("Content-Disposition", "attachment; filename=" + fileName)
Response.BinaryWrite(bytes)
Response.Flush()
Response.Close()
' Response.End()
Catch ex As Exception
End Try

CType(sender, DropDownList).Focus()
End Sub



You have missed out "Handles" part (Please check my previous post),


From:


Protected Sub DDL_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs)

To:


Protected Sub DDL_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles BM_DDL.SelectedIndexChanged



That is basically taking me back to where we started from.  With each DropDownlist having it's own SelectedIndexChanged. 


I tried your code exactly as you put it and it made no difference.



Anyone else got any ideas as to how I can do what I am asking?


[RESOLVED] is this possible to create Excel using itextsharp as PDF


hi,


i have code to write an pdf doc using itextsharp..i need same for excel...i tried to pass it to excel but its not working.?


any solution ??



No - iTextSharp cannot convert Excel to PDF format. 


[RESOLVED] How do I reset Dropdownlist to default or lose focus after SelectedIndexChanged


I am using a dropdownlists to display and on selectedindexchange download specific PDF files that are stored in my SQL Server table.   I have 6 dropdownlists that display different PDF's groups, If I select the first dropdownlist box it downloads the appropriate
PDF.  If I go to one of my other dropdownlist boxes and select a PDF file from it it downloads the file from the first dropdownlist.


So my thinking is that I have to reset each dropdownlist to default upon selectedindexchanged or tell the page to lose focus on the dropdownlist upon completion for the selectedindexchanged event.


Any suggestions would be greatly appreciated. 



Can you post the code ?



Here is the code for my first dropdownlist:


Protected Sub BM_DDL_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles BM_DDL.SelectedIndexChanged
Try

Dim fileName As String = ""
Dim fileExtention As String = ".pdf"
SQLSelectStr = "Select Left(docname,LEN(docname)-4) as DocName,DocData FROM Documents WHERE DocName = '" & BM_DDL.SelectedValue + ".pdf'"
Dim cmd As New SqlCommand(SQLSelectStr, Gov_conn)
Gov_conn.Close()
Gov_conn.Open()
Dim dReader As SqlDataReader = cmd.ExecuteReader()
Dim dt As DataTable = New DataTable
dt.Load(dReader)
Dim bytes() As Byte = CType(dt.Rows(0)("DocData"), Byte())
Response.Buffer = True
Response.Charset = ""
Response.Cache.SetCacheability(HttpCacheability.NoCache)
Response.ContentType = ReturnExtension(fileExtention)
Response.AddHeader("Content-Disposition", "attachment; filename=" + fileName)
Response.BinaryWrite(bytes)
Response.Flush()
Response.Close()
' Response.End()

Gov_conn.Close()
Catch generatedExceptionName As System.Threading.ThreadAbortException
Console.WriteLine(generatedExceptionName)
End Try

End Sub

Any help would be greatly appreciated.



Hi,


set drop down index to  zero after selected index event(if you want to reset the value of drop down)


for ex :      BM_DDL.SelectedIndex = 0 ( please correct me if I understood wrongly).



I've tried that, it isn't changing anything and the focus is still on the first dropdownlist.  This is why I posted the question on here.  I've fought for the last few days trying to get this to work.



Unfortunaltely i've already tried that approach.  It should be making a difference or at least do something however it is not.



You could follow the steps below to simplify your steps,







Protected Sub BM_DDL_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles BM_DDL.SelectedIndexChanged
Try
Dim SelectedItemValue As String = CType(sender,DropDownList).SelectedValue

Dim fileName As String = ""
Dim fileExtention As String = ".pdf"
SQLSelectStr = "Select Left(docname,LEN(docname)-4) as DocName,DocData FROM Documents WHERE DocName = '" & SelectedItemValue + ".pdf'"
.........
.........
End Try
End Sub



That seems like a good option.  However it keeps on giving me this error.


Compiler Error Message: BC30577: 'AddressOf' operand must be the name of a method (without parentheses).



I have updated my post above. Please have a look.


Thank you.



Unfortunately the focus still remains on the first dropdownlist, even after I select a different dropdownlist.


Any suggestions?



I think you might be setting the focus somewhere down the line in your code Anyway, just set the focus before the "End Sub".


Protected Sub BM_DDL_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles BM_DDL.SelectedIndexChanged
..............
..............
End Try
CType(sender,DropDownList).focus() End Sub

If above does not work. Could you post your aspx code.



That didn't work so here is my aspx page:


<%@ Page Language="VB" MasterPageFile="~/BOC.master" AutoEventWireup="True" CodeFile="GovernanceDocuments.aspx.vb" Inherits="_GovernanceDocuments" title="Governance Documents" %>
<%@ Register assembly="AjaxControlToolkit" namespace="AjaxControlToolkit" tagprefix="cc1" %>



Text="All Documents are 10MB or Less">







onclick="btnUpload_Click"
Text="Upload"/>
























AppendDataBoundItems="True" OnSelectedIndexChanged="DDL_SelectedIndexChanged">
AppendDataBoundItems="True" OnSelectedIndexChanged="DDL_SelectedIndexChanged">

AppendDataBoundItems="True" OnSelectedIndexChanged="DDL_SelectedIndexChanged">

AppendDataBoundItems="True" OnSelectedIndexChanged="DDL_SelectedIndexChanged">


AutoGenerateColumns="False"
onrowcommand="GridView1_RowCommand"
DataKeyNames="DocID" Width="300px">

InsertVisible="False"
ReadOnly="True"
SortExpression="DocID" Visible="False" />
HeaderText="Document Name"
SortExpression="DocName" ItemStyle-Wrap="False" />
HeaderText="Last Update"
SortExpression="Loaded" DataFormatString="{0:d}" />
HeaderText="Document Size in KB"
SortExpression="DocData" DataFormatString="{0:F2}" >




SortExpression="Type" Visible="False" />

ImageUrl="~/Images/download.png"
CommandName="Download"
HeaderText="Download File" >


ImageUrl="~/Images/Delete.jpg"
CommandName="DeleteRow"
HeaderText="Delete" />



AutoGenerateColumns="False"
onrowcommand="DM_Gridview_RowCommand"
DataKeyNames="DocID" Width="300px">

InsertVisible="False"
ReadOnly="True"
SortExpression="DocID" Visible="False" />
HeaderText="Document Name"
SortExpression="DocName" ItemStyle-Wrap="False" />
HeaderText="Last Update"
SortExpression="Loaded" DataFormatString="{0:d}" />
HeaderText="Document Size in KB"
SortExpression="DocData" DataFormatString="{0:F2}" >




SortExpression="Type" Visible="False" />

ImageUrl="~/Images/download.png"
CommandName="Download"
HeaderText="Download File" >


ImageUrl="~/Images/Delete.jpg"
CommandName="DeleteRow"
HeaderText="Delete" />



AutoGenerateColumns="False"
onrowcommand="G_Gridview_RowCommand"
DataKeyNames="DocID" Width="300px">

InsertVisible="False"
ReadOnly="True"
SortExpression="DocID" Visible="False" />
HeaderText="Document Name"
SortExpression="DocName" ItemStyle-Wrap="False" />
HeaderText="Last Update"
SortExpression="Loaded" DataFormatString="{0:d}" />
HeaderText="Document Size in KB"
SortExpression="DocData" DataFormatString="{0:F2}" >




SortExpression="Type" Visible="False" />

ImageUrl="~/Images/download.png"
CommandName="Download"
HeaderText="Download File" >


ImageUrl="~/Images/Delete.jpg"
CommandName="DeleteRow"
HeaderText="Delete" />



AutoGenerateColumns="False"
onrowcommand="R_Gridview_RowCommand"
DataKeyNames="DocID" Width="300px">

InsertVisible="False"
ReadOnly="True"
SortExpression="DocID" Visible="False" />
HeaderText="Document Name"
SortExpression="DocName" ItemStyle-Wrap="False" />
HeaderText="Last Update"
SortExpression="Loaded" DataFormatString="{0:d}" />
HeaderText="Document Size in KB"
SortExpression="DocData" DataFormatString="{0:F2}" >




SortExpression="Type" Visible="False" />

ImageUrl="~/Images/download.png"
CommandName="Download"
HeaderText="Download File" >


ImageUrl="~/Images/Delete.jpg"
CommandName="DeleteRow"
HeaderText="Delete" />

































AppendDataBoundItems="True" OnSelectedIndexChanged="DDL_SelectedIndexChanged">


AppendDataBoundItems="True" OnSelectedIndexChanged="DDL_SelectedIndexChanged">



AutoGenerateColumns="False"
onrowcommand="Po_Gridview_RowCommand"
DataKeyNames="DocID" Width="300px">

InsertVisible="False"
ReadOnly="True"
SortExpression="DocID" Visible="False" />
HeaderText="Document Name"
SortExpression="DocName" ItemStyle-Wrap="False" />
HeaderText="Last Update"
SortExpression="Loaded" DataFormatString="{0:d}" />
HeaderText="Document Size in KB"
SortExpression="DocData" DataFormatString="{0:F2}" >




SortExpression="Type" Visible="False" />

ImageUrl="~/Images/download.png"
CommandName="Download"
HeaderText="Download File" >


ImageUrl="~/Images/Delete.jpg"
CommandName="DeleteRow"
HeaderText="Delete" />



AutoGenerateColumns="False"
onrowcommand="MM_Gridview_RowCommand"
DataKeyNames="DocID" Width="300px">

InsertVisible="False"
ReadOnly="True"
SortExpression="DocID" Visible="False" />
HeaderText="Document Name"
SortExpression="DocName" ItemStyle-Wrap="False" />
HeaderText="Last Update"
SortExpression="Loaded" DataFormatString="{0:d}" />
HeaderText="Document Size in KB"
SortExpression="DocData" DataFormatString="{0:F2}" >




SortExpression="Type" Visible="False" />

ImageUrl="~/Images/download.png"
CommandName="Download"
HeaderText="Download File" >


ImageUrl="~/Images/Delete.jpg"
CommandName="DeleteRow"
HeaderText="Delete" />







HI,


On which method to use bind dropdownlist?


Please put your bind code.


I think you have not check Page.IsPostBack. at a binding time.




I have a sub-routine called initializedatasource() upone Page_Load.


Here is the dropdownlists databind code:



Private sub InitializeDatasource()

'Populate Board Management (BM_DDL) dropdownlist.
SQLSelectStr = "Select Left(docname,LEN(docname)-4) as BM_Name from Governance_Documents where DocName like 'BMC%' order by BM_Name"
Sqlcommand.CommandType = CommandType.Text
Sqlcommand.CommandText = SQLSelectStr
Sqlcommand.Connection = Gov_conn

Gov_conn.Close()
Gov_conn.Open()
BM_DDL.DataSource = Sqlcommand.ExecuteReader()
BM_DDL.DataTextField = "BM_Name"
BM_DDL.DataValueField = "BM_Name"
BM_DDL.Items.Insert(0, New ListItem("---Select Board Management---", String.Empty))
BM_DDL.DataBind()
Gov_conn.Close()

'Populate Delegations to Management (DM_DDL) dropdownlist.
SQLSelectStr = "Select Left(docname,LEN(docname)-4) as DM_Name,cast(substring(Left(docname,LEN(docname)-4),5,2) as integer) as DM_Order from Governance_Documents where DocName like 'DM%' order by DM_Order"
Sqlcommand.CommandType = CommandType.Text
Sqlcommand.CommandText = SQLSelectStr
Sqlcommand.Connection = Gov_conn

Gov_conn.Close()
Gov_conn.Open()
DM_DDL.DataSource = Sqlcommand.ExecuteReader()
DM_DDL.DataTextField = "DM_Name"
DM_DDL.DataValueField = "DM_Name"
DM_DDL.Items.Insert(0, New ListItem("---Select Delegation to Management---", String.Empty))
DM_DDL.DataBind()
Gov_conn.Close()

'Populate Governance (G_DDL) dropdownlist.
SQLSelectStr = "Select Left(docname,LEN(docname)-4) as G_Name,cast(substring(Left(docname,LEN(docname)-4),5,1) as integer) as G_Order from Governance_Documents where DocName like 'G%' order by G_Order"
Sqlcommand.CommandType = CommandType.Text
Sqlcommand.CommandText = SQLSelectStr
Sqlcommand.Connection = Gov_conn

Gov_conn.Close()
Gov_conn.Open()
G_DDL.DataSource = Sqlcommand.ExecuteReader()
G_DDL.DataTextField = "G_Name"
G_DDL.DataValueField = "G_Name"
G_DDL.Items.Insert(0, New ListItem("---Select Governance---", String.Empty))
G_DDL.DataBind()
Gov_conn.Close()

'Populate Results (R_DDL) dropdownlist.
SQLSelectStr = "Select Left(docname,LEN(docname)-4) as R_Name,cast(substring(Left(docname,LEN(docname)-4),5,1) as integer) as R_Order from Governance_Documents where DocName like 'R%' order by R_Order"
Sqlcommand.CommandType = CommandType.Text
Sqlcommand.CommandText = SQLSelectStr
Sqlcommand.Connection = Gov_conn

Gov_conn.Close()
Gov_conn.Open()
R_DDL.DataSource = Sqlcommand.ExecuteReader()
R_DDL.DataTextField = "R_Name"
R_DDL.DataValueField = "R_Name"
R_DDL.Items.Insert(0, New ListItem("---Select Result---", String.Empty))
R_DDL.DataBind()
Gov_conn.Close()

'Populate Policies (Policies_DDL) dropdownlist.
SQLSelectStr = "Select DocName from Governance_Documents where DocName like 'A%' or DocName like 'E%' or DocName like 'M%' order by DocName"
Sqlcommand.CommandType = CommandType.Text
Sqlcommand.CommandText = SQLSelectStr
Sqlcommand.Connection = Gov_conn

Gov_conn.Close()
Gov_conn.Open()
Policies_DDL.DataSource = Sqlcommand.ExecuteReader()
Policies_DDL.DataTextField = "DocName"
Policies_DDL.DataValueField = "DocName"
Policies_DDL.Items.Insert(0, New ListItem("---Select Policy---", String.Empty))
Policies_DDL.DataBind()
Gov_conn.Close()

currentYear = Now.AddYears(0).ToString("yy")
Lastyear = Now.AddYears(-1).ToString("yy")
'Populate Policies (Policies_DDL) dropdownlist.
SQLSelectStr = "Select DocID,DocName, DocData = datalength(docData)*.001, Loaded from Governance_Documents where "
SQLSelectStr = SQLSelectStr + "DocName like '" + Lastyear + "%' or DocName like '" + currentYear + "%' order by DocName desc"
Sqlcommand.CommandType = CommandType.Text
Sqlcommand.CommandText = SQLSelectStr
Sqlcommand.Connection = Gov_conn

Gov_conn.Close()
Gov_conn.Open()
MeetingMinutes_DDL.DataSource = Sqlcommand.ExecuteReader()
MeetingMinutes_DDL.DataTextField = "DocName"
MeetingMinutes_DDL.DataValueField = "DocName"
MeetingMinutes_DDL.Items.Insert(0, New ListItem("---Select Meeting Minute---", String.Empty))
MeetingMinutes_DDL.DataBind()
Gov_conn.Close()

End sub



Could you confirm that you are calling a initializedatasource() function as below,


If not Page.IsPostBack() Then
initializedatasource()
End If

Could you post your DDL_SelectedIndexChanged function code.


Also, where are you setting the filename property value in your function (I can't find it in your code)?



I am calling the Initializedatasource(), If not page.IsPostBack.


Here is the DDL_SelectedIndexChanged function.


Protected Sub DDL_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs)
Try
Dim SIV As String = CType(sender, DropDownList).SelectedValue
Dim fileName As String = ""
Dim fileExtention As String = ".pdf"
SQLSelectStr = "Select Left(docname,LEN(docname)-4) as DocName,DocData FROM Governance_Documents WHERE DocName = '" & SIV + ".pdf'"
Dim cmd As New SqlCommand(SQLSelectStr, Gov_conn)
Gov_conn.Close()
Gov_conn.Open()
Dim dReader As SqlDataReader = cmd.ExecuteReader()
Dim dt As DataTable = New DataTable
dt.Load(dReader)
Dim bytes() As Byte = CType(dt.Rows(0)("DocData"), Byte())
Response.Buffer = True
Response.Charset = ""
Response.Cache.SetCacheability(HttpCacheability.NoCache)
Response.ContentType = ReturnExtension(fileExtention)
Response.AddHeader("Content-Disposition", "attachment; filename=" + fileName)
Response.BinaryWrite(bytes)
Response.Flush()
Response.Close()
' Response.End()
Catch ex As Exception
End Try

CType(sender, DropDownList).Focus()
End Sub



You have missed out "Handles" part (Please check my previous post),


From:


Protected Sub DDL_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs)

To:


Protected Sub DDL_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles BM_DDL.SelectedIndexChanged



That is basically taking me back to where we started from.  With each DropDownlist having it's own SelectedIndexChanged. 


I tried your code exactly as you put it and it made no difference.



Anyone else got any ideas as to how I can do what I am asking?