I want to select one item in drop down list in ASP.NET written with VB.NET - I have values and texts in listbox like this:
Volvo
Audi
etc...
But values coming from other place in upper case... VOLVO, AUDI..
This code:
dropdownlist.FindByValue("CAPITAL")
Is not working and giving null for Volvo.. please help.
One way would be LINQ:
Dim volvoItem = dropdownlist.Items.Cast(Of ListItem)().
FirstOrDefault(Function(i) i.Text.Equals("Volvo", StringComparison.InvariantCultureIgnoreCase))
C#:
var volvoItem = dropdownlist.Items.Cast<ListItem>()
.FirstOrDefault(i => i.Text.Equals("Volvo", StringComparison.InvariantCultureIgnoreCase));
This worked for me
foreach(ListItem li in dropdownlist.Items)
{
if (String.Compare(li.Text, myLabel.Text, true) == 0)
myCustomValidator.IsValid = false; // Match Found !
}
Like Tim said LINQ would be you answer.
in C# try the following:
var item = dropdownlist.Items.Cast<ListItem>().SingleOrDefault(li =>
li.Text.ToUpper() == "VOLVO");
Additional variants, using framework CI comparison. VB:
uiList.Items.Cast(Of ListItem)
.FirstOrDefault(Function(i) i.Text.Equals(comparand, StringComparison.InvariantCultureIgnoreCase))
C#:
uiList.Items.Cast<ListItem>()
.FirstOrDefault(i => i.Text.Equals(comparand, StringComparison.InvariantCultureIgnoreCase));
You could also use CurrentCultureIgnoreCase depending on your requirements. These are generally safer than comparing with ToUpper/ToLower, because some cultures have unexpected casing rules.
Related
Hi im relatively new to c# code and i was wondering if there is any way to get the amount of columns and rows in a grid and store that amount in a variable
Something like:
var columnamount = grid.columnamount;
But i could not find anything that works
Thanks
You can use the following code to get a count of the columns and rows directly via the ColumnDefinitions and RowDefinitions properties. No need to enumerate the children of the grid because you may not have views in every column/row.
var columnCount = grid.ColumnDefintions.Count;
var rowCount = grid.RowDefinitions.Count;
For reference the documentation.
You might be able to do it this way, purely based on what I see in the docs:
var countColumns = grid.Children.Where( c => c.Column).Max();
var countRows = grid.Children.Where( c => c.Row).Max();
But I'm not sure if you can access Row anf Column properties on the child element.
This is not the best way to check, I guess, but it's working (same thing for columns):
EDIT: nope, for columns it doesn't work
int GetRowsCount(Grid grid)
{
var item = grid.Children.FirstOrDefault();
return item != null ? Grid.GetRow(item) + 1 : 0;
}
Firstly apologies if this question has been answered elsewhere (I've searched!) but am stuck using an xPathNodeIterator in Umbraco for the first time.
Basically I've created a xPath Checkboxlist and I'm trying to get the values of what's been selected. What I've got (and this is the first time that I've used this, so most of this I've copied parrot fashion)
XPathNodeIterator n = umbraco.library.GetXmlNodeById(myNode.Id.ToString());
while (n.MoveNext())
{
litSpeakers.Text += n.Current.InnerXml;
}
This returns me everything that's in the node content but I'm trying to just get the values of the XPath Checkboxlist (called xpather).
This is first time that I've used this and I'm clueless!! So if anyone could help that would be magic.
Thanks,
Craig
I've not tested this but it should give you a pointer, checkboxes can use different 'truthy' values but I've assumed your checked ones have a value of "checked" rather than something else:
List<string> checkedNodes = new List<string>();
XPathNodeIterator n = umbraco.library.GetXmlNodeById(myNode.Id.ToString());
while (n.MoveNext())
{
if (n.Current.GetAttribute("id","") == "xpather" && n.Current.GetAttribute("checked","") == "checked")
{
checkedNodes.Add("<something to identify whats been checked>");
}
}
umbraco example: http://our.umbraco.org/wiki/reference/umbracolibrary/getprevalues
Hi thanks for your response.
The way that I 'solved' it was (where 'speakers' is the name of the xpath checkboxlist)
string dataValue = myNode.GetProperty("speakers").Value.ToString();
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.LoadXml(dataValue);
foreach (XmlNode itemnode in xmlDoc.DocumentElement)
{
litNode.Text += itemnode.InnerText;
Node myUmbracoNode = new Node(Int32.Parse(itemnode.InnerText));
litNode.Text += myUmbracoNode.Name + "<br/>";
}
I have the list of ProjectId like 14,15,18 and i want to search those items from my datatable using linq.
now i have created the query like
IEnumerable<DataRow> drProjList = from a in dtProj.AsEnumerable()
where a.Field<int>("ProjectId")
.ToString()
.Contains(projFilter)
select a;
but i am not getting the 0 elements.
can any body suggest me any other way like IN operator in linq.
thanks in advance
EDIT
projFilter is the pure string which have the 14,15,18,... project ids.
String's "contains" isn't really an option here. You should convert projFilter into a proper list of integers first.
var projFilter2 = new HashSet<int>(projFilter.Split(',').Select(i => int.Parse(i)));
and only then check if it contains the desired number.
IEnumerable<DataRow> drProjList =
from a in dtProj.AsEnumerable()
where projFilter2.Contains(a.Field<int>("ProjectId"))
select a;
I don't know much about the AsEnumerable(), but I would have done it the other way round, something like this:
List<int> intProjFilter = new List<int>();
foreach(string filter in projFilter.Split(','))
intProjFilter.Add(int.Parse(filter));
from a in dtProj
where projFilter.Contains(a.ProjectID)
select a;
Let me know if this helps
You need to parse the project filter into collection of integers first.
var projectIds = projFilter.Split(',').Select(x => Convert.ToInt32(x)).ToList();
var drProjList =
from a in dtProj.AsEnumerable()
where projectIds.Contains(a.ProjectId)
select a;
Maybe something like this:
IEnumerable<DataRow> drProjList =
(
from a in dtProj.AsEnumerable()
where projFilter.Contains(a.Field<int>("ProjectId").ToString())
select a;
)
Or you can simply do this as well:
IEnumerable<DataRow> drProjList=dtProj.AsEnumerable()
.Where(a=>projFilter
.Contains(a.Field<int>("ProjectId")
.ToString()));
I don't know c# well so i am trying to give answer in vb.net hope that will help you.
Dim dtProj As DataTable
dtProj = FillProjectList()
Dim projIdFilter() As Integer = {14, 15, 16}
Dim drRow = From p In dtProj.AsEnumerable _
Where projIdFilter.Contains(p.Field(Of Integer)("ProjectId")) _
Select p
Thank you...
hi guys i am stuck converting below sql to LINQ query.
all i want is to have maximum number from list of (FA-00001 ,FA-00059)
SELECT MAX(CAST(SUBSTRING(ReferenceId, PATINDEX('%-%', ReferenceId) + 1, LEN(ReferenceId) - PATINDEX('%-%', ReferenceId)) AS int)) AS MaxReferenceId FROM [ClientRC].[dbo].[EHO_Action]
is this possible to convert to LINQ? thanks
An alternative approach using anonymous projection:
var y = (from record in
(from record in db.ClientRC
select new
{
Group = "x",
ReferenceNumber = Convert.ToInt32(record.ReferenceId.Split('-')[1])
})
group record by new { record.Group } into g
select new
{
MaxReferenceId = g.Max(p => p.ReferenceNumber)
});
http://msdn.microsoft.com/en-us/library/bb386972.aspx
var myvar = (from v in db.object where v!=null select v.id).Max();
MSDN has lots of examples for stuff like this.
Or, you can execute queries directly against a datacontext if you're using entity framework. Just make sure if you're doing anything with parameters you're parameterizing the query and not taking user input directly into it.
http://msdn.microsoft.com/en-us/library/ee358769.aspx
Try this..
var list = DBContext.EHO_Action
.Where(x => x.YourListColumn != null)
.Select(x => x.YourListColumn).ToList(); // Take the list (FA-00001 ,FA-00059) from db to a list
var maxNo = list.Max(x => Convert.ToInt32(x.Split('-')[1]));
Please change the context and column names according to your Linq context.
If you want to use sql you can do it this way..
var list = DBContext.ExecuteQuery<string>("select yourreqrdcolumn from [ClientRC].[dbo].[EHO_Action]");
Are anyone aware of any method to achieve indentation in the ASPXGridView (we are running the 10.x version currently available)
What we got
What we'd like to achieve
Some information about the code-behind.
The grid is populated by an ObjectDataSource and the indentation is stored in a property alongside with the other data. In example the BMI row will have 0 indentation while the GENDER will have 1, and MAN will have 2. Etc...
The indentation is calculated runtime since relations might change.
public void GetItemsRecursive(int? parentId, int level)
{
List<qstFeedbackLine> q;
if (parentId == 0)
q = _db.qstFeedbackLines.Where(x => x.ParentId == null).ToList();
else
q = _db.qstFeedbackLines.Where(x => x.ParentId == parentId).ToList();
foreach (var item in q)
{
// Store the indent
item.Indent = level;
// Add item to List
_items.Add(item);
level++;
// ...and get the children of the current id
GetItemsRecursive(item.FeedBackLineId, level);
}
}
Any advice out there?
Thanks!
Ended up using the ASPxTreeList component instead!
http://demos.devexpress.com/ASPxTreeListDemos/