ds = (DataSet)Session["Details"];
DataTable dt = ds.Tables[0];
DataTable temp = dt.Clone();
dt.Rows.Add(ds.Tables[0].Select("ID =" + ID));
Error message:Unable to cast object of type 'System.Data.DataRow' to type 'System.IConvertible'.Couldn't store <System.Data.DataRow> in Date Column. Expected type is DateTime.
can anybody help me please.
ImportRow is designed for this kind of scenarios only, so check it out. I have used it manytimes for such requirements.
I think it's a fault in the overloads. If you were working in VB, I would tell you to use the row:=ds.Tables[0].Select("ID =" + ID) notation in the parameters, but I don't think C# has this.
I think the Add function assumes you're feeding it the array of values for the new row, instead of a datarow itself.
Related
In the cmdb_rel_ci table, I want to retrieve the value and total count of all the unique parent.sys_class_name values for Type(cmdb_rel_type) "Depends on::Used by".
I was trying to use with GlideAggregate, but classname is showing empty.
Can anyone offer some advice?
Use GlideAggregate on the cmdb_ci table, not cmdb_rel_ci. The rel table has no sys_class_name if I recall.
You could also try dot-walking to it using GlideRecord, or using a Join query.
I don't know what "for type Depends on::Used by" means. Pretty sure it doesn't mean anything.
Please take the time to explain your questions clearly and include your code next time if you're expecting anyone to take time out of their day to help you.
As suggested already, use Glideaggregate (more info here)
Try this as an example:
var ga = new GlideAggregate('cmdb_rel_ci');
ga.addQuery('type.name', 'STARTSWITH', 'Depends on::Used by');
ga.addAggregate('COUNT', 'parent');
ga.query();
while (ga.next()) {
var parent = ga.parent.getDisplayValue();
var parentCount = ga.getAggregate('COUNT', 'parent');
gs.info('CI ' + parent + ' has ' + parentCount + ' relationships with type Depends on::Used by');
}
Currently I'm working on legacy application, that uses crystal report engine. I have to get value of database fields programmatically. As I've assumed, I need proper event for getting next code to work:
Report.Database.Tables(1).Fields(1).Value
But the value is always empty in DownloadStarted/Finished event handlers. What I'm doing wrong and is it at least possible?
I think that if you want to get value of your table fields in program the best way is that you get the field name from report and then connect to your table directly and use report field names as the table columns name
i do it in c# i hope it can help you in vb6 too:
string name = report2.Database.Tables[1].Fields[1].Name;
string[] names = name.Split('.');
and then add your database to your program and use names like this:
DataTable dt = new DataTable();
string[] value = dt.Columns[names[1]];
if you just need your tables values, you can use my last answer, but if you need value of database fields in crystal report, i mean something like formula field ,this code can help you:
CRAXDRT.FormulaFieldDefinitions definitions = report2.FormulaFields;
string formulaText = "IF " + report2.Database.Tables[1].Fields[3].Name
+ " > 10 THEN" + report2.Database.Tables[1].Fields[2].Name;
definitions.Add("Test", formulaText);
report2.Sections[1].AddFieldObject(definitions[1], 0, 0);
LINQ newbie here
I am trying to get a value of a field - using a fieldName variable.
If I do a watch on row[FieldName] I do get a value - but when I do it on the actual code it will not compile.
string fieldName = "awx_name"
List<awx_property> propertyQry =
(
from property in crm.awx_propertyawx_properties
where property.awx_propertyid == new Guid(id)
select property
).ToList();
foreach (awx_property row in propertyQry)
{
//THIS DOES NOT WORK
fieldValue = row[fieldName];
}
Thanks in advance. Alternatives would be welcome as well
You keep us guessing what you are trying to do here... You need to specify the types of the objects, so it's easy for us to understand and help. Anyway, I think you are trying to get an object based on the ID. Since you are getting by Id, my guess would be the return value is a single object.
var propertyObj =( from property in crm.awx_propertyawx_properties
where property.awx_propertyid == new Guid(id)
select property
).SingleOrDefault();
if(propertyObj != null) {
fieldValue = propertyObj.GetType().GetProperty(fieldName).GetValue(propertyObj, null);
}
Of course, you need to add validation to make sure you don't get null or any other error while accessing the property value.
Hope it helps.
What type is fieldValue? What does awx_property look like? This will only work is awx_property is a key/value collection. It its not, you could use reflection instead.
If it is a key/value collection you are probably missing a cast. (row[FieldName].ToString() or something) Also you are missing a semi-colon in the foreach block.
I am using Northwind Customers Table where I fill the dataset and get the datatable.
I am trying to use dynamic linq and want to select columnName dynamically
var qry = MyDataTable.AsEnumerable().AsQueryable().Select("new(Country)");
Right now I have hard coded country but even then I get this error
No property or field 'Country' exists in type 'datarow'
I would like to eventually change this query to take the column name dynamically.
Please help!!! thanks.
The important hint is here (in bold):
No property or field 'Country' exists
in type 'datarow'
The extension method AsEnumerable of the DataTable class returns an IEnumerable<T> where T has the type DataRow. Now the Select method of Dynamic LINQ wants to work with this type DataRow which hasn't a property Country of course.
You could try this instead:
var qry = MyDataTable.AsEnumerable().AsQueryable()
.Select("new(it[\"Country\"] as CountryAlias)");
it now represents a variable of type DataRow and you can use methods of this type and perhaps also the indexer in my example above. (Dynamic LINQ supports accessing array elements by an integer index, but I am not sure though if accessing an indexer with a string key will work.)
I've used Slauma's answer and it worked. In addition i was doing OrderBy with dynamic linq maybe this will help to someone. I'll just drop the code here.
string dynamicLinqText = $"it[\"{sortColumnName}\"] {sortDirection}"; //it["PERSON_NAME"] asc
result = result.AsEnumerable().OrderBy(dynamicLinqText).CopyToDataTable();
I'm trying to accomplish the following query (notice .StartsWith):
return (from p in _session.Linq<Profile>()
where (p.Firstname + " " + p.Lastname).StartsWith(wildcard)
select p).ToList();
This throws: could not resolve property: Firstname.Lastname.
If I do this:
return (from p in _session.Linq<Profile>()
where p.Firstname.StartsWith(wildcard)
select p).ToList();
Everything is working. How can this be?
Thanks in advance!
The Where Expression does not know how to handle the concatenation of strings. It is trying to make sense of the properties, not the values.
Also, for future reference the StartsWith with the concat and the other one with out would in practice return the same thing.
Is this what you want?
return (from p in _session.Linq<Profile>()
where p.Firstname.StartsWith(wildcard) || p.Lastname.StartsWith(wildcard)
select p).ToList();
Update: rewritten answer based on new insights and edited questions.
What's in wildcard and what's the expected output vs. input? If you concat "Abel" + " " + "Braaksma" it will return true for wildcard.StartsWith("Abel") or wildcard.StartsWith("Abel Br") but not wildcard.StartsWith("Braaks"). Do you perhaps mean Contains instead? But this won't solve your error:
The exception you receive seems to come from NHibernate, not from your code. Is it possible that Lastname does not have a correct mapping to the database table? Can you show the stacktrace? Can you access the properties outside of the context of the LINQ statement, but filled with data from the table?