Click here to Skip to main content
15,907,328 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
Friends, first of all many many thanks that you are helping me a lot
I get answer to all my posts
kindly help me in this scenario
code looks lengthy but please read and help me friends.....


current doubt scenario
In one of my page i found that
C#
-->a button is there....it generates report,,that is excel report is generated
-->dataset is used here

 protected void btnExcel_Click(object sender, EventArgs e)
    {
        try
        {
            if (Page.IsValid)
            {
                DataSet ds = new DataSet();
                // string strUserid = string.Empty;
                System.Data.DataTable dt = new System.Data.DataTable();
                    int company_id = Convert.ToInt32(Session[TMS.Common.CommonConstants.CONST_COMPANY_ID].ToString());
                    DateTime from_date = Convert.ToDateTime(CalendarControl_FromDate.TextDate.Text);
                    DateTime To_date = Convert.ToDateTime(CalendarControl_ToDate.TextDate.Text);
                    int hub_id = Convert.ToInt32(ddl_Hub.SelectedValue);
                   
                    
                    //string strRouteID = Convert.ToString(txtRouteID.Text);
                    //string strRouteType = string.Equals((ddlRouteType.SelectedValue), "-1") ? string.Empty : Convert.ToString(ddlRouteType.SelectedValue);
                   
                    //TMS.Business.Report.INT_NoShow objNoShow = new TMS.Business.Report.INT_NoShow();
                    //ds = objNoShow.NoShowData(company_id, strRouteID, from_date, To_date, strApptTime, strProjectCode, strRouteType, hub_id, intNumOfDays, strLoginID, strUserid);
                    TMS.Business.Report.INT_Adhoc_Capping obj = new TMS.Business.Report.INT_Adhoc_Capping();
                    ds = obj.Adhoc_Capping(company_id, from_date, To_date, hub_id);
                    if (isExecuteReportGeneration(ds))
                    {
                        dt = ds.Tables[0];
                        string attachment = "attachment; filename=Report_Adhoc_Capping.xls";
                        Response.ClearContent();
                        Response.AddHeader("content-disposition", attachment);
                        Response.ContentType = "application/vnd.ms-excel";
 
                        string tab = "";
 
                        foreach (DataColumn dc in dt.Columns)
                        {
 
                            Response.Write(tab + dc.ColumnName);
                            tab = "\t";
                        }
                        Response.Write("\n");
 
                        int i;
                        foreach (DataRow dr in dt.Rows)
                        {
                            tab = "";
                            for (i = 0; i < dt.Columns.Count; i++)
                            {
                                Response.Write(tab + dr[i].ToString());
                                tab = "\t";
                            }
                            Response.Write("\n");
                        }
 
                        //HttpContext.Current.ApplicationInstance.CompleteRequest();
                        Response.End();
                    }
 
                    else
                    {
                        ShowMessageBox("No Record(s) Found.");
                    }
                    //con.Close();
                }
             
        }
        catch (Exception ex)
        {
 
            // LogError.WriteError(ex);
 
        }
 
        finally
        {
 
        }
    }
 
 
    protected bool isExecuteReportGeneration(DataSet dsResult)
    {
        bool result = true;
        try
        {
 
 
            if (dsResult == null)
            {
                result = false;
            }
 
 
 
            if (dsResult.Tables.Count != 1)
            {
                result = false;
 
            }
            if (dsResult.Tables[0] == null)
            {
                result = false;
            }
            if (dsResult.Tables[0].Rows.Count == 0)
            {
                result = false;
            }
 
 
 
        }
 
        catch (Exception ex)
        {
            throw ex;
        }
        finally
        {
        }
        return result;
 
    }
 
 
}


--so in above code
C#
ds = obj.Adhoc_Capping(company_id, from_date, To_date, hub_id);

here i get the dataset from backend sp and that is used in the below function..and the report gets successfully generated if (isExecuteReportGeneration(ds))

-------------------------------------Doubt in second page--------------------------------------
here the scenario is
i have a search button ..upon search the data gets displayed....data comes through arraylist
if data is found the excelbutton visible becomes true,,,and upon click..the data which was displayed by search gets displayed in xcel........


C#
protected void ExportToExcel_Click(object sender, EventArgs e)

   {
       TMS.API.Schedule.VO_Schedule_SC obj_Schedule = new TMS.API.Schedule.VO_Schedule_SC();

       obj_Schedule.StartDate = DateTime.Parse(txt_From_date.Text).ToString("MM/dd/yyyy HH:mm:ss");

       obj_Schedule.EndDate = DateTime.Parse(txt_To_date.Text).ToString("MM/dd/yyyy HH:mm:ss");

       obj_Schedule.Hub_ID = ddl_Hub.SelectedValue;

       obj_Schedule.UserID = string.IsNullOrEmpty(txtEmployeeID.Text) ? string.Empty : txtEmployeeID.Text;

        INT_Schedule obj_Int_Schedule = new INT_Schedule();

       ArrayList dsRecords = obj_Int_Schedule.GetAllImportedUser_Records(obj_Schedule, PageToDisplay, PageSize);


now here data comes from arraylist---in 1st case it was coming from dataset
i want to use the code of 1st case---but here the data comes from arraylist rather than dataset
C#
ArrayList dsRecords = obj_Int_Schedule.GetAllImportedUser_Records(obj_Schedule, PageToDisplay, PageSize); 

dsRecods which is arraylist...if it can be converted to dataset so that i can use the code of first case-that is pass the dataset to the below function and my excel report will be generated.....

C#
if (isExecuteReportGeneration(dsRecords))

        {
            string attachment = "attachment; filename=Importeduserschedule.xls";
            Response.ClearContent();
            Response.AddHeader("content-disposition", attachment);
            Response.ContentType = "application/vnd.ms-excel";
            string tab = "";
            foreach (DataColumn dc in dt.Columns)
            {
                Response.Write(tab + dc.ColumnName);
                tab = "\t";
            }
            Response.Write("\n");
            int i;
            foreach (DataRow dr in dt.Rows)
            {
                tab = "";
                for (i = 0; i < dt.Columns.Count; i++)
                {
                    Response.Write(tab + dr[i].ToString());
                    tab = "\t";
                }
                Response.Write("\n");
            }
            //HttpContext.Current.ApplicationInstance.CompleteRequest();
            Response.End();
        }
        else
        {
            ShowMessageBox("No Record(s) Found.");
        }
    }
}

protected bool isExecuteReportGeneration(DataSet dsResult)
    {
        bool result = true;
        try
        {
            if (dsResult == null)
            {
                result = false;
            }
            if (dsResult.Tables.Count != 1)
            {
                result = false;
            }
            if (dsResult.Tables[0] == null)
            {
                result = false;
            }
            if (dsResult.Tables[0].Rows.Count == 0)
            {
                result = false;
            }
        }
        catch (Exception ex)
        {
            throw ex;
        }
        finally
        {
        }
        return result;
    }


kindly help me in this case
Posted
Updated 3-Jul-13 9:53am
v3

So I understand that you want to convert an ArrayList to a DataSet?
You can convert it into a DataTable as described here: http://tomgilkison.blogspot.com/2008/05/convert-arraylist-to-datatable.html[^]
Then add this DataTable into your DataSet
 
Share this answer
 
------------------yes i have done it--------------------:)-------------:)-------------------------
--this is how i am converting the arraylist to datatable-----------below function is called in button click of excelreport----then i am getting the datatable----using datatable i am generating the excel report--------

public DataTable ConvertArraylistToDataSet(ArrayList arrayList)
{

DataTable dtEntityReturned = new DataTable();

dtEntityReturned.Columns.Add("UserID", System.Type.GetType("System.String"));
dtEntityReturned.Columns.Add("ScheduleDate", System.Type.GetType("System.String"));
dtEntityReturned.Columns.Add("ShiftStartTime", System.Type.GetType("System.String"));
dtEntityReturned.Columns.Add("ShiftEndTime", System.Type.GetType("System.String"));
dtEntityReturned.Columns.Add("Creation_Date", System.Type.GetType("System.String"));
dtEntityReturned.Columns.Add("Cab_Pick", System.Type.GetType("System.String"));
dtEntityReturned.Columns.Add("Cab_Drop", System.Type.GetType("System.String"));
dtEntityReturned.Columns.Add("IsWeekend", System.Type.GetType("System.String"));
dtEntityReturned.Columns.Add("IsClientHoliday", System.Type.GetType("System.String"));
dtEntityReturned.Columns.Add("Remarks", System.Type.GetType("System.String"));
dtEntityReturned.Columns.Add("Shift_Type", System.Type.GetType("System.String"));


foreach (TMS.API.Schedule.VO_Schedule_SR objreqSR in arrayList)
{
DataRow myRow = dtEntityReturned.NewRow();

myRow["UserID"] = objreqSR.UserID;
myRow["ScheduleDate"] = objreqSR.ScheduleDate;
myRow["ShiftStartTime"] = objreqSR.ShiftStartTime;
myRow["ScheduleDate"] = objreqSR.ScheduleDate;
myRow["Creation_Date"] = objreqSR.Creation_Date;
myRow["Cab_Pick"] = objreqSR.Cab_Pick;
myRow["Cab_Drop"] = objreqSR.Cab_Drop;
myRow["IsWeekend"] = objreqSR.IsWeekend;
myRow["Remarks"] = objreqSR.Remarks;
myRow["Shift_Type"] = objreqSR.Shift_Type;


dtEntityReturned.Rows.Add(myRow);
}
return dtEntityReturned;
}



protected void ExportToExcel_Click(object sender, EventArgs e)
{

TMS.API.Schedule.VO_Schedule_SC obj_Schedule = new TMS.API.Schedule.VO_Schedule_SC();
obj_Schedule.StartDate = DateTime.Parse(txt_From_date.Text).ToString("MM/dd/yyyy HH:mm:ss");
obj_Schedule.EndDate = DateTime.Parse(txt_To_date.Text).ToString("MM/dd/yyyy HH:mm:ss");
obj_Schedule.Hub_ID = ddl_Hub.SelectedValue;
obj_Schedule.UserID = string.IsNullOrEmpty(txtEmployeeID.Text) ? string.Empty : txtEmployeeID.Text;
obj_Schedule.fullrecords="1";
INT_Schedule obj_Int_Schedule = new INT_Schedule();
ArrayList dsRecords = obj_Int_Schedule.GetAllImportedUser_Records(obj_Schedule, PageToDisplay, PageSize);
DataTable dsEntityReturned = ConvertArraylistToDataSet(dsRecords);
DataSet ds =new DataSet();

if (isExecuteReportGeneration(dsEntityReturned))
{

string attachment = "attachment; filename=Importeduserschedule.xls";
Response.ClearContent();
Response.AddHeader("content-disposition", attachment);
Response.ContentType = "application/vnd.ms-excel";

DataTable dt = null;
dt = dsEntityReturned;
string tab = "";

foreach (DataColumn dc in dt.Columns)
{

Response.Write(tab + dc.ColumnName);

tab = "\t";
}
Response.Write("\n");




int i;
foreach (DataRow dr in dt.Rows)
{
tab = "";
for (i = 0; i < dt.Columns.Count; i++)
{
Response.Write(tab + dr[i].ToString());
tab = "\t";
}
Response.Write("\n");
}

//HttpContext.Current.ApplicationInstance.CompleteRequest();
Response.End();
}

else
{
ShowMessageBox("No Record(s) Found.");
}



}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900