I am having trouble displayin the values of list itmes in the proper order. The following are the fields in the list (with their actual system names):
-Title
-Category
-NominatedWon
-Logo
The "Title" field contains year values eg, "2011", "2010", etc.
//getting the awards list and extracting correct values from the necesary fields
//asp running with elevated privilegs
SPSecurity.RunWithElevatedPrivileges(delegate()
{
using (SPSite site = new SPSite(webUrl))
{
using (SPWeb web = site.OpenWeb())
{
try
{
SPList awardsList = web.Lists["Awards"];
SPListItemCollection listItemCollection = awardsList.Items;
//Creating the table
Table tbl = new Table();
//foreach (SPListItem oListItem in listItemCollection)
int x = listItemCollectionI.Count;
for(int i = 0; (i * 2) < x; i++) // divide total item collection by two, each loop
iteration, add two awards to a row (left cell, right cell)
{
// get listItemCollection[i];
//Create table rows, table cells
int leftIndexer = i * 2;
int rightIndexer = (i * 2) + 1;
if (leftIndexer == x)
{
break;
}
TableRow tblRow = new TableRow();
TableCell tblCellLeft = new TableCell(); //for the awards in the first column
TableCell tblCellRight = new TableCell(); //for the awards in the second column
tblCellLeft.VerticalAlign = VerticalAlign.Top;
tblCellLeft.HorizontalAlign = HorizontalAlign.Center;
tblCellLeft.CssClass = ("style5");
tblCellRight.VerticalAlign = VerticalAlign.Top;
tblCellRight.HorizontalAlign = HorizontalAlign.Center;
// get the values
awardYear = listItemCollection[leftIndexer]["Title"].ToString();
awardCategory = listItemCollection[leftIndexer]["Category"].ToString();
awardNomWon = listItemCollection[leftIndexer]["NominatedWon"].ToString();
if(listItemCollection[leftIndexer]["Logo"] != null)
awardLogo = (string)listItemCollection[leftIndexer]["Logo"];
// add to left cell
//values for the left column
tblCellLeft.Controls.Add(new LiteralControl("<div class=\"style1\">" + awardYear +
"</div>"));
tblCellLeft.Controls.Add(new LiteralControl("<div class=\"style2\">" + awardCategory
+ "</div>"));
tblCellLeft.Controls.Add(new LiteralControl("<div class=\"style3\">" + awardNomWon +
"</div>"));
tblCellLeft.Controls.Add(new LiteralControl("<div class=\"style4\">" + "<img src=" +
awardLogo.Replace(",", "") + "</div>"));
// add left cell to row
tblRow.Cells.Add(tblCellLeft);
if (rightIndexer < x) // if this item exists in the collection (prevent bug with odd
number of awards)
{
// get the values
awardYear = listItemCollection[rightIndexer]["Title"].ToString();
awardCategory = listItemCollection[rightIndexer]["Category"].ToString();
awardNomWon = listItemCollection[rightIndexer]["NominatedWon"].ToString();
if (listItemCollection[rightIndexer]["Logo"] != null)
awardLogo = (string)listItemCollection[rightIndexer]["Logo"];
// add to right cell
//Values for the right column
tblCellRight.Controls.Add(new LiteralControl("<div class=\"style1\">" + awardYear +
"</div>"));
tblCellRight.Controls.Add(new LiteralControl("<div class=\"style2\">" + awardCategory
+ "</div>"));
tblCellRight.Controls.Add(new LiteralControl("<div class=\"style3\">" + awardNomWon +
"</div>"));
tblCellRight.Controls.Add(new LiteralControl("<div class=\"style4\">" + "<img src=" +
awardLogo.Replace(",", "") + "</div>"));
// add right cell to row
tblRow.Cells.Add(tblCellRight);
}
// add row to table
tbl.Rows.Add(tblRow);
}
PlaceHolder6.Controls.Add(tbl); // add table outside of loop
}
catch (Exception err)
{
PlaceHolder3.Controls.Add(new LiteralControl(err.ToString()));
}
}
}
});
So the output for some reason seems to show the two latest lists items eg list item "2012", and list item "2011" are displayed at the bottom. In theory these should be displayed at the top followed by the rest of the list items.
Any sugestions on this will be greatly appreciated!
Thanks
There's nothing in your code to indicate the order, use something along the lines of:
SPList awardsList = web.Lists["Awards"];
SPQuery q = new SPQuery();
q.Query="<OrderBy><FieldRef Name='Title' /></OrderBy>";
SPListItemCollection listItemCollection = awardsList.Items.GetItems(q);
Don't you have to use something like:
listItemCollection.OrderBy(i=>i.Title).ThenBy(i=>i.Category).ThenBy(i=>i.NominatedWon).ThenBy(i=>i.Logo)
Related
I have two string lists:
currentRow = contains the info that the row should have
currentCol = contains the names of the columns that data from currentRow should go in.
each List contains 25(0-24) items, and is ordered in the same way as the dataRow it should be written to.
I am filling the Lists here, from labels and textboxes on a form:
List<string> currentRow = new List<string>();
List<string> currentCol = new List<string>();
foreach (var c in form11.Controls)
{
if (c.GetType() == typeof(TextBox))
{
var str = c.ToString();
var str1 = str.Substring(35);
currentRow.Add(str1);
}
if (c.GetType() == typeof(Label))
{
var str = c.ToString();
var str1 = str.Substring(34);
currentCol.Add(str1);
}
}
I then select the row in the dataTable that needs to be updated from the 3rd item in currentRow, which is a unique identifier.
var updateRow = arraysDt.Select("SERIAL =" + "'" + currentRow.ElementAtOrDefault(2) + "'");
Now i try to update the row from the items in the Lists here:
for (int i = 0; i < currentRow.Count; i++)
{
//MessageBox.Show(currentCol.ElementAtOrDefault(i).ToString() + " " + currentRow.ElementAtOrDefault(i).ToString());
updateRow[0][currentCol.ElementAtOrDefault(i)] = currentRow.ElementAtOrDefault(i);
}
As soon as it gets inside the for loop i throws a "index was out of bounds of the array" error.
As i said, currentCol contains column names and currentRow is the value.
So when it get here i expect it to find the column name and then update it with the value.
updateRow[0][currentCol.ElementAtOrDefault(i)] = currentRow.ElementAtOrDefault(i);
What am i doing wrong?
I have found out the issue:
"SERIAL =" + "'" + currentRow.ElementAtOrDefault(2) + "'"
will give me this:
SERIAL=' XXXXX'
What i need is:
SERIAL='XXXXX'
so to fix it i did:
string SMnum = currentRow.ElementAt(2).ToString().Replace(" ", string.Empty);
string query = string.Format("SERIAL='{0}'", SMnum.Replace(#"'", "''"));
var updateRow = arraysDt.Select(query);
This removes any white space in the string that i am looking for.
I'm a C# newbie_and in programming in general_ and in a previous question C# return linq result as a list from a wcf service method then use it in aspx web forms page , I managed to return a row from a table and display the result in labels in my apx web forms page. Now I want to display the whole table-> an unknown number of rows. I edited my code and I was almost successful. The problem is that the table that I get, instead of the four different rows that my table contains, displays the first one four times. I check again and again but I can't find the error nor some friends. Here is my code:
staffPanel.aspx.cs
protected void Page_Load(object sender, EventArgs e)
{
displayClients_Ref.IdisplayClientsSrvcClient dcClient = new displayClients_Ref.IdisplayClientsSrvcClient();
List<string> allClients = new List<string>(dcClient.displayClients());
foreach (string row in allClients)
{
int size = 0;
string client = allClients.FirstOrDefault();
if (String.IsNullOrEmpty(client))
{
// record cannot be found
}
else
{
string[] columns = client.Split(';');
size = columns.Length;
TableRow tr = new TableRow();
allClients_tbl.Rows.Add(tr);
for (int i = 0; i < size; i++)
{
TableCell tc = new TableCell();
tc.Text = columns[i];
tr.Cells.Add(tc);
}
}
}
}
displayClient.svc.cs
public List<string> displayClients()
{
List<string> result = new List<string>();
try
{
using (paragon_db_Models.clients_Entity context = new paragon_db_Models.clients_Entity())
{
var query = from cl in context.clients
select cl;
foreach (var c in query)
{
string row = c.user_account_id + ";" + c.client_name + ";" + c.client_surname + ";" + c.business_name + ";" + c.client_address + ";" + c.postal_code + ";" + c.telephone_number + ";" + c.fax + ";" + c.email + ";" + c.fiscal_code + ";" + c.public_fiscal_service;
result.Add(row);
}
}
return result;
}
catch (Exception)
{
return result;
}
}
If it is a simple, stupid little mistake that I cannot see I will remove the question. I'm open to suggestions and comments concerning a different way of doing this.
You don't need
string client = allClients.FirstOrDefault();
You are already getting the info you want with "row" in
foreach (string row in allClients)
Replace your "client" instances with "row". FirstOrDefault would always get the first object or null.
I am having this error: Multiple controls with the same ID 'ddl_Weight' were found. Find-control requires that controls have unique IDs. thus i am not add to add more objective text-box, drop-down list.
I have to add objectives, drop-down list for weightage and achieved.
private List<DropDownList> inputDropDownList;
private List<DropDownList> inputDropDownList2;
protected void btn_AddObjectives_Click(object sender, EventArgs e)
{
int rowCount = 0;
//initialize a session.
rowCount = Convert.ToInt32(Session["clicks"]);
rowCount++;
//In each button click save the numbers into the session.
Session["clicks"] = rowCount;
//Create the textboxes and labels each time the button is clicked.
for (int i = 0; i < rowCount; i++)
{
TextBox TxtBoxO = new TextBox();
TxtBoxO.TextMode = TextBoxMode.MultiLine;
DropDownList DDLW = new DropDownList();
DropDownList DDLA = new DropDownList();
inputDropDownList = new List<DropDownList>();
inputDropDownList2 = new List<DropDownList>();
Label lblO = new Label();
Label lblW = new Label();
Label lblA = new Label();
TxtBoxO.ID = "TextBoxO" + i.ToString();
DDLW.ID = "DDLW" + i.ToString();
DDLA.ID = "DDLA" + i.ToString();
inputDropDownList.Add(DDLW);
inputDropDownList2.Add(DDLA);
TxtBoxO.Width = 325;
DDLW.Height = 25;
DDLA.Height = 25;
DDLA.ID = "ddl_Achieved";
DDLA.Items.Add("Select");
DDLA.Items.Add("5");
DDLA.Items.Add("10");
DDLA.Items.Add("15");
DDLA.Items.Add("20");
DDLA.Items.Add("25");
DDLA.Items.Add("30");
DDLA.Items.Add("35");
DDLA.Items.Add("40");
DDLA.Items.Add("45");
DDLA.Items.Add("50");
DDLA.Items.Add("55");
DDLA.Items.Add("60");
DDLA.Items.Add("65");
DDLA.Items.Add("70");
DDLA.Items.Add("75");
DDLA.Items.Add("80");
DDLA.Items.Add("85");
DDLA.Items.Add("90");
DDLA.Items.Add("95");
DDLA.Items.Add("100");
DDLW.ID = "ddl_Weight";
DDLW.Items.Add("Select");
DDLW.Items.Add("5");
DDLW.Items.Add("10");
DDLW.Items.Add("15");
DDLW.Items.Add("20");
DDLW.Items.Add("25");
DDLW.Items.Add("30");
DDLW.Items.Add("35");
DDLW.Items.Add("40");
DDLW.Items.Add("45");
DDLW.Items.Add("50");
DDLW.Items.Add("55");
DDLW.Items.Add("60");
DDLW.Items.Add("65");
DDLW.Items.Add("70");
DDLW.Items.Add("75");
DDLW.Items.Add("80");
DDLW.Items.Add("85");
DDLW.Items.Add("90");
DDLW.Items.Add("95");
DDLW.Items.Add("100");
lblO.ID = "LabelO" + i.ToString();
lblW.Text = "LabelW" + i.ToString();
lblA.ID = "LabelA" + i.ToString();
lblO.Text = "Objective " + " " + (i + 1).ToString() + " : ";
lblW.Text = " Weightage" + " " + (i + 1).ToString() + " : ";
lblA.Text = " Achieved " + " " + (i + 1).ToString() + " : ";
//Add the labels and textboxes to the Panel.
Panel1.Controls.Add(lblO);
Panel1.Controls.Add(TxtBoxO);
Panel1.Controls.Add(lblW);
Panel1.Controls.Add(DDLW);
Panel1.Controls.Add(lblA);
Panel1.Controls.Add(DDLA);
}
The error is pretty clear. You're adding multiple controls with the same ID, which is disallowed. Note that you're in a loop:
for (int i = 0; i < rowCount; i++)
We'll assume that rowCount is greater than 1 and that the loop iterates more than once. Within that loop you create controls:
DropDownList DDLW = new DropDownList();
DropDownList DDLA = new DropDownList();
And you assign IDs to them:
DDLW.ID = "DDLW" + i.ToString();
DDLA.ID = "DDLA" + i.ToString();
These IDs are probably unique. But then you overwrite them with ones that are not unique:
DDLA.ID = "ddl_Achieved";
DDLW.ID = "ddl_Weight";
And then you add them to the page:
Panel1.Controls.Add(DDLW);
Panel1.Controls.Add(DDLA);
The purpose of an ID, as its name suggests, is to uniquely identify an object. Just like with id attributes in HTML, if you re-use the same one multiple times in a page then the behavior becomes undefined. The system (in this case wherever you're using FindControl()) expects there to be 0 or 1 elements with any given ID value. You've created more then 1, which is invalid.
You can probably just remove the lines where you set the non-unique IDs and keep the lines where you set the unique ones.
Ok so I have a datagrid which has a button to export data into an email, it currently with the code below, creates a new mail and it inserts the first row displayed in the datagrid correctly but doesn't insert any other rows.
I assume I need a loop IE my foreach statement (albeit empty) needs something in there but I cant figure it out, been stuck for hours) basically I just want to to loop through all the rows displayed from the stated 2 columns and then get them into the variable and then use that to insert into the subject body of the new mail item...
Microsoft.Office.Interop.Outlook.Application app = new Microsoft.Office.Interop.Outlook.Application();
Microsoft.Office.Interop.Outlook.MailItem mailItem = (MailItem)app.CreateItem(Microsoft.Office.Interop.Outlook.OlItemType.olMailItem);
foreach (DataGridViewRow rows in dataGridView2.SelectedRows)
{
}
var column1 = dataGridView2.Rows[0].Cells["dataGridViewTextBoxColumn2"].Value;
var column2 = dataGridView2.Rows[0].Cells["dataGridViewTextBoxColumn1"].Value;
mailItem.Body = column1.ToString() + " - " + column2.ToString();
mailItem.Subject = this.subjectText.Text;
mailItem.To = this.senderText.Text;
//mailItem.Attachment.Add(logPath);//logPath is a string holding path to the log.txt file
//mailItem.Importance = Microsoft.Office.Tools.Outlook.OlImportance.olImportanceHigh;
mailItem.Display(false);
First, if you want to display every row, you need to replace dataGridView2.SelectedRows by dataGridView2.Rows, then, simply get the value of column1 and column2 at each iteration :
object column1 = "";
object column2 = "";
foreach (DataGridViewRow row in dataGridView2.Rows)
{
if(!item.IsNewRow)
{
column1 = item.Cells["dataGridViewTextBoxColumn1"].Value;
column2 = item.Cells["dataGridViewTextBoxColumn2"].Value;
mailItem.Body += column2 != null ? column2.ToString() : "" + " - " +
column1 != null ? column1.ToString() : "" + "\n";
}
You can do it this way:
StringBuilder builder = new StringBuilder();
foreach (DataGridViewRow rows in dataGridView2.SelectedRows)
{
builder.AppendLine(
((rows.Cells["dataGridViewTextBoxColumn2"].Value == null) ? "" : rows.Cells["dataGridViewTextBoxColumn2"].Value.ToString()) +
" - " +
((rows.Cells["dataGridViewTextBoxColumn1"].Value == null) ? "" : rows.Cells["dataGridViewTextBoxColumn1"].Value.ToString()));
}
mailItem.Body = builder.ToString();
Also, try using column index if possible.
i want to add textboxes dynamically in C# on a button click in a table row. for that i have used the following code.
[1]: http://pastie.org/7702237.
the problem is i am able to adding as many textboxes as I want but they are adding at same location, i mean the table row is not incrementing for every button click instead it is simply replacing the old table row with the new one. Please help me in how to solve this problem.
Thanks in advance
Ganesh
Web-based applications do not maintain state; to this end the state of the table and any variables is not being maintained. With each postback (generated by the button), the table's state reverts to what it was prior to adding a row and then a row is programatically added to it.
In order to achieve your goal, you will need to maintain state somehow. In the following code snippet I am making use of a session:
private List<TableRow> TableRows
{
get
{
if(Session["TableRows"] == null)
Session["TableRows"] = new List<TableRow>();
return (List<TableRow>)Session["TableRows"];
}
}
The following is your code modified to work with the session variable:
TextBox txtE, txtM, txtB;
Button btnAdd, btnDel;
TableRow trow;
TableCell tcell;
foreach(TableRow tr in TableRows)
tblEduDetails.Controls.Add(tr);
int count = TableRows.Count + 1;
txtE = new TextBox();
txtE.ID = "E" + count.ToString();
txtE.Visible = true;
txtE.Text = "E " + count.ToString();
txtE.BorderWidth = 2;
txtE.TextMode = TextBoxMode.SingleLine;
txtE.Height = 30;
txtM = new TextBox();
txtM.ID = "M" + count.ToString();
txtM.Visible = true;
txtM.Text = "M " + count.ToString();
txtM.TextMode = TextBoxMode.SingleLine;
txtM.Height = 30;
txtB = new TextBox();
txtB.ID = "E" + count.ToString();
txtB.Visible = true;
txtB.Text = "B " + count.ToString();
txtB.TextMode = TextBoxMode.SingleLine;
txtB.Height = 30;
btnAdd = new Button();
btnAdd.ID = "A" + count.ToString();
btnDel = new Button();
btnDel.ID = "D" + count.ToString();
trow = new TableRow();
trow.ID = "R" + count.ToString();
trow.BorderWidth = 1;
tcell = new TableCell();
tcell.ID = "E" + count.ToString();
tcell.Controls.Add(txtE);
trow.Controls.Add(tcell);
tcell = new TableCell();
tcell.ID = "B" + count.ToString();
tcell.Controls.Add(txtM);
trow.Controls.Add(tcell);
tcell = new TableCell();
tcell.ID = "M" + count.ToString();
tcell.Controls.Add(txtB);
trow.Controls.Add(tcell);
tblEduDetails.Controls.Add(trow);
TableRows.Add(trow);