Add Multiple series to a chart dynamically in asp.net - asp.net

I want to add dynamic series in the chart.
I have a data like date,totalamount. i would like to plot those points on chart.
I get the data from sql database and bind.
i want to plot the data from datatable which will update dynamically.
Series newSeries=new Series();
newseries.ChartType=SeriesChartType.Line;
newSeries.BorderWidth = 3;
Chart1.Series.Add(newSeries);
newSeries.XValueMember = "date1";
newSeries.YValueMembers = "total";
Chart1.DataBind();
this is plotting at last series of the tree view.
please help me on this?

foreach(DataRow row in myDataSet.Tables["Query"].Rows)
{
// For each Row add a new series
string seriesName = row["SalesRep"].ToString();
Chart1.Series.Add(seriesName);
Chart1.Series[seriesName].ChartType = SeriesChartType.Line;
Chart1.Series[seriesName].BorderWidth = 2;
for(int colIndex = 1; colIndex < myDataSet.Tables["Query"].Columns.Count; colIndex++)
{
// For each column (column 1 and onward) add the value as a point
string columnName = myDataSet.Tables["Query"].Columns[colIndex].ColumnName;
int YVal = (int) row[columnName];
Chart1.Series[seriesName].Points.AddXY(columnName, YVal);
}
}

Related

Count number of rows in DataTable that meet some criteria

DataTable dt = GetDataTable();
if(field=="TaskId")
{
string value = ((e.Item as GridFilteringItem)[field] as GridTableCell).Column.CurrentFilterValue
int VALUE = Int32.Parse(value);
int numberOfRecords = dt.Select("TaskId = VALUE").Length;
I want to count the number of rows in a DataTable that have a TaskId == value.
This code is throwing an error saying no column found TaskId[value].
Replace this line
int numberOfRecords = dt.Select("TaskId = VALUE").Length;
WITH
int numberOfRecords = dt.Select("TaskId = " + VALUE).Length;
you passed VALUE within ("")
You could use a simple for loop to iterate each row, looking at the TaskId field of each row. If it matches the criteria, increase a counter.
string value = ((e.Item as GridFilteringItem)[field] as GridTableCell).Column.CurrentFilterValue
int count = 0;
for(int i = 0; i < dt.Rows.Count; i++)
{
if(dt.Rows[i]["TaskId"].ToString() == value)
{
count++;
}
}
Either that or you could use LINQ as well. The rows variable will hold a collection of DataRows that meet your criteria.
var rows = from row in dt.AsEnumerable()
where row.Field<string>("TaskId") == value
select row;
int count = rows.Count<DataRow>();

Clone dataset > Change column type > Populate dataset

I have a dataset populated from a database:
dataset_original = new DataSet()
data_adapter.Fill(dataset_original)
and I cloned it:
dataset_cloned = dataset_original.Clone()
I cloned it because 1 of the columns in the original is of type int, and I want to change that to type string:
dataset_cloned.Tables(0).Columns("int_column_name_goes_here").DataType = GetType(String)
Now I need to populate the new dataset with the data from the old dataset. How do I do that?
I am using asp.net 1.1 coded with vb.net.
This simple loop should work (even with OPTION STRICT ON):
Dim dataset_cloned = dataset_original.Clone()
dataset_cloned.Tables(0).Columns("int_column_name_goes_here").DataType = GetType(String)
For i As Int32 = 0 To dataset_original.Tables.Count - 1
Dim tbl_original As DataTable = dataset_original.Tables(i)
Dim tbl_cloned As DataTable = dataset_cloned.Tables(i)
For Each row As DataRow In tbl_original.Rows
tbl_cloned.Rows.Add(row.ItemArray)
Next
Next
assuming you have only one table in the data set, you can do something like this.
int ColumnIndex = 0; //Column index of your data you want to copy
for (int i = 0; i < dataset_original.Tables[0].Rows.Count; i++)
{
dataset_cloned.Tables[0].Rows[i].SetField(ColumnIndex, dataset_original.Tables[0].Rows[ColumnIndex].ItemArray[0].ToString());
}
in the same for loop you can copy remaining columns data

Specific row withing a DataTable

I have a data table which has a "Total" column. I want to be able to get a specific rows "Total" not all rows.
public void maxValue()
{
string pass = (String)Session["name"];
DataTable table = (DataTable)Session["CocaCola"];
int total = table.AsEnumerable().Sum(r => r.Field<int>("Total"));
int totalAllowed = table.AsEnumerable().Sum(r => r.Field<Int32>("Total Allowed"));
if (total >= totalAllowed)
{
Label1.Text = "Total value exceeded the maximum of " + totalAllowed;
}
else if (total < totalAllowed)
{
Label1.Text = "Total value which is allowed :" + totalAllowed;
}
if (pass.Equals("Low"))
{
Label1.Text = "You are not allowed any assets at this Stage";
//SNS.Checked = false;
//TT.Checked = false;
//Music.Checked = false;
//SNS.Enabled = false;
//TT.Enabled = false;
//Music.Enabled = false;
}
}
As you can see my method works but add the column up which i dont want to do. How would i go about changing it?
You can do it this way
int yourTargetindex = 0; //Change this to get the value of your target element
int total =(from row in table.AsEnumerable()
select row.Field<int>("Total")).ElementAt(yourTargetindex);
//This will return the first value of "total" in the DataTable
You don't have to use linq. DataTable has built-in methods for this kind of operations:
var selectedTotal = table.Compute("sum(Total)", "columnX == 'x'");
This tell the table to calculate the sum of all Total cells in rows where columnX has the specified value.
Of course you can use linq. You would need to add a Where() before you calculate the sum.

Extracting values of textbox in array?

I have dynamically created textbox in asp.net. Now i am extracting the values through following code.
string[] sublist = new string[] { };
int[] maxmarkslist = new int[] { };
int i;
for (i = 0; i < Convert.ToInt32(Label15.Text); i++)
{
string sub = "subject" + i;
string marks = "maxmarks" + i;
TextBox subject = (TextBox)PlaceHolder1.FindControl(sub);
TextBox maxmarks = (TextBox)PlaceHolder1.FindControl(marks);
sublist[i] = subject.Text;
maxmarkslist[i] = Convert.ToInt32(maxmarks.Text);
}
But I getting error "Index was outside the bounds of the array" for the below two lines:
sublist[i] = subject.Text;
maxmarkslist[i] = Convert.ToInt32(maxmarks.Text);
When I debugged it, values are coming in subject.Text and maxmarks.Text but not going to array.
Did I define the array in a wrong way?
You define both the arrays as empty arrays. So you will get index out of bound erros if you try to index into those.
Arrays are not dynamically expanding. If you want that, use a collection type and may be later convert to an array.
Try this:
int length = Convert.ToInt32(Label15.Text);
string[] sublist = new string[length-1];
int[] maxmarkslist = new int[length-1];
for (int i = 0; i < length; i++)
{
string sub = "subject" + i;
string marks = "maxmarks" + i;
TextBox subject = (TextBox)PlaceHolder1.FindControl(sub);
TextBox maxmarks = (TextBox)PlaceHolder1.FindControl(marks);
sublist[i] = subject.Text;
maxmarkslist[i] = Convert.ToInt32(maxmarks.Text);
}
Or here is how to do this with a collection (List) type:
int length = Convert.ToInt32(Label15.Text);
List<string> sublist1 = new List<string>();
List<int> maxmarkslist1 = new List<int>();
for (int i = 0; i < Convert.ToInt32(Label15.Text); i++)
{
string sub = "subject" + i;
string marks = "maxmarks" + i;
TextBox subject = (TextBox)PlaceHolder1.FindControl(sub);
TextBox maxmarks = (TextBox)PlaceHolder1.FindControl(marks);
sublist1.Add(subject.Text);
maxmarkslist1.Add(Convert.ToInt32(maxmarks.Text));
}
string[] sublist = sublist1.ToArray();
int[] maxmarkslist = maxmarkslist1.ToArray();
Note with collections you dont have to specify the size upfront. But keep adding items to it as it can expand as needed. But arrays can not do this.
Your string[] sublist = new string[] { }; is a shortcut method where you create and initialize the array. In that you don't have to specify the size, but compiler will count the elements between {} and set the size appropriately. In your case since there are no elements inside {} it will create an empty array.
string[] sublist = new string[100];
int[] maxmarkslist = new int[100];
Put this..replace 100 with the max possible value of your loop...but this is not a good practice...will come back to this thread if i found something better...

how to calculate the working days in between two different dates in asp.net

i have two textbox using calaendarExtender and one label.
My needed is if i select two different dates in calendar extender the number of working days(excluding Sunday) to be automatically display in the label.
can any one help me.....
im new in ASP.net......
What about this:
DateTime start = new DateTime(2010, 12, 1);
DateTime end = new DateTime(2010, 12, 31);
int workdays = 0;
DateTime aux = start;
while(aux <= end)
{
aux = aux.AddDays(1);
if (aux.DayOfWeek != DayOfWeek.Sunday)
workdays++;
}
yourLabel.Text = workdays.ToString();

Resources