I want to filter datagrid data according to Dropdownlist order by Months and Year(retrive from databse/listitem)showing same problem. Invalid column name 'Jan'.Same for All months(its when i choosed jan in dropdownlist
using
System;using
System.Data;using
System.Configuration;using
System.Collections;using
System.Web;using
System.Web.Security;using
System.Web.UI;using
System.Web.UI.WebControls;using
System.Web.UI.WebControls.WebParts;using
System.Web.UI.HtmlControls;using
System.Data.SqlClient;public
partial class Er_TimeSheetHistory : System.Web.UI.Page{
public string strcon1 = "Data Source=LENOVO-EF645A82;AttachDbFilename=E:\\ERSYSTEMS_WEB_1\\DataSourse\\EmpDb1.mdf;Initial Catalog=Erdata;Integrated Security=True"; public string WelcomeName; SqlDataReader dr; SqlParameter mparam, yparam; SqlCommand cmd; protected void Page_Load(object sender, EventArgs e){
WelcomeName = Session[
"userid"].ToString(); //lbldate.Text = DateTime.Now.ToString (); DateTime Date = DateTime.Now;Label2.Text = Date.ToString(
"MMMM");Label3.Text = Date.ToString(
"yyyy");BtnSubmit.Enabled =
false;}
protected void BtnSubmit_Click1(object sender, EventArgs e){
HtmlForm frm=new HtmlForm() ;Response.Clear();
Response.AddHeader(
"content-disposition", "attachment;filename=FileName.xls");Response.Charset =
"";Response.Cache.SetCacheability(
HttpCacheability.NoCache);Response.ContentType =
"application/vnd.xls";System.IO.
StringWriter stringWrite = new System.IO.StringWriter();System.Web.UI.
HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWrite); //frm.Attributes.Add("runat", "server"); //GridView1.Attributes.Add("runat", "server"); //GridView1.RenderControl(htmlWrite);DataGrid1.RenderControl(htmlWrite);
Response.Write(stringWrite.ToString());
Response.End();
}
protected void Button1_Click(object sender, EventArgs e){
Bindgrid();
}
public void Bindgrid(){
SqlConnection conn = new SqlConnection(strcon1); SqlDataAdapter da = new SqlDataAdapter("select * from D_TimeSheet1 where EmpId=" + Session["usid"] + " AND Year=" +lstyear .SelectedValue + " AND Months="+DropDownList1 .SelectedValue.ToString () +" order By AttDate DESC", conn); DataSet ds = new DataSet();da.Fill(ds,
"D_TimeSheet1"); // Filling a employee table DataTable dt = ds.Tables["D_TimeSheet1"];dt = RemoveDuplicateRows(dt,
"EmpId");DataGrid1.DataSource = ds.Tables[
"D_TimeSheet1"].DefaultView;DataGrid1.DataBind();
}
public DataTable RemoveDuplicateRows(DataTable dTable, string AttDate){
Hashtable hTable = new Hashtable(); ArrayList duplicateList = new ArrayList(); //Add list of all the unique item value to hashtable, which stores combination of key, value pair. //And add duplicate item value in arraylist. foreach (DataRow drow in dTable.Rows){
if (hTable.Contains(drow[AttDate]))duplicateList.Add(drow);
elsehTable.Add(drow[AttDate],
string.Empty);}
//Removing a list of duplicate items from datatable. foreach (DataRow dRow in duplicateList)dTable.Rows.Remove(dRow);
//Datatable which contains unique records will be return as output. return dTable;}
}
Vijaya KadiyalaPosted Apr 9, 2008, 4:49 PM
Hi,
By lookin gat the error it looks like you are using Jan as the column name in the query. But can u tell me which pice of line causing this problem?
Thanks -- Vj
http://dotnetvj.blogspot.com