I want to print the specific rows of the Gridview by storing the OrderID and ItemID concatenated together and adding them in an Arraylist.
Now I pass this ArrayList to a method in my DAL Class which returns a DataTable.
When I set the report.SetDataSource(dal.Print(printPage));
('where Print is a method that accepts the Arraylist printPage')
it only prints a blank report.
Here is the code I am doing upon the button click:
protected void Button1_Click(object sender, EventArgs e)
{
DispatchablePowerItems_DAL dal = new DispatchablePowerItems_DAL();
ArrayList printPage = new ArrayList();
string constr = "Server=.; Database=SupplyCM; uid=sa; pwd=123";
string query = "SELECT UserName, OrderID, ItemID, ItemName, Quantity, UnitPrice, Total FROM PowersDispatch";
SqlDataAdapter da = new SqlDataAdapter(query, constr);
ReportDocument report = new ReportDocument();
string reportFile = Server.MapPath("CrystalReportPower.rpt");
report.Load(reportFile);
int sum = 0;
string driverName;
foreach (GridViewRow row in GridView1.Rows)
{
if (row.RowType == DataControlRowType.DataRow)
{
if (TextBox1.Text == row.Cells[3].Text)
{
sum = sum + Convert.ToInt32(row.Cells[9].Text);
string orderId = row.Cells[4].Text;
string itemId = row.Cells[5].Text;
string orderIDitemID = orderId + "#" + itemId;
printPage.Add(orderIDitemID);
}
}
}
TextBox2.Text = sum.ToString();
report.SetDataSource(dal.Print(printPage));
TextBox1.Text = "";
TextBox2.Text = "";
TextBox3.Text = "";
report.PrintToPrinter(1, false, 0, 0);
}
The following is the code I am doing in my DAL Class:
public DataTable Print(System.Collections.ArrayList PageList)
{
string constr = "Server=.; Database=SupplyCM; uid=sa; pwd=123";
string query = "SELECT UserName, OrderID, ItemID, ItemName, Quantity, UnitPrice, Total FROM PowersDispatch Order By UserName, OrderID";
SqlConnection con = new SqlConnection(constr);
SqlCommand com = new SqlCommand(query, con);
SqlTransaction tr = null;
SqlDataAdapter da = new SqlDataAdapter(query, constr);
DataTable table = new DataTable();
da.Fill(table);
try
{
con.Open();
tr = con.BeginTransaction();
com.Transaction = tr;
com.Parameters.Add("@OrderID", SqlDbType.NVarChar);
com.Parameters.Add("@ItemID", SqlDbType.NVarChar);
foreach (object item in PageList)
{
string s = item.ToString();
string[] tokens = s.Split(new char[] { '#' });
string left = tokens[0];
string right = tokens[1];
com.Parameters["@OrderID"].Value = left;
com.Parameters["@ItemID"].Value = right;
com.ExecuteNonQuery();
}
tr.Commit();
return table;
}
catch (Exception ex)
{
tr.Rollback();
throw ex;
}
finally
{
con.Close();
}
}