Repeater binding with for loop to retrieve multiple values from database - asp.net

This my code i want to fetch values fron database and insert in the repeater this code only returns last value from database
for (int i = 0; i < dtFinalVessel.Rows.Count; i++)
if (i == 0)
{
if (e.Item.ItemType == ListItemType.Item)
{
DataTable dtCostElement = DAL.MonthlyCrudeAccrualsDetail.GetMonthlyCrudeAccrualsPerFinalBolID_Get_List(int.Parse(dtFinalVessel.Rows[0]["FinalVesselPerBOLID"].ToString()));
ArrayList arrlist = new ArrayList();
Repeater rptrCostElementHeader = (Repeater)e.Item.FindControl("rptrCostElementHeader");
rptrCostElementHeader.DataSource = dtCostElement;
rptrCostElementHeader.DataBind();
Repeater rptrUS = (Repeater)e.Item.FindControl("rptrUS");
rptrUS.DataSource = dtCostElement;
rptrUS.DataBind();
Repeater rptrZAR = (Repeater)e.Item.FindControl("rptrZAR");
rptrZAR.DataSource = dtCostElement;
rptrZAR.DataBind();
Repeater rptrUSCent = (Repeater)e.Item.FindControl("rptrUSCent");
rptrUSCent.DataSource = dtCostElement;
rptrUSCent.DataBind();
for (int j = 0; j < dtCostElement.Rows.Count; j++)
{
string strUscent = dtCostElement.Rows[j]["USCent"].ToString();
decimal total = decimal.Parse(strUscent.ToString()) / decimal.Parse(hdnTotalVessel.Value.ToString());
arrlist.Add(total.ToString());
}
Repeater rptAvgUSCent = (Repeater)e.Item.FindControl("rptAvgUSCent");
rptAvgUSCent.DataSource = arrlist;
rptAvgUSCent.DataBind();`
}
}
}

Related

Dynamically created textbox value in gridview looping issue

In the below code i have created dynamic textbox in grid and i am saving the values in database .In my case there is only two rows created in gridview but in database it is saving 4 rows.Pls help me to solve the issue.
TestSchool.SchoolBusinessLyr.SchoolBizClient Grade = new TestSchool.SchoolBusinessLyr.SchoolBizClient();
System.Collections.Generic.Dictionary<string, string> AssignGrade = new System.Collections.Generic.Dictionary<string, string>();
foreach (GridViewRow row in gdassignmark.Rows)
{
int rowIndex = 0;
for (int i = 0; i < gdassignmark.Rows.Count; i++)
{
//extract the TextBox values
TextBox Sa = (TextBox)gdassignmark.Rows[i].Cells[i].FindControl("txtsa");
TextBox fa = (TextBox)gdassignmark.Rows[i].Cells[i].FindControl("txtfa");
// var SubjectID = gdassignmark.DataKeys[rowIndex]["SubjectID"] as string;
String cellText = row.Cells[0].Text;
String cellText1 = row.Cells[2].Text;
if (cellText != string.Empty)
{
if (fa.Text == "0")
{
int faval = int.Parse(fa.Text);
int mark = (faval / 40) * 100;
string strmark = mark.ToString();
AssignGrade.Add("BranchID", dpbranch.SelectedValue);
AssignGrade.Add("Academicyear", dpacademicyear.SelectedValue);
AssignGrade.Add("ExamID", dpExamName.SelectedValue);
AssignGrade.Add("ClassID", dpClassName.SelectedValue);
AssignGrade.Add("SectionID", "1");
AssignGrade.Add("SubjectID", cellText.ToString());
AssignGrade.Add("StudentID", dpStudentName.SelectedValue);
AssignGrade.Add("FA", Sa.Text);
AssignGrade.Add("SA", "");
AssignGrade.Add("FAandSA", (strmark));
Grade.InsertStudentGrade(AssignGrade);
}}
You are looping through the Rows twice. Assuming that Rows[i]Cells[i] is correct you can change your code to not loop through all rows for each row:
//foreach (GridViewRow row in gdassignmark.Rows)
//{
int rowIndex = 0;
for (int i = 0; i < gdassignmark.Rows.Count; i++)
{
GridViewRow row = gdassignmark.Rows[i]
//Do your stuff here
}
//}
You are looping over your gridview twice
Remove outer foreach so your code should look like this
TestSchool.SchoolBusinessLyr.SchoolBizClient Grade
= new TestSchool.SchoolBusinessLyr.SchoolBizClient();
System.Collections.Generic.Dictionary<string, string> AssignGrade
= new System.Collections.Generic.Dictionary<string, string>();
for (int i = 0; i < gdassignmark.Rows.Count; i++)
{
//extract the TextBox values
TextBox Sa = (TextBox)gdassignmark.Rows[i].Cells[i].FindControl("txtsa");
TextBox fa = (TextBox)gdassignmark.Rows[i].Cells[i].FindControl("txtfa");
// var SubjectID = gdassignmark.DataKeys[rowIndex]["SubjectID"] as string;
String cellText = row.Cells[0].Text;
String cellText1 = row.Cells[2].Text;
if (cellText != string.Empty)
{
if (fa.Text == "0")
{
int faval = int.Parse(fa.Text);
int mark = (faval / 40) * 100;
string strmark = mark.ToString();
AssignGrade.Add("BranchID", dpbranch.SelectedValue);
AssignGrade.Add("Academicyear", dpacademicyear.SelectedValue);
AssignGrade.Add("ExamID", dpExamName.SelectedValue);
AssignGrade.Add("ClassID", dpClassName.SelectedValue);
AssignGrade.Add("SectionID", "1");
AssignGrade.Add("SubjectID", cellText.ToString());
AssignGrade.Add("StudentID", dpStudentName.SelectedValue);
AssignGrade.Add("FA", Sa.Text);
AssignGrade.Add("SA", "");
AssignGrade.Add("FAandSA", (strmark));
Grade.InsertStudentGrade(AssignGrade);
}
}

ASP.NET Dynamically add rows into existing Table while looping through XML nodes

What im trying to do is to populate an empty Table with cells and the number of cells per row is 4 and the number of all cells is the number of nodes in btnNode
The empty table ID = "MainTable"
and so far i created this but it seems i get an error in
TableRowCollection rows = new TableRowCollection();
has no constructors defined.Searched in google and didnt find help to solve this issue
Here is my code :
private void linkBTN_Click(object sender, EventArgs e)
{
LinkButton btn = (LinkButton)sender;
string text = btn.Text.ToString();
XmlDocument clickDoc = new XmlDocument();
clickDoc.Load(Server.MapPath("~/ProductShow.xml"));
XmlNodeList btnNode = clickDoc.SelectNodes("products/" + text.ToString() + "/*");
int count = btnNode.Count;
TableRowCollection rows = new TableRowCollection();
if (count < 4)
{
TableRow row = new TableRow();
rows.Add(row);
}
else
{
for (int i = 0; i < count / 4; i++)
{
TableRow row = new TableRow();
rows.Add(row);
}
}
int j = 1;
foreach (XmlNode node in btnNode)
{
//TableRow row = new TableRow();
TableCell cell = new TableCell();
cell.CssClass = "Cell";
LinkButton linkbtn = new LinkButton();
linkbtn.Text = node.InnerText;
linkbtn.Attributes.Add("runat", "server");
Image img = new Image();
cell.Controls.Add(linkbtn);
cell.Controls.Add(new LiteralControl("<br/>"));
cell.Controls.Add(img);
rows[j].Cells.Add(cell);
j++;
}
foreach (TableRow row in rows)
{
MainTable.Rows.Add(row);
}
}

Unable to add data in a datatable asp.net

DataRow new_row = tb11.NewRow();
for (i = 0; i < 8; i++)
{
new_row[i] = tb11.Rows[0][i].ToString();
}
tb11.Rows.Add(new_row);
I am running the following code to add data to a existing datatable but no data is inserted into the databale.
DataTable dt = ds3.Tables["FormName"];
dt.Columns.Add();
dt.Columns.Add();
dt.Columns.Add();
dt.Columns[2].ColumnName = "EmpId";
dt.Columns[3].ColumnName = "EmpName";
dt.Columns[4].ColumnName = "Branch";
for (int i = 0; i < dt.Rows.Count; i++)
{
dt.Rows[i]["EmpId"] = UID;
dt.Rows[i]["EmpName"] = EmpName;
dt.Rows[i]["Branch"] = Zone;
}
dt.AcceptChanges();
foreach (DataRow row in dt.Rows)
{
string NEmpId = dt.Rows[j]["EmpId"].ToString();
string NEmpName = dt.Rows[j]["EmpName"].ToString();
}
you are calling newrow() on the same table tb11 from where you get your source data and adding new row in same table tb11. Also tb11.Rows[0] will access first row everytime

how to find which imagebutton was clicked?

I have created a function which displays a bus seat layout, dynamically. Now after adding all this dynamically created table and cells into a tag, I want to know which imagebutton was clicked.I have also mentioned proper ID to each imagebutton in cell.
public void DisplaySeatLayout(ListBus _listBus) {
//fetching data from database
SeatBUS _seatBUS = new SeatBUS();
DataTable dt = _seatBUS.GetAllSeatByBusRouter(_listBus);
//Layout generation code
ImageButton img ;
HtmlTable table = new HtmlTable();
table.Attributes.Add("runat", "server");
table.Attributes.Add("id", "LayoutTable");
HtmlTableRow [] tr = new HtmlTableRow[] { new HtmlTableRow(), new HtmlTableRow(), new HtmlTableRow(),new HtmlTableRow(),new HtmlTableRow()};
HtmlTableCell tc = null;
int SeatNo=0;
//Getting Total no of seats.
int MaxSeatNo = dt.Rows.Count;
//Iterating datatable
//Displaying labels for displaying column names in the table
for (int columnCounter = 0; columnCounter < 8; columnCounter++){
for (int rowCounter = 0; rowCounter < 5; rowCounter++){
if (SeatNo < MaxSeatNo){
if (rowCounter == 2 && columnCounter < 7){
tc = new HtmlTableCell();
Label lbl = new Label();
lbl.Text = "";
lbl.ID = "lbl " + rowCounter.ToString() + columnCounter.ToString();
tc.Controls.Add(lbl);
tr[rowCounter].Controls.Add(tc);
//reducing seat number for sake of sequence.
}
else{
//adding label in each cell.
tc = new HtmlTableCell();
Label lbl = new Label();
lbl.Text = dt.Rows[SeatNo]["NumberSeat"].ToString();
lbl.ID = "lbl " + rowCounter.ToString() + columnCounter.ToString();
tc.Controls.Add(lbl);
//adding imagebutton in each cell .
img = new ImageButton();
img.Attributes.Add("type", "image");
img.Attributes.Add("id", rowCounter.ToString());
img.CssClass = "seatRightMostRow1";
img.ImageUrl = "../Images/available_seat_img.png";
img.ID = dt.Rows[SeatNo]["NumberSeat"].ToString();
img.Click += new ImageClickEventHandler(Imagebutton_Click);
tc.Controls.Add(img);
tr[rowCounter].Controls.Add(tc);
SeatNo++;
}
}//SeatNo < MaxSeatNo
table.Controls.Add(tr[rowCounter]);
}
seatArranngement.Controls.Add(table);
}
}
this is complete code after suggestion.
function that is displaying dynamic table is inside Page_load
protected void Page_Load(object sender, EventArgs e)
{
_listBusBUS = new ListBusBUS();
_routerBUS = new RouterBUS();
_listBus = new ListBus();
_promoteBUS = new PromoteBUS();
if (!Page.IsPostBack)
{
LoadData();
}
DisplaySeatLayout();
}
Also here i have copied code in prerender
protected override void OnPreRender(EventArgs e)
{
if (MultiView1.ActiveViewIndex == 1)
{
int listBusID = int.Parse(hdlListBusID.Value.ToString());
_listBus = _listBusBUS.GetAllListBusById(listBusID);
litListBusID.Text = _listBus.ListBusID.ToString();
litRouterID.Text = _listBus.RouterID.ToString();
litArrival.Text = _listBus.Arrival.ToString();
litDeparture.Text = _listBus.Departure.ToString();
litPrice.Text = _listBus.Price.ToString();
}
else if (MultiView1.ActiveViewIndex == 2)
{
//original code starts from here
litListBusIDS.Text = litListBusIDS.Text;
litRouterIDS.Text = litRouterID.Text;
litArrivalS.Text = litArrival.Text;
litDepartureS.Text = litDeparture.Text;
litSeat.Text = hdlSeat.Value;// chkSeat.SelectedItem.Text;
litPrices.Text = litPrice.Text;
//hdlSeat.Value = chkSeat.SelectedItem.Text.ToString();
}
else if (MultiView1.ActiveViewIndex == 3)
{
litCustomerNameStep4.Text = txtcustomername.Text;
litSeatStep4.Text = litSeat.Text;
litPriceStep4.Text = litPrices.Text;
}
base.PreRender(e);
}
Also, layout is getting created twice if i click on any imagebutton.
All you have to do is get the value that you already placed inside the "id" attribute.
You can do this inside the Imagebutton_Click even you created:
protected void Imagebutton_Click(object sender, EventArgs e)
{
ImageButton b = sender as ImageButton;
string id = b.Attributes["id"]; // Returns the id
}

How do I group rows in asp.net's datalist control?

For example in my database there are 96 rows and what I want to happen is to group rows by four(the resulting table will have 3 columns with 8 rows). ex
You might have a much easier time if you can get away with a control other than a DataList. For instance, you could use a Table server control like this:
<asp:Table ID="tblGrouped" runat="server"></asp:Table>
And then in code:
protected void LoadData() {
var items = MyDataSource.GetMyItems();
TableRow tr = null;
TableCell tc = null;
for (int i = 0; i < items.Count; i++) {
if (i % 12 == 0) {
tr = new TableRow();
tc = new TableCell();
tc.Text = items[i].MyProperty;
tr.Cells.Add(tc);
tblGrouped.Rows.Add(tr);
} else if (i % 4 == 0) {
tc = new TableCell();
tc.Text = items[i].MyProperty;
tr.Cells.Add(tc);
} else {
tc.Text += "<br />" + items[i].MyProperty;
}
}
}

Resources