When i add the new row to the List view above selected dropdownlist gets cleared .what is the reason for this problem plz suggest me
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
FirstListViewRow();
BindDataToGridviewDropdownlist();
}
}
protected void BindDataToGridviewDropdownlist()
{
DataSet dsDept = new DataSet();
dsDept.ReadXml(Server.MapPath("XMLFile2.xml"));
DataView dv = dsDept.Tables[0].DefaultView;
foreach (var list in listview1.Items)
{
if (list.ItemType == ListViewItemType.DataItem)
{
DropDownList ddf = (DropDownList)list.FindControl("ddldatatype");
ddf.DataSource = dv;
ddf.DataTextField = "value";
ddf.DataBind();
ddf.Items.Insert(0, new ListItem("--Select--", "0"));
}
}
}
private void FirstListViewRow()
{
DataTable dt = new DataTable();
DataRow dr = null;
dt.Columns.Add(new DataColumn("OrderNo", typeof(string)));
dt.Columns.Add(new DataColumn("ColumnTitle", typeof(string)));
dt.Columns.Add(new DataColumn("Datatype", typeof(string)));
dt.Columns.Add(new DataColumn("Examples", typeof(string)));
dt.Columns.Add(new DataColumn("Options", typeof(string)));
dt.Columns.Add(new DataColumn("Delete", typeof(string)));
dr = dt.NewRow();
dt.Rows.Add(dr);
dr["OrderNo"] = 1;
Session["CurrentTable"] = dt;
listview1.DataSource = dt;
listview1.DataBind();
}
private void AddNewRow()
{
int rowIndex = 0;
if (Session["CurrentTable"] != null)
{
DataTable dtCurrentTable = (DataTable)Session["CurrentTable"];
DataRow drCurrentRow = null;
if (dtCurrentTable.Rows.Count > 0)
{
for (int i = 1; i <= dtCurrentTable.Rows.Count; i++)
{
Label TextBoxorder = (Label)listview1.Items[rowIndex].FindControl("txtorder");
TextBox TextBoxcolumnname = (TextBox)listview1.Items[rowIndex].FindControl("txtcolumnname");
DropDownList DropDatatype = (DropDownList)listview1.Items[rowIndex].FindControl("ddldatatype");
DropDownList Dropexample = (DropDownList)listview1.Items[rowIndex].FindControl("ddlexamples");
TextBox TextBoxoptions = (TextBox)listview1.Items[rowIndex].FindControl("txtoptions");
CheckBox Checkdel = (CheckBox)listview1.Items[rowIndex].FindControl("chkdel");
drCurrentRow = dtCurrentTable.NewRow();
drCurrentRow["OrderNo"] = i + 1;
// dtCurrentTable.Rows[i - 1]["Order"] = TextBoxorder.Text;
dtCurrentTable.Rows[i - 1]["ColumnTitle"] = TextBoxcolumnname.Text;
dtCurrentTable.Rows[i - 1]["Datatype"] = DropDatatype.Text;
dtCurrentTable.Rows[i - 1]["Examples"] = Dropexample.Text;
dtCurrentTable.Rows[i - 1]["Options"] = TextBoxoptions.Text;
dtCurrentTable.Rows[i - 1]["Delete"] = Checkdel.Text;
rowIndex++;
}
dtCurrentTable.Rows.Add(drCurrentRow);
Session["CurrentTable"] = dtCurrentTable;
listview1.DataSource = dtCurrentTable;
listview1.DataBind();
Label txn = (Label)listview1.Items[rowIndex].FindControl("txtorder");
txn.Focus();
}
}
else
{
Response.Write("Session is null");
}
BindDataToGridviewDropdownlist();
}
protected void btnGenerate_Click(object sender, EventArgs e)
{
}
protected void btnAdd_Click(object sender, EventArgs e)
{
AddNewRow();
}
protected void btndelete_Click(object sender, EventArgs e)
{
DataTable dt = new DataTable();
if (Session["CurrentTable"] != null)
{
dt = (DataTable)Session["CurrentTable"];
int j = 0;
for (int i = 0; i < listview1.Items.Count; i++)
{
ListViewDataItem items = listview1.Items[i];
CheckBox chkBox = (CheckBox)items.FindControl("chkdel");
if (chkBox.Checked == true)
{
dt.Rows.RemoveAt(j);
dt.AcceptChanges();
}
else
{
j++;
}
}
Session["CurrentTable"] = dt;
listview1.DataSource = dt;
listview1.DataBind();
BindDataToGridviewDropdownlist();
}
}
protected void btnClear_Click(object sender, EventArgs e)
{
listview1.Items.Clear();
}
}
}
Add a property DataKeyNames in this ListView like this:
<asp:listview id="listview1" runat="server" datakeynames="Datatype" xmlns:asp="#unknown"></asp:listview>
and add these lines inside BindDataToGridviewDropdownlist method:
ListViewDataItem di = (ListViewDataItem)list;
string dataType = listview1.DataKeys[di.DisplayIndex].Values[0].ToString();
ddf.SelectedValue = dataType;
here is how you BindDataToGridviewDropdownlist method would look like:
protected void BindDataToGridviewDropdownlist()
{
DataSet dsDept = new DataSet();
dsDept.ReadXml(Server.MapPath("XMLFile2.xml"));
DataView dv = dsDept.Tables[0].DefaultView;
foreach (var list in listview1.Items)
{
if (list.ItemType == ListViewItemType.DataItem)
{
DropDownList ddf = (DropDownList)list.FindControl("ddldatatype");
ddf.DataSource = dv;
ddf.DataTextField = "value";
ddf.DataBind();
ddf.Items.Insert(0, new ListItem("--Select--", "0"));
ListViewDataItem di = (ListViewDataItem)list;
string dataType = listview1.DataKeys[di.DisplayIndex].Values[0].ToString();
ddf.SelectedValue = dataType;
}
}
}
Related
I want to add labels through loop
`private void CourseOutcomes_Load(object sender, EventArgs e)
{
studentDetails dets = new studentDetails();
dets.ShowDialog();
name=dets.CourseName;
labelCourseName.Text = name;
string id = dets.CourseValue;
SqlDataReader sdr = bznessLogic.GetQuestions(id);
List<Label> lbl = new List<Label>();
int count = 0;
while (sdr.Read())
{
MessageBox.Show("s");
lbl[count] = new Label();
lbl[count].Text = sdr[0].ToString();
this.Controls.Add(lbl[count]);
count++;
}
sdr.Close();
}`
But it keeps giving error
'Index was out of range.
Should I initiablize it diffrently or..
So it turns out I just needed to initialize it properly which I expected, noob mistake but here we are.
private void CourseOutcomes_Load(object sender, EventArgs e)
{
studentDetails dets = new studentDetails();
dets.ShowDialog();
name=dets.CourseName;
labelCourseName.Text = name;
string id = dets.CourseValue;
SqlDataReader sdr = bznessLogic.GetQuestions(id);
List<Label> lbl = new List<Label>();
int count = 0;
while (sdr.Read())
{
MessageBox.Show("s");
lbl.Add(new Label() { Name="lbl"+count});
lbl[count].Text = sdr[0].ToString();
this.Controls.Add(lbl[count]);
count++;
}
sdr.Close();
}
I need help here. Below is my code. In this line of code
dtCurrentTable.Rows[0]["Kolicina"] = Convert.ToInt32(Label37.Text) + 3;
instead of 3 in next method this Label37.Text value should be incremented for one each time when user press the button.So I want this label value in next method to start to count from current value for one each time on button click.
protected void btnTest_Click(object sender, EventArgs e)
{
var clickedRow = ((Button)sender).NamingContainer as GridViewRow;
var clickedIndex = clickedRow.RowIndex;
decimal old = dtCurrentTable.Rows[clickedIndex].Field<decimal>("Kolicina");
decimal oldIznos = dtCurrentTable.Rows[clickedIndex].Field<decimal>("VkIznos");
decimal VkDanok = dtCurrentTable.Rows[clickedIndex].Field<decimal>("VkDanok");
string Cena1 = dtCurrentTable.Rows[clickedIndex].Field<string>("Cena1");
int TarifaID = dtCurrentTable.Rows[clickedIndex].Field<Int16>("TarifaID");
newValue = old - 1;
// so i need label value from here to go in next method and increment++
Label37.Text = newValue.ToString();
decimal newIznos = oldIznos - Convert.ToDecimal(Cena1);
dtCurrentTable.Rows[clickedIndex].SetField("Kolicina", newValue.ToString());
dtCurrentTable.Rows[clickedIndex].SetField("VkIznos", newIznos.ToString());
}
protected void Button6_Click(object sender, EventArgs e)
{
SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["string2"].ConnectionString);
conn.Open();
ViewState["count"] = Convert.ToInt32(ViewState["count"]) + 1;
// SqlCommand sqlCmd = new SqlCommand();
// sqlCmd.CommandText = "SupaSpanak";
// sqlCmd.Connection = conn;
// sqlCmd.CommandType = CommandType.StoredProcedure;
SqlCommand cmd1 = new SqlCommand("SELECT pkid,Artikal,Vid,EdMera,TarifaID,Cena1 FROM Artikli WHERE Artikal= 'N KREM SUPA OD SPANA]++' AND Cena1 = 130.00", conn);
//brojac za kolicina
count++;
decimal noofcount = count;
Session["kolicina"] = noofcount;
Label5.Text = Session["kolicina"].ToString();//ViewState["count"].ToString();//
SqlDataReader reader = cmd1.ExecuteReader();
if (reader.Read())
{
Label9.Text = (string)reader["pkid"].ToString();
Label3.Text = (string)reader["Artikal"].ToString();
Label23.Text = (string)reader["Vid"].ToString();
Label10.Text = (string)reader["EdMera"].ToString();
Label7.Text = (string)reader["TarifaID"].ToString();
Label4.Text = (string)reader["Cena1"].ToString();
decimal cena = Convert.ToDecimal(Label5.Text);//kolicina
decimal kolicina = Convert.ToDecimal(Label4.Text);//cena
//vkIznos
decimal mnoz = cena * kolicina;
Label6.Text = mnoz.ToString();
Convert.ToDecimal(Label6.Text);
conn.Close();
DataTable dtCurrentTable = (DataTable)ViewState["Markici"];
if (Label37.Text == "0")
{
dtCurrentTable.Rows[0]["Kolicina"] = Label5.Text;
}
if (Label37.Text != "0")
{
//Here this current value of Label37.text need to be incremented for one each time when button is clicked.
dtCurrentTable.Rows[0]["Kolicina"] = Convert.ToInt32(Label37.Text) + 3;
}
you need viewstate,
public int Counter
{
get
{
if (ViewState["Counter"] != null)
{
return Convert.ToInt32(ViewState["Counter"]);
}
else
return 0;
}
set { ViewState["Counter"] = value; }
}
protected void Button6_Click(object sender, EventArgs e)
{
//other codes..
Counter = Counter + 1;
if (Label37.Text != "0")
{
dtCurrentTable.Rows[0]["Kolicina"] = Convert.ToInt32(Label37.Text) + Counter;
}
}
Hello i have problem with data Table. I want to insert new row in data Table every time when user click on button.I have 10 buttons and when user click on button 1,it insert new row but when user clicks on button 2 existing row is replaced with new one. HELP!
////here i call methods for adding new rows from button clicks
private void RemoveDuplicates (Data_Table dt)
{
if (ViewState["Markici"] != null)
{
dtCurrentTable = (DataTable)ViewState["Markici"];
if (dtCurrentTable.Rows.Count > 0)
{
for (int i = dtCurrentTable.Rows.Count - 1; i >= 0; i--)
{
if (i == 0)
{
break;
}
for (int j = i - 1; j >= 0; j--)
{
if (dtCurrentTable.Rows[i]["Cena1"].ToString() == dtCurrentTable.Rows[j]["Cena1"].ToString())
{
dtCurrentTable.Rows[i].Delete();
break;
}
}
}
}
dtCurrentTable.AcceptChanges();
// ViewState["Markici"] = dtCurrentTable;
// Repeater1.DataSource = dtCurrentTable;
// Repeater1.DataBind();
AddNewRecordRowToGrid();
AddNewRecordRowToGridf();
decimal vkupno = 0m;
vkupno += Convert.ToDecimal(Label6.Text);
decimal vkupno2 = 0m;
vkupno2 += Convert.ToDecimal(Label17.Text);
vkupno += vkupno2;
Label26.Text = vkupno.ToString();
//xml table
// DataSet ds = new DataSet();
// ds.Tables.Add(dtCurrentTable);
//binding Gridview with New Row
GridView2.DataSource = dtCurrentTable;
GridView2.DataBind();
}
}
//method for adding row, call this in button click 9
private void AddNewRecordRowToGrid()
{
int counter;
if (Request.Cookies["kasa"] == null)
counter = 0;
else
{
counter = int.Parse(Request.Cookies["kasa"].Value);
}
counter++;
Response.Cookies["kasa"].Value = counter.ToString();
Response.Cookies["kasa"].Expires = DateTime.Now.AddYears(2);
if (ViewState["Markici"] != null)
{
DataTable dtCurrentTable = (DataTable)ViewState["Markici"];
DataRow drCurrentRow = null;
if (dtCurrentTable.Rows.Count > 0)
{
for (int i = 1; i <= dtCurrentTable.Rows.Count; i++)
{
drCurrentRow = dtCurrentTable.NewRow();
drCurrentRow["FirmaID"] = Request.Cookies["firma"].Value;
drCurrentRow["Godina"] = Request.Cookies["godina"].Value;
drCurrentRow["KasaID"] = Request.Cookies["kasa"].Value;
drCurrentRow["MarkicaID"] = counter;
drCurrentRow["Datum"] = DateTime.Now;
drCurrentRow["Masa"] = Session["masa39"];
drCurrentRow["VrabotenID"] = Session["New"];
drCurrentRow["Artikal"] = Label3.Text;
drCurrentRow["Cena1"] = Label4.Text;
drCurrentRow["Kolicina"] = Label5.Text;
drCurrentRow["Smena"] = Session["smena1"];
drCurrentRow["VkIznos"] = Label6.Text;
drCurrentRow["VkDanok"] = Label8.Text;
drCurrentRow["SySDatum"] = DateTime.Now;
drCurrentRow["Vid"] = Label23.Text;
drCurrentRow["Edmera"] = Label10.Text;
drCurrentRow["ArtikalID"] = Label33.Text;
}
/*
for (int i = 0; i < dtCurrentTable.Rows.Count; i++)
{
if (dtCurrentTable.Rows[i].IsNull(0) == true)
{
dtCurrentTable.Rows[i].Delete();
dtCurrentTable.AcceptChanges();
}
}*/
//Removing initial blank row
if (dtCurrentTable.Rows[0][0].ToString() == "")
{
dtCurrentTable.Rows[0].Delete();
dtCurrentTable.AcceptChanges();
}
//Added New Record to the DataTable
dtCurrentTable.Rows.Add(drCurrentRow);
//storing DataTable to ViewState
ViewState["Markici"] = dtCurrentTable;
//binding Gridview with New Row
GridView2.DataSource = dtCurrentTable;
GridView2.DataBind();
}
}
}
// call this in button 10 click
private void AddNewRecordRowToGridf()
{
if (ViewState["Markici"] != null)
{
DataTable dtCurrentTable = (DataTable)ViewState["Markici"];
DataRow drCurrentRow = null;
if (dtCurrentTable.Rows.Count > 0)
{
for (int i = 1; i <= dtCurrentTable.Rows.Count; i++)
{
//Creating new row and assigning values
drCurrentRow = dtCurrentTable.NewRow();
drCurrentRow["FirmaID"] = Request.Cookies["firma"].Value;
drCurrentRow["Godina"] = Request.Cookies["godina"].Value;
drCurrentRow["KasaID"] = Request.Cookies["kasa"].Value;
drCurrentRow["MarkicaID"] = Request.Cookies["kasa"].Value;
drCurrentRow["Datum"] = DateTime.Now;
drCurrentRow["Masa"] = Session["masa39"];
drCurrentRow["VrabotenID"] = Session["New"];
drCurrentRow["Artikal"] = Label12.Text;
drCurrentRow["Cena1"] = Label13.Text;
drCurrentRow["Kolicina"] = Label11.Text;
drCurrentRow["Smena"] = Session["smena1"];
drCurrentRow["VkIznos"] = Label17.Text;
drCurrentRow["VkDanok"] = Label18.Text;
drCurrentRow["SySDatum"] = DateTime.Now;
drCurrentRow["Vid"] = Label24.Text;
drCurrentRow["Edmera"] = Label16.Text;
drCurrentRow["ArtikalID"] = Label34.Text;
}
for (int i = 0; i < dtCurrentTable.Rows.Count; i++)
{
if (dtCurrentTable.Rows[i].IsNull(0) == true)
{
dtCurrentTable.Rows[i].Delete();
dtCurrentTable.AcceptChanges();
}
}
/*
//Removing initial blank row
if (dtCurrentTable.Rows[0][0].ToString() == "")
{
dtCurrentTable.Rows[0].Delete();
dtCurrentTable.Rows[1].Delete();
dtCurrentTable.AcceptChanges();
}
*/
//Added New Record to the DataTable
dtCurrentTable.Rows.Add(drCurrentRow);
//storing DataTable to ViewState
ViewState["Markici"] = dtCurrentTable;
//binding Gridview with New Row
GridView2.DataSource = dtCurrentTable;
GridView2.DataBind();
}
}
protected void Button9_Click(object sender, EventArgs e)
{
RemoveDuplicates(dt);
}
protected void Button10_Click(object sender, EventArgs e)
{
RemoveDuplicates(dt);
}
// create DataTable
private void AddDefaultFirstRecord()
{
//creating DataTable
DataTable dt = new DataTable();
DataRow dr;
dt.TableName = "Markici";
//creating columns for DataTable
dt.Columns.Add(new DataColumn("FirmaID", typeof(System.Int32)));
dt.Columns.Add(new DataColumn("Godina", typeof(System.Int32)));
dt.Columns.Add(new DataColumn("KasaID", typeof(System.Int32)));
dt.Columns.Add(new DataColumn("MarkicaID", typeof(System.Int64)));
dt.Columns.Add(new DataColumn("Datum", typeof(System.DateTime)));
dt.Columns.Add(new DataColumn("Masa", typeof(System.Int32)));
dt.Columns.Add(new DataColumn("VrabotenID", typeof(System.Int32)));
dt.Columns.Add(new DataColumn("Artikal", typeof(System.String)));
dt.Columns.Add(new DataColumn("Cena1", typeof(System.String)));
dt.Columns.Add(new DataColumn("Kolicina", typeof(System.Decimal)));
dt.Columns.Add(new DataColumn("Smena", typeof(System.Int32)));
dt.Columns.Add(new DataColumn("VkIznos", typeof(System.Decimal)));
dt.Columns.Add(new DataColumn("VkDanok", typeof(System.Decimal)));
dt.Columns.Add(new DataColumn("SysDatum", typeof(System.DateTime)));
dt.Columns.Add(new DataColumn("Vid", typeof(System.String)));
dt.Columns.Add(new DataColumn("EdMera", typeof(System.String)));
dt.Columns.Add(new DataColumn("ArtikalID", typeof(System.String)));
dr = dt.NewRow();
dt.Rows.Add(dr);
ViewState["Markici"] = dt;
GridView2.DataSource = dt;
GridView2.DataBind();
}
Add dtCurrentTable.AcceptChanges(); after dtCurrentTable.Rows.Add(drCurrentRow);
Don't keep a DataTable in to ViewState. DataTable is a big object. It will affect page performance. You can keep in to cache or session.
My recomandation is to cache the datatable datasource not the datatable object.
I have to click linkbutton twice to change the page and load subfolders of current folder. I think I have some problems with my session and event.
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
TableRow r = new TableRow();
TableCell c1 = new TableCell();
LinkButton l = new LinkButton();
var strarray = Server.MapPath(Session["url"].ToString()).Split(Path.DirectorySeparatorChar);
Table table = new Table();
l.ID = "lable" + table.Rows.Count;
l.Text = strarray.Last();
l.EnableViewState = true;
r.ID = "newRow" + table.Rows.Count;
c1.ID = "newC1" + table.Rows.Count;
c1.Controls.Add(l);
r.Cells.Add(c1);
table.Rows.Add(r);
this.form1.Controls.Add(table);
}
else
{
if(Directory.Exists(Server.MapPath(Session["url"].ToString())))
{
string[] allFolders = Directory.GetDirectories(Server.MapPath(Session["url"].ToString()));
Table table = new Table();
foreach (string str in allFolders)
{
TableRow r = new TableRow();
TableCell c1 = new TableCell();
LinkButton l = new LinkButton();
var strarray = str.Split(Path.DirectorySeparatorChar);
l.ID = strarray.Last();
l.CommandName = "createLink";
l.Click+= new EventHandler(LinkButton_Command);
l.Text = strarray.Last();
l.EnableViewState = true;
r.ID = "newRow" + table.Rows.Count;
c1.ID = "newC1" + table.Rows.Count;
c1.Controls.Add(l);
r.Cells.Add(c1);
table.Rows.Add(r);
this.form1.Controls.Add(table);
}
}
else
{
Work work=new Work();
work.SetFolder(Session["url"].ToString());
work.CreateFolder();
}
}
}
public void LinkButton_Command(object sender, EventArgs e)
{
Session["url"] = Session["url"] + "\\" + ((LinkButton)sender).ID;
}
I think this is because the Page_Load() method is called before the LinkButton_Command(). The session variable Session["url"] still contains the previous value when the code in Page_Load() executes.
Try moving the code from the else-branch of Page_Load() at the end of LinkButton_Command().
after mix up your answer and my program , come to a answer that will work.
protected void Page_Load(object sender, EventArgs e)
{
if (Directory.Exists(Server.MapPath(Session["url"].ToString())))
{
string[] allFolders = Directory.GetDirectories(Server.MapPath(Session["url"].ToString()));
Table table = new Table();
foreach (string str in allFolders)
{
TableRow r = new TableRow();
TableCell c1 = new TableCell();
LinkButton l = new LinkButton();
var strarray = str.Split(Path.DirectorySeparatorChar);
l.ID = strarray.Last();
l.CommandName = "createLink";
l.Click += new EventHandler(LinkButton_Command);
l.Text = strarray.Last();
l.EnableViewState = true;
r.ID = "newRow" + table.Rows.Count;
c1.ID = "newC1" + table.Rows.Count;
c1.Controls.Add(l);
r.Cells.Add(c1);
table.Rows.Add(r);
this.form1.Controls.Clear();
this.form1.Controls.Add(table);
}
}
}
public void LinkButton_Command(object sender, EventArgs e)
{
Session["url"] = Session["url"] + "\\" + ((LinkButton)sender).ID;
if (Directory.Exists(Server.MapPath(Session["url"].ToString())))
{
string[] allFolders = Directory.GetDirectories(Server.MapPath(Session["url"].ToString()));
Table table = new Table();
foreach (string str in allFolders)
{
TableRow r = new TableRow();
TableCell c1 = new TableCell();
LinkButton l = new LinkButton();
var strarray = str.Split(Path.DirectorySeparatorChar);
l.ID = strarray.Last();
l.CommandName = "createLink";
l.Click += new EventHandler(LinkButton_Command);
l.Text = strarray.Last();
l.EnableViewState = true;
r.ID = "newRow" + table.Rows.Count;
c1.ID = "newC1" + table.Rows.Count;
c1.Controls.Add(l);
r.Cells.Add(c1);
table.Rows.Add(r);
this.form1.Controls.Clear();
this.form1.Controls.Add(table);
}
}
}
i am trying to do sorting and paging for my gridview without datasource but when i click on the header of the gridview to sort it , nothing happen but when i click for paging is working fine and there is no error. how to do about it?
this is my code:
protected void Page_Load(object sender, EventArgs e)
{
dbBind();
}
protected void GridView1_Sorting(object sender, GridViewSortEventArgs e)
{
DataTable dtSortTable = GridView1.DataSource as DataTable;
if (dtSortTable != null)
{
DataView dvSortedView = new DataView(dtSortTable);
dvSortedView.Sort = e.SortExpression + " " + getSortDirectionString(e.SortDirection);
GridView1.DataSource = dvSortedView;
GridView1.DataBind();
}
}
private string getSortDirectionString(SortDirection sortDirection)
{
string newSortDirection = String.Empty;
if(sortDirection== SortDirection.Ascending)
{
newSortDirection = "ASC";
}
else
{
newSortDirection = "DESC";
}
return newSortDirection;
}
private void dbBind()
{
DataSet ds;
string startdate = (string)(Session["startdate"]);
string enddate = (string)(Session["enddate"]);
var format = "dd/MM/yyyy";
DateTime one = DateTime.ParseExact(startdate, format, CultureInfo.InvariantCulture);
DateTime two = DateTime.ParseExact(enddate, format, CultureInfo.InvariantCulture);
if (two >= one)
{
SqlConnection conn = new SqlConnection("Data Source=localhost;Initial Catalog=n;Integrated Security=True");
//conn.Open();
SqlCommand cmd = new SqlCommand("SELECT [AmountSpent], [TimeDate]=convert(nvarchar,timedate,103), [StallNo] FROM [StudentTransactions] WHERE TimeDate BETWEEN #one AND #two", conn);
cmd.Parameters.AddWithValue("#one", one);
cmd.Parameters.AddWithValue("#two", two);
ds = new DataSet();
using (SqlDataAdapter sda = new SqlDataAdapter(cmd))
{
sda.Fill(ds);
if (ds.Tables.Count > 0)
{
DataView dv = ds.Tables[0].DefaultView;
if (ViewState["SortDirection"] != null)
{
sortDirection = ViewState["SortDirection"].ToString();
}
if (ViewState["SortExpression"] != null)
{
sortExpression = ViewState["SortExpression"].ToString();
dv.Sort = string.Concat(sortExpression, " ", sortDirection);
}
GridView1.DataSource = dv;
GridView1.DataBind();
}
}
//GridView1.DataBind();
//conn.Close();
}
else
{
GridView1.Visible = false;
string strMsg = " Data not found for the choosen dates.";
Response.Write("<script>alert('" + strMsg + "')</script>");
}
}