pooja nirmalkar

pooja nirmalkar

  • NA
  • 22
  • 7.5k

how to return datatable in one page to another page load

Mar 21 2016 1:42 AM
page1:-
public partial class Employee_Reports : System.Web.UI.Page
{
MySqlCommand cmd = new MySqlCommand();
MySqlConnection conn = new MySqlConnection();
MySqlDataReader reader = null;
string mobile, from_date, to_date;
string constr = ConfigurationManager.ConnectionStrings["haajiri"].ConnectionString;
protected void Page_Load(object sender, EventArgs e)
{
}
protected void btn_submit_Click(object sender, EventArgs e)
{
try
{
using (conn = new MySqlConnection(constr))
{
conn.Open();
cmd = new MySqlCommand("select EMP_LOG_DATE,EMP_LOG_DATE,EMP_MOBILE from empmobility ");
cmd.Parameters.AddWithValue("@EMP_MOBILE",txt_mobile.Text);
cmd.Parameters.AddWithValue("@EMP_FROM_DATE", txt_from_date.Text);
cmd.Parameters.AddWithValue("@EMP_TO_DATE", txt_to_date.Text);
cmd.ExecuteReader();
while (reader.Read())
{
mobile = reader["EMP_MOBILE"].ToString();
from_date = reader["EMP_LOG_DATE"].ToString();
to_date = reader["EMP_LOG_DATE"].ToString();
}
reader.Close();
if (txt_mobile.Text == mobile && txt_from_date.Text == from_date && txt_to_date.Text == to_date)
{
GetData(mobile,from_date,to_date);
}
else
{
}
}
}
catch(Exception ex)
{
throw;
}
}
public DataTable GetData(string mobile,string from_date,string to_date)
{
using (MySqlConnection con = new MySqlConnection(constr))
{
using (MySqlCommand cmd = new MySqlCommand("SELECT DATE_FORMAT(EMP_LOG_DATE,'%d/%m/%Y') FROM empmobility WHERE EMP_MOBILE="+"'"+ mobile +"'"+ "and EMP_LOG_DATE >=" + "'" + from_date + "'" + "and EMP_LOG_DATE <=" + "'" + to_date + "'"))
{
using (MySqlDataAdapter sda = new MySqlDataAdapter())
{
cmd.Connection = con;
sda.SelectCommand = cmd;
using (DataTable dt = new DataTable())
{
sda.Fill(dt);
return dt; //i need this return value on another page load
}
}
}
}
}
}
 
 
PAGE2:
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
//Populating a DataTable from database.
DataTable dt = this.GetData(); //ERROR
//Building an HTML string.
StringBuilder html = new StringBuilder();
//Table start.
html.Append("<table border = '1'>");
//Building the Header row.
html.Append("<tr>");
foreach (DataColumn column in dt.Columns)
{
html.Append("<th>");
//html.Append(column.ColumnName);
html.Append("Date");
html.Append("</th>");
}
html.Append("</tr>");
//Building the Data rows.
foreach (DataRow row in dt.Rows)
{
html.Append("<tr>");
foreach (DataColumn column in dt.Columns)
{
html.Append("<td>");
html.Append(row[column.ColumnName]);
html.Append("</td>");
}
html.Append("</tr>");
}
//Table end.
html.Append("</table>");
//Append the HTML string to Placeholder.
PlaceHolder1.Controls.Add(new Literal { Text = html.ToString() });
}
}
 
 

Answers (2)