SqlDataReader.GetGuid with column name c#? - c#

Is there a way to get a column by name and retain the SQL type information returned by SqlDataReader?
I only see .GetGuid(int column)?

There is no separate method to get a GUID (or any of the other types) by column name directly.
What you need to do is this:
Guid someguid = dr.GetGuid(yourDataReader.GetOrdinal("your-col-name"));

You could always just cast the result of the SqlDataReader's indexer:
Guid myGuid = (Guid)myDataReader["myColumnName"];

Related

How to return data from a column of a model .Net

I would like to get the data of a column "type" of a Model object (Nature)
public async Task<IEnumerable<Nature>> FindAll()
{
using DBModelContext context = CreateNewContext();
IEnumerable<Nature> nature = await context.Nature.Where(_ => _.Type);
return nature;
}
"Where" is not offered to me
Thank you in advance for your help
My understanding of your question is that you would like to return all values held in the Type column of your Nature table.
As type information isn't available in the question, I'm assuming that Nature.Type is a string property.
Queryable.Where() takes a predicate argument that will filter the results of your table query. It doesn't look like that's what you are intending. Instead, try using Queryable.Select():
IEnumerable<string> nature = await context.Nature.Select(nature => nature.Type);
You will then need to alter the return type of your method from IEnumerable<Nature> to IEnumerable<string> as you are only returning the value of the Type column, not the whole Nature object.

Linq string field range based selection using string type filed

i want do range based filtration on linq query but property type is string even though it stored numerical data is there way to do this
VOUCHERNO property type is string but now new request came to do range base selection from to it stores numbers
example VOUCHERNO TYPE IS string
from ........ WHERE(O=>O.VOUCHERNO >=100 && O=>O.VOUCHERNO <=100)
This is not full linq query but I cant do this type of filteration
How to handle this
assuming VOUCHERNO always contains int values, why not just cast it to int? (or better yet, change it's definition to int)
from ........ WHERE(O=>(int)O.VOUCHERNO >=100 && (int)O.VOUCHERNO <=100)

Calling the property names of a dynamic property "ints": can't call property

I am assigning property names of a dynamic object as ints in string form. The int value represents an int ID in a database I am using. However I am stuck on how to retrieve the value assigned to the property as shown below:
dynamic test = new ExpandoObject()
IDictionary<string, object> proxyFiler = test as IDictionary<string, object>;
proxyFiler["four"] = 4;
proxyFiler["5"] = 5;
int r = test.four; // Works
int s = test.5; // Doesn't work
A method which reads the database will return an "int" and I would like to be able to access the property value with that property name.
To expand on this: what if I wanted to do a linq query to sort out a list of dynamic objects according to a property name? In this case I need to get the propertyName which I have retrieved as a string e.g. "15":
return disorderedList.OrderBy(o => o.propertyName).ToList();
Does anyone know a simple solution to this problem or do you recommend a different approach? Thanks.
In order for dynamic to work in this way, the key has to follow the rules for valid identifier names in C# (the rules are specified in this outdated MSDN page, but also in the C# language specification). A single number (5) is not an allowed identifier name, which is why that doesn't work.
Note that you can still retrieve the value by using the type as a dictionary, in a similar manner to how you populated it.
As for your second example - you are never using value, so it has no effect. It's the same as just writing int r = test.four;
Edit:
I believe, given your approach, you'd need to cast to a dictionary:
return disorderedList
.OrderBy(o => ((IDictionary<string, object>)o)[propertyName]).ToList();

Convert webcontrols.ListItem to an object of a class

when I try to cast a list item as an object, I get the following. "Cannot convert type 'System.Web.UI.WebControls.ListItem' to 'ASPGigManager2.GigOpportunity'"
Is there anyway you can do this? Here is my code:
GigOpportunity gigOpportunity;
gigList.removeGig((GigOpportunity)lstGigs.SelectedItem);
I have tried to go the long way round and convert it to string but I still get a conversion error, string to GigOpportunity.
GigOpportunity gigOpportunity;
string test;
test = Convert.ToString(lstGigs.SelectedItem);
gigOpportunity = test;
Its as it says, you cannot convert a ListItem to GigOpportunity. And since this is ASP.NET, your original object no longer exists inside the list control. So, during your initial binding, set the DataValueField property to a unique value that identifies each gig (such as a primary key).
Then, on a callback, you have to find your original gig again. For example:
var selectedValue = lstGigs.SelectedValue;
var gig = gigList.Where(x => x.SomeKeyValue == selectedValue).Single();
gigList.Remove(gig);
Better yet, turn you gigList into a dictionary who's key is the same key you used as the value. Then, all you have to do is
gigDict.Remove(lstGigs.SelectedValue);

Checking new guid (unique identifiers) values in SQL Server

I'm always using LINQ so I forgot how to check value in stored procedure whether it equals to 'new guid()' in C# or not
Is this correct
where (#SearchCategory = '000000-0000-0000-0000' )
or what???
'000000-0000-0000-0000' is not a 'new guid'. It is the value that is returned by Guid.Empty
Why don't you just use parametrized queries, and pass Guid.Empty to the parameter as a value ?
in .NET Guid is a class and it has static method to create a new GUID
Guid.NewGuid(); // This will return new guid
Guid.Empty; // This will return the 000000.... type value which is a empty guid value
Guid.NewGuid() holds a different value each time you call it.
When checking if the value holds a 'empty' Guid (uniqueidentifier) use '00000000-0000-0000-0000-000000000000'
(thats 8-4-4-4-12 zero's)

Categories

Resources