Populate ParameterCollection w/ sp_columns (or INFORMATION_SCHEMA.Columns) Results - asp.net

I'd like to build a ParameterCollection object based on the results of either execute sp_columns MyTableName or SELECT * FROM INFORMATION_SCHEMA.Columns WHERE TABLE_NAME = 'MyTableName'.
The problem I'm having is assigning the appropriate data type to each Parameter ... I'm unsure of how to gather that information from either of the above two queries and convert it into a System.Data.DbType or System.TypeCode.
Any help is greatly appreciated.
Links: MSDN: sp_columns, MSDN: Information Schema.Columns
Edit: I guess what I'm looking for is functionality similar to Type.GetType("TypeName") that would accept a SQL data type. For example, DbType.GetType("int") or DbType.GetType("varchar").
Edit: Links from Answer: MSDN: Enum.Parse Method

It's actually really easy to do :-) SqlDbType is an enum, and there's a nice static function on the Enum class which accomplishes just what your looking for:
private SqlDbType ConvertType(string typeName)
{
return (SqlDbType)Enum.Parse(typeof(SqlDbType), typeName, true);
}
With this, you should have no trouble converting the output from your INFORMATION_SCHEMA query into a collection of SqlParameters.

Related

Fluent binding and conversions

When following the Tutorial for TipCalc's iOS UI, I noticed that the binding method described is obsolete(?) and decided to start using Fluent bindings like described here.
Everything gone fine except for one thing: the iOS slider on the tutorial uses a float value between 0 and 1, and the view model uses a int between 0 and 100. So, obviously, I need a conversion here.
Since it's a two-way-binding, how can bind it to be converted for ViewModel -> View and View -> ViewModel? (ideally with fluent binding)
Also, I'd like to know how can I register a conversion under a "name" to later reuse it. Like it seems to be done on this line.
I tried to search on MvvmCross repos for these named conversions but I didn't find anything like a list of the available conversions, there is such a thing?
Thanks a lot for any help!!
I noticed that the binding method described is obsolete(?)
The message attached to that method is:
[Obsolete("Please use SourceDescribed or FullyDescribed instead")]
So use SourceDescribed if you only want to describe the source, or FullyDescribed if you want to describe the source and target.
set.Bind(label).For(l => l.Text).SourceDescribed("'Hello ' + SourceText");
or:
set.Bind(label).FullyDescribed("Text 'Hello ' + SourceText");
Since it's a two-way-binding, how can bind it to be converted for ViewModel -> View and View -> ViewModel?
Two way converters implement both Convert and ConvertBack.
For example - see:
public class TwoWayConverter : MvxValueConverter<double, string>
{
protected override string Convert(double value, Type targetType, object parameter, CultureInfo culture)
{
return (value*value).ToString();
}
protected override double ConvertBack(string value, Type targetType, object parameter, CultureInfo culture)
{
double doubleValue;
double.TryParse(value, out doubleValue);
return Math.Sqrt(doubleValue);
}
}
from https://github.com/MvvmCross/MvvmCross-Tutorials/blob/master/ValueConversion/ValueConversion.Core/Converters/Converters.cs
I'd like to know how can I register a conversion under a "name" to later reuse it
The names are registered by reflection and convention - see a full description in https://github.com/MvvmCross/MvvmCross/wiki/Value-Converters#referencing-value-converters-in-touch-and-droid
a list of the available conversions
MvvmCross doesn't provide many by default - these are mainly app things.
The only built-in converters that I know of are for:
color
visible
language
command parameter
All of these are discussed in https://github.com/MvvmCross/MvvmCross/wiki/Value-Converters#the-mvx-visibility-valueconverters

LINQ and web service cannot return anonymous types, and you cannot construct an object in a query?

Web services cannot return an anonymous type.
If you are building a LINQ query using classes through a datacontext... you cannot construct instances of those classes in a query.
Why would I want to do this? Say I want to join three "tables" or sets of objects. I have three items with a foreign key to each other. And say the lowest, most detailed of these was represented by a class that had fields from the other two to represent the data from those. In my LINQ query I would want to return a list of the lowest, most detailed class. This is one way I have decided to "join some tables together" and return data from each of them via LINQ to SQL via a WebService. This may be bad practice. I certainly do not like adding the additional properties to the lowest level class.
Consider something like this... (please ignore the naming conventions, they are driven by internal consideration) also for some reason I need to instantiate an anonymous type for the join... I don't know why that is... if I do not do it this way I get an error...
from su in _dataContext.GetTable<StateUpdate>()
join sfs in _dataContext.GetTable<SystemFacetState>()
on new { su.lngSystemFacetState } equals new { lngSystemFacetState = sfs.lngSystemFacetState }
join sf in _dataContext.GetTable<SystemFacet>()
on new { sfs.lngSystemFacet } equals new { lngSystemFacet = sf.lngSystemFacet }
join s in _dataContext.GetTable<System>()
on new { sf.lngSystem } equals new {lngSystem = s.lngSystem}
select new
{
lngStateUpdate = su.lngStateUpdate,
strSystemFacet = sf.strSystemFacet,
strSystemFacetState = sfs.strSystemFacetState,
dtmStateUpdate = su.dtmStateUpdate,
dtmEndTime = su.dtmEndTime,
lngDuration = su.lngDuration,
strSystem = s.strSystem
}
).ToList();
Notice I have to build the anonymous type which is composed of pieces of each type. Then I have to do something like this... (convert it to a known type for transport via the web service)
result = new List<StateUpdate>(from a in qr select(new StateUpdate
{
lngStateUpdate = a.lngStateUpdate,
strSystemFacet = a.strSystemFacet,
strSystemFacetState = a.strSystemFacetState,
dtmStateUpdate = a.dtmStateUpdate,
dtmEndTime = a.dtmEndTime,
lngDuration = a.lngDuration,
strSystem = a.strSystem
}));
It is just awful. And perhaps I have created an awful mess. If I am way way off track here please guide me to the light. I feel I am missing something fundamental here when I am adding all these "unmapped" properties to the StateUpdate class.
I hope someone can see what I am doing here so I can get a better way to do it.
You can create a 'dto' class which just contains the properties you need to return and populate it instead of the anonymous object:
public class Result
{
public string lngStateUpdate
{
get;
set;
}
... // other properties
}
then use it like this:
from su in _dataContext.GetTable<StateUpdate>()
...
select new Result
{
lngStateUpdate = su.lngStateUpdate,
... // other properties
}
Nitpick note - please ditch the Hungarian notation and camel casing for properties :)
I think the answer is to create another object to serve as a DTO. This object would not be mapped to the data context and can contain fields that cross the mapped objects. This solves the problems of repetitive properties in the mapped objects, and allows for instantiation of the DTO class in the query as it is not mapped.
FYI: with respect to the problem with the join- I revisited that and I think I may have had the inner and outer components of the join switched around before.

Magic Casting to collection of results

In the Simple.Data examples, there is an example of 'Magic Casting':
// When you assign the dynamic record to a static type, any matching properties are auto-mapped.
var db = Database.Open();
Customer customer = db.Customers.FindByCustomerId(1);
Does Simple.Data also magically cast if there are multiple records returned? Something like this:
var db = Database.Open();
IEnumerable<Customer> customers = db.Customers.FindBySurname("Smith");
Obviously I have tried the above and it doesn't work ("Cannot implicitly convert type" from SimpleQuery to my concrete type). Any advice would be welcome.
FindBySurname returns a single record. If you use FindAllBySurname you'll get an enumerable, which should magic cast OK. (If for some reason it doesn't, you can call .Cast() on it.)

Can I cast a string object passed on command line argument to the actual object?

Is it possible to cast a command-line passed string object back to actual object?
I want to do the following, but throwing error can't cast.
Button objPro = (Button) sender;
cProduct cp = (cProduct) objPro.CommandArgument;
If no, then why?
This is what the string holds.
cProduct cpObj = (cProduct)e.Row.DataItem;
Button btnAddProduct = (Button)e.Row.FindControl("btnAddProduct");
if (btnAddProduct != null)
{
btnAddProduct.CommandArgument = cpObj.ToString();
}
You probably can't, because it's a string. It's not a cProduct (whatever that is - consider following .NET naming conventions and naming it Product instead).
Now you could do this if you had a explicit conversion operator in cProduct to create an instance from a string.
You haven't really explained what's in the string, or what's in the type - but if your cProduct type provides a ToString method which contains all the data in a reversible form, then you could easily write a method or a constructor to create the product again:
Product product = new Product(objPro.CommandArgument);
or maybe:
Product product = Product.Parse(objPro.CommandArgument);
You'll have to write that constructor/method, of course.
I would strongly recommend using a constructor or method instead of an operator, just to keep your code clearer - it's very rarely a good idea to write your own conversion operators.
Take a look at CommandArgument on MSDN. The property is a string, when you assign the a value to the property, you aren't casting some complex type to string, you are setting a string value on the property. Can you cast a string back to your object type anyway, regardless of it being a CommandArgument. I doubt it. If the argument is an int you could try int.Parse or similar for other types which have a parse method.

Programmatically get a Type's Alias in .NET

Is it possible to determine an object type's alias(es) through reflection? Given the following example where myObject is of type System.Int32 -- e.g
Type t = myObject.GetType();
t.Name will be Int32. However, I would like to identify the possibile alias(es), if any, of the objects in question, so that I could resolve the type name of int in addition to Int32
I'm not looking to spend a huge amount of time on this. If there isn't an extreamely simple, built in solution, I'll just map the values myself. =)
Not directly with reflection, there is not. The "type name" (as you are calling it) "int" is not a type name at all, but a language keyword.
You could, of course, use a dictionary to store the look ups from type names to the shorter convenience forms. But there is nothing in the reflection API's that will do that for you.
You can get language specific type alias by using CodeDom classes
var cs = new CSharpCodeProvider();
var vb = new VBCodeProvider();
var type = typeof (int);
Console.WriteLine("Type Name: {0}", type.Name); // Int32
Console.WriteLine("C# Type Name: {0}", cs.GetTypeOutput(new CodeTypeReference(type))); // int
Console.WriteLine("VB Type Name: {0}", vb.GetTypeOutput(new CodeTypeReference(type))); // Integer
The aliases appear to be compile-time only. Notice how they don't apply outside of a given source file. They are simply conveniences, nothing more.

Resources