Data is not filling in dataview - asp.net

I am trying to bind my repeater with dataview but it is not showing me data in dataview when i see it through breakpoint
private void Get_Data()
{
String File = Server.MapPath("BlogContent.xml");
DataSet ds = new DataSet();
ds.ReadXml(File);
DataView dv = new DataView(ds.Tables[0]);
DataTable dt = dv.Table;
ViewState.Add("Mytable", dt);
}
private void Bind_Data(int take, int pageSize)
{
PagedDataSource page = new PagedDataSource();
page.AllowCustomPaging = true;
page.AllowPaging = true;
DataTable dtv = (DataTable)ViewState["Mytable"];
DataView dv = new DataView();
dv = dtv.DefaultView;
dv.RowFilter = "id>=" + pageSize + " AND " + "id<=" + take;
page.DataSource = dv;
page.PageSize = psize;
Repeater1.DataSource = page;
Repeater1.DataBind();
if (!IsPostBack)
{
int rowcount = dtv.Rows.Count;
CreatePagingControl(rowcount);
}
}
PagedDataSource page = new PagedDataSource();
page.AllowCustomPaging = true;
page.AllowPaging = true;
DataTable dtv = (DataTable)ViewState["Mytable"];
DataView dv = new DataView();
dv = dtv.DefaultView;
dv.RowFilter = "id>=" + pageSize + " AND " + "id<=" + take;
page.DataSource = dv;
page.PageSize = psize;
Repeater1.DataSource = page;
Repeater1.DataBind();
if (!IsPostBack)
{
int rowcount = dtv.Rows.Count;
CreatePagingControl(rowcount);
}
please check it and tell me where i am going wrong.. I try to add paging in repeater control the number of paging display well but data is not fetching in dataview . When i check my datatable it is filling but when it comes to dataview shows me nothng

I got the answers.. The order of id in my xml was not in correct In my xml the id of each node is like
5
3
4
1
2
I do it correct
1
2
3
4
5
and it is working now ....

Related

How to create SelectIndexChanged event for multiple selection in asp.net

I want to select multiple item from checkbox list and then fill another check Box list on select index changed event of First check box list but after one selection its not working for another selected items because of "Auto Post Back = true" while debbuging i can see pointer start from page load event. I want after selection of all Item it should be fire "ddlregion_SelectedIndexChanged" so that I can able see all selected Item related value in another check Box List.
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
BindRegion();
}
}
Due to this its not fetching data for another selected Items how to solve this issues Please help me.
ddlRegion Code Binding:
public void BindRegion()
{
OracleCommand Cmd = new OracleCommand("select * from regions", con);
Cmd.CommandType = CommandType.Text;
OracleDataAdapter da = new OracleDataAdapter();
DataSet ds = new DataSet();
da.SelectCommand = Cmd;
da.Fill(ds);
ddlregion.DataSource = ds;
ddlregion.DataTextField = "REGION_DESC";
ddlregion.DataValueField = "REGION_CODE";
ddlregion.DataBind();
}
Select Index Changed Event
protected void ddlregion_SelectedIndexChanged(object sender, EventArgs e)
{
ddlDepot.Items.Clear();
ddlDepot.Items.Add(new ListItem("--Select Depot--", ""));
foreach (ListItem item in ddlregion.Items)
{
if (item.Selected == true)
{
string str = "select d.depot_code, d.depot_description from regions r, sub_regions sr, depots d where r.region_code = sr.region_code and sr.sub_region_code = d.sub_region_code and active = 'Y' and r.region_code in " + ddlregion.SelectedValue + "";
OracleCommand Cmd = new OracleCommand(str, con);
Cmd.CommandType = CommandType.Text;
OracleDataAdapter da = new OracleDataAdapter();
DataSet ds = new DataSet();
da.SelectCommand = Cmd;
da.Fill(ds);
ddlDepot.DataSource = ds;
ddlDepot.DataTextField = "DEPOT_DESCRIPTION";
ddlDepot.DataValueField = "DEPOT_CODE";
ddlDepot.DataBind();
}
}
}
You need to build your region list for the SQL, then once done call your SQL and Bind.
string regions = "";
bool regionSelected = false;
foreach (ListItem item in ddlregion.Items)
{
if (item.Selected == true)
{
regions += item.SelectedValue + ",";
regionSelected = true;
}
}
// looping done so clean up string for SQL
regions = "(" + regions.TrimEnd(',') + " )";
//only run if a region is selected
if(regionSelected)
{
string str = "select d.depot_code, d.depot_description from regions r, sub_regions sr,
depots d where r.region_code = sr.region_code and
sr.sub_region_code = d.sub_region_code and active = 'Y' and r.region_code
in " + regions;
OracleCommand Cmd = new OracleCommand(str, con);
Cmd.CommandType = CommandType.Text;
OracleDataAdapter da = new OracleDataAdapter();
DataSet ds = new DataSet();
da.SelectCommand = Cmd;
da.Fill(ds);
ddlDepot.DataSource = ds;
ddlDepot.DataTextField = "DEPOT_DESCRIPTION";
ddlDepot.DataValueField = "DEPOT_CODE";
ddlDepot.DataBind();
}

Counter doesn't increment

Relatively new to coding, so there might be a clear mistake in the code. What I am trying to do is have a counter that increases by one each time a new product is added to the basket.
When the program is run, its default value is 0, when the you click on the first product to add to the basket, it increases to 1, however, if you add any more products it sticks at 1 instead of increasing each time.
I have included the code below - is there an obvious mistake I am over looking being a novice (I'll be the first to attempt that ha!). No error message is thrown at any point.
protected void BtnAddToBasket_Click(object sender, EventArgs e)
{
string ProductID = Convert.ToInt16((((Button)sender).CommandArgument)).ToString();
string ProductStock = "1";
DataListItem currentItem = (sender as Button).NamingContainer as DataListItem;
Label lblAvailableStock = currentItem.FindControl("lblAvailableStock") as Label;
if (Session["MyBasket"] != null)
{
DataTable dt = (DataTable)Session["My Basket"];
var checkProduct = dt.AsEnumerable().Where(r => r.Field<string>("ProductID") == ProductID);
if (checkProduct.Count() == 0)
{
string query = "SELECT * FROM Product WHERE ProductID = " + ProductID + "";
DataTable dtProduct = GetData(query);
DataRow dr = dt.NewRow();
dr["ProductID"] = ProductID;
dr["ProductName"] = Convert.ToString(dtProduct.Rows[0]["ProductName"]);
dr["ProductDescription"] = Convert.ToString(dtProduct.Rows[0]["ProductDescription"]);
dr["ProductPrice"] = Convert.ToString(dtProduct.Rows[0]["ProductPrice"]);
dr["ProductStock"] = ProductStock;
dr["ProductImageUrl"] = Convert.ToString(dtProduct.Rows[0]["ProductImageUrl"]);
dr["AvailableStock"] = lblAvailableStock;
dt.Rows.Add(dr);
Session["MyBasket"] = dt;
btnShoppingBasket.Text = dt.Rows.Count.ToString();
}
}
else
{
string query = "SELECT * FROM Product WHERE ProductID =" + ProductID + "";
DataTable dtProduct = GetData(query);
DataTable dt = new DataTable();
dt.Columns.Add("ProductID", typeof(string));
dt.Columns.Add("ProductName", typeof(string));
dt.Columns.Add("ProductDescription", typeof(string));
dt.Columns.Add("ProductPrice", typeof(string));
dt.Columns.Add("ProductStock", typeof(string));
dt.Columns.Add("ProductImageUrl", typeof(string));
dt.Columns.Add("AvailableStock", typeof(string));
DataRow dr = dt.NewRow();
dr["ProductID"] = ProductID;
dr["ProductName"] = Convert.ToString(dtProduct.Rows[0]["ProductName"]);
dr["ProductDescription"] = Convert.ToString(dtProduct.Rows[0]["ProductDescription"]);
dr["ProductPrice"] = Convert.ToString(dtProduct.Rows[0]["ProductPrice"]);
dr["ProductStock"] = ProductStock;
dr["ProductImageUrl"] = Convert.ToString(dtProduct.Rows[0]["ProductImageUrl"]);
dr["AvailableStock"] = lblAvailableStock;
dt.Rows.Add(dr);
Session["My Basket"] = dt;
btnShoppingBasket.Text = dt.Rows.Count.ToString();
}
}
Counter
From what I see it appears that you create new basket every time an item is added into the basket. The little mistake is that you use different names of saved basket entry in session:
if (Session["MyBasket"] != null)
This will be false first time so it goes into else block.
Session["My Basket"] = dt;
Here you store new DataTable with different key. So the next time the if statement will still be false. So just make sure that the name is consistent. "MyBasket" != "My Basket"
Fixed where "MyBasket" was "My Basket"; reduced code duplication. The misspelling is a compelling reason to use constants instead of string literals.
protected void BtnAddToBasket_Click(object sender, EventArgs e)
{
string ProductID = Convert.ToInt16((((Button)sender).CommandArgument)).ToString();
string ProductStock = "1";
DataListItem currentItem = (sender as Button).NamingContainer as DataListItem;
Label lblAvailableStock = currentItem.FindControl("lblAvailableStock") as Label;
if (Session["MyBasket"] == null)
{
DataTable dt = new DataTable();
dt.Columns.Add("ProductID", typeof(string));
dt.Columns.Add("ProductName", typeof(string));
dt.Columns.Add("ProductDescription", typeof(string));
dt.Columns.Add("ProductPrice", typeof(string));
dt.Columns.Add("ProductStock", typeof(string));
dt.Columns.Add("ProductImageUrl", typeof(string));
dt.Columns.Add("AvailableStock", typeof(string));
}
DataTable dt = (DataTable)Session["MyBasket"];
var checkProduct = dt.AsEnumerable().Where(r => r.Field<string>("ProductID") == ProductID);
if (checkProduct.Count() == 0)
{
string query = "SELECT * FROM Product WHERE ProductID = " + ProductID + "";
DataTable dtProduct = GetData(query);
DataRow dr = dt.NewRow();
dr["ProductID"] = ProductID;
dr["ProductName"] = Convert.ToString(dtProduct.Rows[0]["ProductName"]);
dr["ProductDescription"] = Convert.ToString(dtProduct.Rows[0]["ProductDescription"]);
dr["ProductPrice"] = Convert.ToString(dtProduct.Rows[0]["ProductPrice"]);
dr["ProductStock"] = ProductStock;
dr["ProductImageUrl"] = Convert.ToString(dtProduct.Rows[0]["ProductImageUrl"]);
dr["AvailableStock"] = lblAvailableStock;
dt.Rows.Add(dr);
Session["MyBasket"] = dt;
btnShoppingBasket.Text = dt.Rows.Count.ToString();
}
}

Asp.net - DropDownLists in GridView

I was trying to make a column of DropDownLists in GridView from the C# code, to no avail. Here is the code I use:
var manager=Context.GetOwinContext().GetUserManager<ApplicationUserManager>();
DataTable dt = new DataTable();
DataRow dr = null;
dt.Columns.Add("Email", typeof(String));
dt.Columns.Add("Username", typeof(String));
dt.Columns.Add("Roles", typeof(DropDownList));
var a = manager.Users.ToArray();
for (int i = 0; i < a.Length; i++ )
{
dr = dt.NewRow();
dr["Email"] = a[i].Email;
dr["Username"] = a[i].UserName;
DropDownList lista = new DropDownList();
var b = manager.Users.ToArray();
lista.DataSource = b;
lista.DataBind();
dr["Roles"] = (DropDownList)lista;
dt.Rows.Add(dr);
}
GridView1.AutoGenerateSelectButton = true;
dt.AcceptChanges();
GridView1.DataSource = dt;
GridView1.DataBind();
The first 2 colums show up, the last (which should have DropDownLists) does not. Why?

Placing DataBound Drop Down List into TableCell

I have a bound drop down list that I would like to place into a Table Cell. I am used to Labels and Text Boxes but I cannot seem to get the syntax working on this one. This code is inside of a Webmethod and my output has to be just the html. Thank you.
[System.Web.Services.WebMethod]
public static string gatherSurchargeData(string PriceListItemID, string ProdID, string ColorCode)
{
DataTable GetProductSizes = new DataTable();
GetProductSizes = DataLayer.PricingToolDL.getProductSizes(ProdID, ColorCode);
DataTable dt = new DataTable();
dt = DataLayer.PricingToolDL.getScharge(PriceListItemID);
Table tblScharges = new Table();
tblScharges.ID = "tblScharges";
TableHeaderRow th = new TableHeaderRow();
TableHeaderCell thSizeScharge = new TableHeaderCell();
thSizeScharge.Text = "Size";
th.Cells.Add(thSizeScharge);
tblScharges.Rows.Add(th);
int i = 0;
while (i <= dt.Rows.Count - 1)
{
TableRow tr = new TableRow();
tr.ID = "tableTr" + i;
TableCell tcSizeScharge = new TableCell();
DropDownList ddl = new DropDownList();
ddl.DataSource = GetProductSizes;
ddl.DataTextField = "FitSize";
ddl.DataValueField = "FitSize";
ddl.DataBind();
//string dtMovexSKU = dt.Rows[i]["MovexSKU"].ToString();
//DataRow[] GetProductSizesMovexSKU = GetProductSizes.Select("MovexSKU Like'" + dtMovexSKU + "'");
//tcSizeScharge.ID = "tcSizeScharge" + i;
//tcSizeScharge.Text = GetProductSizesMovexSKU[0][1].ToString();
tr.Cells.Add(tcSizeScharge);
tblScharges.Rows.Add(tr);
i++;
}
string html = "";
using (StringWriter sw = new StringWriter())
{
tblScharges.RenderControl(new HtmlTextWriter(sw));
html = sw.ToString();
}
return html;
}
The commented lines would be the code I would use if I were only wanting text to appear if that helps.
You need to add control in tablecell like this
tcSizeScharge.Controls.Add(ddl);

Exit ListView Update Mode after clicking button

I currently am using a listview to edit and update data. When I click edit it goes into the edit format that I want it to. I want to be able to exit the entire edit mode after the update button is clicked. Is this possible?
When I use ListView1.EditIndex = -1 it doesn't go back to the regular view.
protected void ListView1_ItemUpdating(object sender, ListViewUpdateEventArgs e)
{
updateButton = true;
ListViewItem lvl = ListView1.Items[e.ItemIndex];
var formTitleListBox = (ListBox)lvl.FindControl("ListBox1");
var controlTypeListBox = (ListBox)lvl.FindControl("ControlType");
var formSectionListBox = (ListBox)lvl.FindControl("formsection");
var sortOrderTextBox = (TextBox)lvl.FindControl("SortOrderTextBox");
var subsectionListBox = (ListBox)lvl.FindControl("subsection");
var subSectionTextBox = (TextBox)lvl.FindControl("SubSectionOrderTextBox");
var sectionItemListBox = (ListBox)lvl.FindControl("sectionitem");
var sectionSortOrderTextBox = (TextBox)lvl.FindControl("SectionSortOrderTextBox");
var validationRuleListBox = (ListBox)lvl.FindControl("RuleDesc");
var crossItemListBox = (ListBox)lvl.FindControl("CrossItem");
var hiddenID = (HiddenField)lvl.FindControl("HiddenPrimaryID");
using (SqlConnection connection = new SqlConnection("Data Source=RCK-HRSA-DB01;Initial Catalog=ORHP_Dev03182014;User ID=ohitrural;Password=0h!trural"))
{
try
{
SqlCommand cmd1 = new SqlCommand("UPDATE ORHP_Dev03182014.Core.Form_Section_SubSection_Item_Rel SET FormID = #FormTitle, FormSectionID = #FormSection, SubSectionID = #SubSection, SectionItemID = #SectionItem, SortOrder = #SortOrder, SectionSortOrder = #SectionSortOrder, SubSectionSortOrder = #SubSectionSortOrder, ValidationRulesetId = #RuleDesc, ControlTypeID = #ControlType, CrossItemID = #CrossItem WHERE DataCollectionPeriodID = " + DropDownList2.SelectedValue + " AND FormSectionSubSectionItemRelID = #FormSectionSubSectionID");
connection.Open();
cmd1.Connection = connection;
cmd1.CommandType = CommandType.Text;
cmd1.Parameters.AddWithValue("#FormTitle", formTitleListBox.SelectedValue);
cmd1.Parameters.AddWithValue("#ControlType", DbNullIfNull(controlTypeListBox.SelectedValue));
cmd1.Parameters.AddWithValue("#FormSection", formSectionListBox.SelectedValue);
cmd1.Parameters.AddWithValue("#SortOrder", DbNullIfNull(sortOrderTextBox.Text));
cmd1.Parameters.AddWithValue("#SubSection", subsectionListBox.SelectedValue);
cmd1.Parameters.AddWithValue("#SubSectionSortOrder", DbNullIfNull(subSectionTextBox.Text));
cmd1.Parameters.AddWithValue("#SectionItem", sectionItemListBox.SelectedValue);
cmd1.Parameters.AddWithValue("#SectionSortOrder", DbNullIfNull(sectionSortOrderTextBox.Text));
cmd1.Parameters.AddWithValue("#RuleDesc", DbNullIfNull(validationRuleListBox.SelectedValue));
cmd1.Parameters.AddWithValue("#CrossItem", DbNullIfNull(crossItemListBox.SelectedValue));
cmd1.Parameters.AddWithValue("#FormSectionSubSectionID", hiddenID.Value);
cmd1.ExecuteNonQuery();
SqlDataAdapter dt = new SqlDataAdapter(cmd1);
DataSet ds = new DataSet();
searchDS = new DataSet();
dt.Fill(ds);
searchDS = ds;
UpdatePanel1.Update();
ListView1.DataSource = searchDS;
ListView1.EditIndex = -1;
ListView1.DataBind();
e.Cancel = true;
}
catch (Exception ex)
{
}
}
}
Add ListView1.DataBind(); and also e.Cancel = true;.
// ...
cmd1.ExecuteNonQuery();
ListView1.EditIndex = -1;
ListView1.DataBind()
e.Cancel = true;
// ...
Side-note: remove the empty catch if you want to notice if something goes wrong.

Resources