using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Text;
using System.Text.RegularExpressions;
public partial class Pre_Owned_customasp_searchresult : System.Web.UI.UserControl
{
int count ;
int counter;
int display;
int pages;
int current2;
int start;
string check;
protected void Page_Load(object sender, EventArgs e)
{
if (HttpContext.Current.Request.QueryString[0] != null || HttpContext.Current.Request.QueryString[0] != "/Searchlist")
{
if ( HttpContext.Current.Request.QueryString[0] == "999"){
Continue_Search2();
List_Mysearch();
List_Mysearch3();
}
else
{
Continue_Search2();
List_Mysearch();
List_Mysearch2();
}
}
}
//protected void destroyme()
//{
// Session["var1"] = null;
// Session["var2"] = null;
// Session["var3"] = null;
// check = null;
//}
protected void Submit_Click(object sender, EventArgs e)
{
Session["year"] = yearsearch.SelectedValue;
Session["model"] = modelsearch.SelectedValue;
Session["location"] = locationsearch.SelectedValue;
Session["counter"] = 1;
//destroyme();
//Session["var1"] = yearsearch.SelectedValue;
//Session["var2"] = modelsearch.SelectedValue;
//Session["var3"] = locationsearch.SelectedValue;
// check = "&" + "var1=" + yearsearch.SelectedValue + "&" + "var2=" + modelsearch.SelectedValue + "&" + "var3=" + locationsearch.SelectedValue;
// Response.Redirect("~/searchlist.aspx?currentvalue=1" + check);
//Response.Redirect(Request.RawUrl);
Response.Redirect("~/searchlist.aspx?currentvalue=1");
}
//protected void Continue_Search(object sender, EventArgs e)
//{
// Session["counter"] = 3;
// Response.Redirect("~/searchlist.aspx");
//}
protected void Continue_Search2()
{
string haha = HttpContext.Current.Request.QueryString[0].ToString();
// UsernameLabel.Text = HttpContext.Current.Request.QueryString[0];
Session["counter"] = haha;
// Response.Redirect("~/searchlist.aspx");
string hehe = HttpContext.Current.Request.QueryString[1].ToString();
Session["display"] = hehe;
}
/*
protected void Continue_Search3()
{
Session["counter"] = 999;
} */
protected void List_Mysearch()
{
string yearvalue = (string)(Session["year"]);
string modelvalue = (string)(Session["model"]);
string locationvalue = (string)(Session["location"]);
/*check counter*/
int v;
double myNum = 0;
String testVar = (string)(Session["counter"]);
if (Double.TryParse(testVar, out myNum))
{
v = Convert.ToInt32(Session["counter"]);
}
else
{
v = 0;
}
/* end check counter */
/* check display */
int g;
double myNum2 = 0;
String testVar2 = (string)(Session["display"]);
if (Double.TryParse(testVar2, out myNum2))
{
g = Convert.ToInt32(Session["display"]);
}
else
{
g = 10;
}
/* end check display */
if (yearvalue == "0")
{
yearvalue = "car_year IS NOT NULL AND";
}
else
{
yearvalue = "car_year=" + yearvalue + " " + "AND";
}
if (modelvalue == "0")
{
modelvalue = " " +"car_model IS NOT NULL AND";
}
else
{
modelvalue =" " + "car_model LIKE" + " " + "'" + modelvalue + "%'" + " " + "AND";
}
if (locationvalue == "0")
{
locationvalue =" " + "car_location IS NOT NULL";
}
else
{
locationvalue =" " + "car_location LIKE" + " " + "'" + locationvalue + "'" ;
}
string searchvalue =" " + yearvalue + " " + modelvalue + " " + locationvalue;
string orvalue =" " + "AND (car_status = 'Available' OR car_status = 'Booked' OR (car_status = 'Sold' AND status_date Between DateAdd(DD,-14,GETDATE() ) and GETDATE()) )" ;
//string gugugaga = "SELECT car_model,car_year,car_price,car_location,car_plate FROM cardatabase WHERE" + searchvalue;
//mylabel.Text = gugugaga;
DataTable dt = new DataTable();
SqlConnection myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["CMSConnectionString"].ConnectionString);
try
{
SqlCommand cmd = new SqlCommand();
cmd.Connection = myConnection;
cmd.CommandText = "SELECT car_model,car_year,car_price,car_location,car_plate FROM cardatabase WHERE car_approve = 'Approved' AND" + searchvalue + orvalue + " " + "ORDER BY car_price ASC";
//cmd.Parameters.AddWithValue("@epicsearchvalue", searchvalue);
myConnection.Open();
SqlDataAdapter da = new SqlDataAdapter();
da.SelectCommand = cmd;
da.Fill(dt);
count = dt.Rows.Count;
counter = 1;
display = g;
pages = (count / display) + 2;
if (v < 1)
{
current2 = 1;
}
else
{
current2 = v;
}
start = (current2 * display) - display;
string[] mypages = new string[dt.Rows.Count];
StringBuilder spage = new StringBuilder();
// if(current2 == pages){
// mypages[counter] = "
" + counter + "
";
// spage.Append(mypages[counter]);
// }
// else
// {
for (counter = 1; counter < pages; counter++)
{
if (counter == v )
{
mypages[counter] = "";
}
else
{
mypages[counter] = "";
}
spage.Append(mypages[counter]);
}
// }
/*old coding*/
/* if (v == 999)
{
thepage.InnerHtml = "
" + spage.ToString() + "" + "
";
}
else
{
thepage.InnerHtml = "
" + spage.ToString() + "" + "
";
} */
/* end old coding*/
// thepage.InnerHtml = "
";
if (g == 20)
{
string show20 = "";
spage.Append(show20);
}
else if (g == 30)
{
string show30 = "";
spage.Append(show30);
}
else
{
string shownormal = "";
spage.Append(shownormal);
}
if (v == 999)
{
string showitall = "";
spage.Append(showitall);
}
else
{
string showitall = "";
spage.Append(showitall);
}
thepage.InnerHtml = "
";
//searchlistall.Value = sb.ToString();
}
catch (Exception f)
{
Console.WriteLine(f.ToString());
}
finally
{
// Close Connection
myConnection.Close();
}
}
protected void List_Mysearch2()
{
string yearvalue = (string)(Session["year"]);
string modelvalue = (string)(Session["model"]);
string locationvalue = (string)(Session["location"]);
if (yearvalue == "0")
{
yearvalue = "car_year IS NOT NULL AND";
}
else
{
yearvalue = "car_year =" + yearvalue + " " + "AND";
}
if (modelvalue == "0")
{
modelvalue = " " + "car_model IS NOT NULL AND";
}
else
{
modelvalue = " " + "car_model LIKE" + " " + "'" + modelvalue + "%'" + " " + "AND";
}
if (locationvalue == "0")
{
locationvalue = " " + "car_location IS NOT NULL";
}
else
{
locationvalue = " " + "car_location LIKE " + " " + "'" + locationvalue + "'";
}
string searchvalue = " " + yearvalue + " " + modelvalue + " " + locationvalue;
string orvalue =" " + "AND (car_status = 'Available' OR car_status = 'Booked' OR (car_status = 'Sold' AND status_date Between DateAdd(DD,-14,GETDATE() ) and GETDATE()) )" ;
//mylabel.Text = modelvalue;
DataTable dt = new DataTable();
SqlConnection myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["CMSConnectionString"].ConnectionString);
try
{
SqlCommand cmd = new SqlCommand();
cmd.Connection = myConnection;
cmd.CommandText = "SELECT car_model,car_year,car_price,car_location,car_plate,car_status,status_date FROM ( SELECT *, ROW_NUMBER() OVER (ORDER BY car_year) as row FROM cardatabase WHERE car_approve = 'Approved' AND" + searchvalue + orvalue + ")" + " " + "a" + " " + "WHERE row >"+ start + " " + "AND row <=" + (current2 * display) + " " + "ORDER BY car_price ASC";
SqlDataAdapter da = new SqlDataAdapter();
da.SelectCommand = cmd;
da.Fill(dt);
string[] myallsearch = new string[dt.Rows.Count];
string[] myyear = new string[dt.Rows.Count];
string[] mymodel = new string[dt.Rows.Count];
string[] myprice = new string[dt.Rows.Count];
string[] mylocation = new string[dt.Rows.Count];
string[] myplate = new string[dt.Rows.Count];
string[] mystatus = new string[dt.Rows.Count];
int[] whyohwhy = new int[dt.Rows.Count];
int[] checkdays = new int[dt.Rows.Count];
DateTime [] startDate = new DateTime[dt.Rows.Count];
StringBuilder sb = new StringBuilder();
if ( dt.Rows.Count != 0)
{
for (int i = 0; i <= dt.Rows.Count - 1; i++)
{
mymodel[i] = dt.Rows[i][0].ToString();
myyear[i] = dt.Rows[i][1].ToString();
whyohwhy[i]=Convert.ToInt32(dt.Rows[i][2]);
myprice[i] = string.Format("{0:#,###0}",whyohwhy[i]);
//mylocation[i] = dt.Rows[i][3].ToString();
mylocation[i] = Regex.Replace(dt.Rows[i][3].ToString(), "_", " ");
myplate[i] = Uri.EscapeDataString(dt.Rows[i][4].ToString());
mystatus[i] = dt.Rows[i][5].ToString();
startDate[i] = Convert.ToDateTime(dt.Rows[i][6]);
checkdays[i] = (DateTime.Today - startDate[i]).Days;
/*myallsearch[i] = "
";*/
/* the old before change 27/10/2016
myallsearch[i] = "
"; */
/*added by cooper for sale/booked image*/
/*
*/
if ( mystatus[i] == "Sold")
{
myallsearch[i] = "
";
}
//else if ( mystatus[i] == "Booked" && status_date Between DateAdd(DD,-7,GETDATE() ) and GETDATE()
else if ( mystatus[i] == "Booked" && checkdays[i] <= 7)
{
myallsearch[i] = "
";
}
else
{
myallsearch[i] = "
";
}
/* end of edition 27/10/2016 */
sb.Append(myallsearch[i]);
allsearch.InnerHtml = sb.ToString() + "
";
}
}
else
{
allsearch.InnerHtml = "There are no result from the search.
";
}
//searchlistall.Value = sb.ToString();
}
catch (Exception f)
{
Console.WriteLine(f.ToString());
}
finally
{
// Close Connection
myConnection.Close();
}
}
protected void List_Mysearch3()
{
string yearvalue = (string)(Session["year"]);
string modelvalue = (string)(Session["model"]);
string locationvalue = (string)(Session["location"]);
if (yearvalue == "0")
{
yearvalue = "car_year IS NOT NULL AND";
}
else
{
yearvalue = "car_year =" + yearvalue + " " + "AND";
}
if (modelvalue == "0")
{
modelvalue = " " + "car_model IS NOT NULL AND";
}
else
{
modelvalue = " " + "car_model LIKE" + " " + "'" + modelvalue + "%'" + " " + "AND";
}
if (locationvalue == "0")
{
locationvalue = " " + "car_location IS NOT NULL";
}
else
{
locationvalue = " " + "car_location LIKE " + " " + "'" + locationvalue + "'";
}
string searchvalue = " " + yearvalue + " " + modelvalue + " " + locationvalue;
string orvalue =" " + "AND (car_status = 'Available' OR (car_status = 'Booked' AND status_date Between DateAdd(DD,-7,GETDATE() ) and GETDATE()) OR (car_status = 'Sold' AND status_date Between DateAdd(DD,-14,GETDATE() ) and GETDATE()) )" ;
//mylabel.Text = modelvalue;
DataTable dt = new DataTable();
SqlConnection myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["CMSConnectionString"].ConnectionString);
try
{
SqlCommand cmd = new SqlCommand();
cmd.Connection = myConnection;
cmd.CommandText = "SELECT car_model,car_year,car_price,car_location,car_plate,car_status,status_date FROM cardatabase WHERE car_approve = 'Approved' AND" + searchvalue + orvalue + " " + "ORDER BY car_price ASC";
SqlDataAdapter da = new SqlDataAdapter();
da.SelectCommand = cmd;
da.Fill(dt);
string[] myallsearch = new string[dt.Rows.Count];
string[] myyear = new string[dt.Rows.Count];
string[] mymodel = new string[dt.Rows.Count];
string[] myprice = new string[dt.Rows.Count];
string[] mylocation = new string[dt.Rows.Count];
string[] myplate = new string[dt.Rows.Count];
string[] mystatus = new string[dt.Rows.Count];
int[] whyohwhy = new int[dt.Rows.Count];
int[] checkdays = new int[dt.Rows.Count];
DateTime [] startDate = new DateTime[dt.Rows.Count];
StringBuilder sb = new StringBuilder();
if ( dt.Rows.Count != 0)
{
for (int i = 0; i <= dt.Rows.Count - 1; i++)
{
mymodel[i] = dt.Rows[i][0].ToString();
myyear[i] = dt.Rows[i][1].ToString();
whyohwhy[i]=Convert.ToInt32(dt.Rows[i][2]);
myprice[i] = string.Format("{0:#,###0}",whyohwhy[i]);
//mylocation[i] = dt.Rows[i][3].ToString();
mylocation[i] = Regex.Replace(dt.Rows[i][3].ToString(), "_", " ");
myplate[i] = Uri.EscapeDataString(dt.Rows[i][4].ToString());
mystatus[i] = dt.Rows[i][5].ToString();
startDate[i] = Convert.ToDateTime(dt.Rows[i][6]);
checkdays[i] = (DateTime.Today - startDate[i]).Days;
/*myallsearch[i] = "
";*/
/* the old before change 27/10/2016
myallsearch[i] = "
";
*/
/*added by cooper for sale/booked image*/
/*
*/
if ( mystatus[i] == "Sold")
{
myallsearch[i] = "
";
}
else if ( mystatus[i] == "Booked" && checkdays[i] <= 7)
{
myallsearch[i] = "
";
}
else
{
myallsearch[i] = "
";
}
/* end of edition 27/10/2016 */
sb.Append(myallsearch[i]);
allsearch.InnerHtml = sb.ToString();
}
}
else
{
allsearch.InnerHtml = "There are no result from the search.
";
}
//searchlistall.Value = sb.ToString();
}
catch (Exception f)
{
Console.WriteLine(f.ToString());
}
finally
{
// Close Connection
myConnection.Close();
}
}
}