why pie chart is showing value for only one field? - asp.net

I am trying to use a pie chart and I expect 2 regions get displayed on it. But for my following code, only the last one is shown.
Code behind:
protected void Page_Load(object sender, EventArgs e)
{
DataTable dt = new DataTable();
dt.Columns.Add("status");
dt.Columns.Add("count");
DataRow dr = dt.NewRow();
dr["status"] = "safe";
dr["count"] = loadChart("safe");
dt.Rows.Add(dr);
dr["status"] = "unsafe";
dr["count"] = loadChart("unsafe");
dr = dt.NewRow();
dt.Rows.Add(dr);
Chart1.DataSource = dt;
Chart1.Series[0].XValueMember = "status";
Chart1.Series[0].YValueMembers = "count";
Chart1.DataBind();
}
Mark up:
<asp:Chart ID="Chart1" runat="server">
<series>
<asp:Series ChartType="Pie" Name="Series1">
</asp:Series>
</series>
<chartareas>
<asp:ChartArea Name="ChartArea1">
</asp:ChartArea>
</chartareas>
</asp:Chart>
output:
I want to see the safe portion also. I have value for it.how to resolve this issue ?

You are setting values in the same row twice:
DataRow dr = dt.NewRow();
dr["status"] = "safe";
dr["count"] = loadChart("safe");
dt.Rows.Add(dr);
//still in the same row
dr["status"] = "unsafe";
dr["count"] = loadChart("unsafe");
dr = dt.NewRow();
dt.Rows.Add(dr);
you should create new row and then set second values:
DataRow dr = dt.NewRow();
dr["status"] = "safe";
dr["count"] = loadChart("safe");
dt.Rows.Add(dr);
//new row creation
dr = dt.NewRow();
dr["status"] = "unsafe";
dr["count"] = loadChart("unsafe");
dt.Rows.Add(dr);

Related

Display Same data in row and column in asp.net gridview from codebehind?

enter image description hereDisplay Same data in row and column in asp.net gridview from codebehind ?
It is not clear from your question but try this.
//Create your Original DataSource
DataTable dt = new DataTable();
DataColumn dc1 = new DataColumn("col1");
DataColumn dc2 = new DataColumn("col2");
DataColumn dc3 = new DataColumn("col3");
dt.Columns.Add(dc1);
dt.Columns.Add(dc2);
dt.Columns.Add(dc3);
DataRow dr = dt.NewRow();
dr[0] = "A";
dr[01] = "B";
dr[2] = "C";
dt.Rows.Add(dr);
//Adding Rows
for (int i = 1; i < dt.Columns.Count; i++)
{
DataRow dr1 = dt.NewRow();
dr1[0] = dt.Rows[0][i].ToString();
dt.Rows.Add(dr1);
}
GridViewName.DataSource = dt;
GridViewName.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?

adding textbox in dataTable

I want to add textBox in my dataTable.My dataTable is dynamically created and bind to gridView.When I try to add new row on button click gives me this error:
Index was out of range. Must be non-negative and less than the size of
the collection.
Parameter name: index
TextBox TextBox1 = (TextBox)GridView2.Rows[0].Cells[2].FindControl("TextBox1");
Here is my code:
Markup:
<asp:GridView ID="GridView2" runat="server"
ShowHeader="true"
OnRowDataBound="GridView2_RowDataBound">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:Button ID="btnTest" runat="server"
CssClass="button2"
OnClick="btnTest_Click"
Text="-"
Width="100px"
Font-Bold="True" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="TextBox1">
<ItemTemplate>
<asp:TextBox ID="TextBox1" runat="server">
</asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
Code Behind:
private void AddNewRecordRowToGridf()
{
//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)));
dt.Columns.Add(new DataColumn("TarifaID", typeof(System.Int32)));
dt.Columns.Add(new DataColumn("Zabeleshka", typeof(System.String)));
dr = dt.NewRow();
dt.Rows.Add(dr);
ViewState["Markici2"] = dt;
if (ViewState["Markici2"] != null)
{
DataTable dtCurrentTable = (DataTable)ViewState["Markici2"];
DataRow drCurrentRow = null;
if (dtCurrentTable.Rows.Count > 0)
{
for (int i = 1; i <= dtCurrentTable.Rows.Count; i++)
{
TextBox TextBox1 = (TextBox)GridView2.Rows[0].Cells[2].FindControl("TextBox1");
//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;
if (Session["MinusVrganj"] != null)
{
drCurrentRow["Kolicina"] = Convert.ToInt32(Session["MinusVrganj"]) + Counter; //Label5.Text; //count;
}
drCurrentRow["Kolicina"] = Label11.Text; //count;
drCurrentRow["Smena"] = Session["smena1"];
drCurrentRow["VkIznos"] = Label17.Text;//Counter1 * Convert.ToDecimal(Label13.Text); //Label17.Text;
drCurrentRow["VkDanok"] = Label18.Text;
drCurrentRow["SySDatum"] = DateTime.Now;
drCurrentRow["Vid"] = Label24.Text;
drCurrentRow["Edmera"] = Label16.Text;
drCurrentRow["ArtikalID"] = Label34.Text;
drCurrentRow["TarifaID"] = Label14.Text;
drCurrentRow["Zabeleshka"] = TextBox1.Text;
}
dtCurrentTable.Rows.Add(drCurrentRow);
//storing DataTable to ViewState
ViewState["Markici"] = dtCurrentTable;
//binding Gridview with New Row
GridView2.DataSource = dtCurrentTable;
GridView2.DataBind();
}
}
}

how to add row in temporary table data table in asp.net

protected void Add_New_Click(object sender, System.EventArgs e)
{
DataTable dt= new DataTable();
DataRow dr;
dr = dt.NewRow();
string CustomerID = ((TextBox)GridView1.FooterRow.FindControl("txtCustomerID")).Text;
string CustomerName = ((TextBox)GridView1.FooterRow.FindControl("txtCustomerName")).Text;
string CustomerFatherName = ((TextBox)GridView1.FooterRow.FindControl("txtCustomerFatherName")).Text;
dt.Rows.Add(dr);
GridView1.DataSource = dt;
GridView1.DataBind();
ViewState["sajjad_Viewstate"] = dt;
}
Try this:
protected void Add_New_Click(object sender, System.EventArgs e)
{
DataTable dt= new DataTable();
// Add columns here
dt.Columns.Add(new DataColumn("CustomerID"));
dt.Columns.Add(new DataColumn("CustomerName"));
dt.Columns.Add(new DataColumn("CustomerFatherName"));
//Create new row
DataRow dr= dt.NewRow();
string CustomerID = ((TextBox)GridView1.FooterRow.FindControl("txtCustomerID")).Text;
string CustomerName = ((TextBox)GridView1.FooterRow.FindControl("txtCustomerName")).Text;
string CustomerFatherName = ((TextBox)GridView1.FooterRow.FindControl("txtCustomerFatherName")).Text;
//populate columns with data
dr["CustomerID"] = CustomerID;
dr["CustomerID"] = CustomerName;
dr["CustomerFatherName"] = CustomerFatherName;
dt.Rows.Add(dr);
GridView1.DataSource = dt;
GridView1.DataBind();
ViewState["sajjad_Viewstate"] = dt;
}
dt.Columns.Add("ItemName", typeof(string));
dt.Columns.Add("ItemCode", typeof(string));
dt.Columns.Add("Price", typeof(decimal));
dt.Columns.Add("Qty", typeof(decimal));
dt.Columns.Add("Amount", typeof(decimal));
dr = dt.NewRow();
dr[0] = txtitemname.Text;
dr[1] = txtitemcode.Text;
dr[2] = Convert.ToDecimal(txtprice.Text);
dr[3] = Convert.ToDecimal(txtqty.Text);
dr[4] = Convert.ToDecimal(txtamount.Text);
dt.Rows.Add(dr);
dt.AcceptChanges();
ViewState["DtItem"] = dt;
}
else
{
dt = (DataTable)ViewState["DtItem"];
dr = dt.NewRow();
dr[0] = txtitemname.Text;
dr[1] = txtitemcode.Text;
dr[2] = Convert.ToDecimal(txtprice.Text);
dr[3] = Convert.ToDecimal(txtqty.Text);
dr[4] = Convert.ToDecimal(txtamount.Text);
dt.Rows.Add(dr);
dt.AcceptChanges();
ViewState["DtItem"] = dt;
}
grditem.DataSource =(DataTable)ViewState["DtItem"];

Resources