I have a stored procedure. One of its input parameters is expecting a char(8). I try to convert a string "AAA" to this particular parameter type, which is a DBType.AnsiStringFixedLength.
object v = Convert.ChangeType("AAA", param.DbType.GetTypeCode());
// param is AnsiStringFixedLength
However, all I get is an exception: Input string was not in a correct format.
And the stack trace says: at System.Number.StringToNumber(String str, NumberStyles options, NumberBuffer& number, NumberFormatInfo info, Boolean parseDecimal) [...]
Why is System.Convert trying to convert a string into a number, even though the prodecure's parameter is expecting a char(8)? How do I solve this? I don't want to use one huge switch case mapping all SQL types to CLR types...
EDIT:
This is the code in question: (A generic method to call any MS SQL stored procedure)
using (SqlConnection conn = new SqlConnection(this.config.ConnectionString))
{
using (SqlCommand cmd = conn.CreateCommand())
{
cmd.CommandType = CommandType.StoredProcedure;
cmd.CommandText = this.config.StoredProcedureName;
conn.Open();
SqlCommandBuilder.DeriveParameters(cmd);
foreach (SqlParameter param in cmd.Parameters)
{
if (param.Direction == ParameterDirection.Input ||
param.Direction == ParameterDirection.InputOutput)
{
try
{
string rawParam = param.ParameterName.Replace("#", "");
if (this.config.Parameters.ContainsKey(rawParam))
{
try
{
param.Value = Convert.ChangeType(this.config.Parameters[rawParam],
param.DbType.GetTypeCode());
}
catch(Exception oops)
{
throw new Exception(string.Format("Could not convert to '{0}'.", param.DbType), oops);
}
}
else
throw new ArgumentException("parameter's not available");
}
catch (Exception e)
{
throw;
}
}
}
cmd.ExecuteNonQuery();
}
}
The actual parameter values are provided by this.config.Parameters - all of them are strings. I iterate through SqlCommand's parameter list and set them accordingly. Converting the string values to the parameter's Sql type is necessary here, and as far as I can see, the Sql type is provided by param.DBType.
You seem to mix up some things here, or I don't get what you try to do. The DbType (an enumeration) inherits Enum and that implements IConvertible -> You can call GetTypeCode(). But - you are now calling Enum.GetTypeCode(), which returns the underlying type. If you didn't specify it (and DbType didn't) any Enum is backed by an int.
What are you trying to solve with the code anyway? Why would you want to change the type of a string if the parameter is a string (although with a fixed length)?
Looking at the question some more it seems even more odd. You have an object v (probably for value?) - what do you care about the type?
object v1 = "Foo";
object v1 = 42;
What is the difference for you? I guess you want to pass the values to something else, but - if you only reference the value as object you might still need to cast it.
Please update your question and explain what you really want to do, what you expect to gain.
Regarding the comment:
I'm using Convert.ChangeType(object
value, TypeCode typeCode), so it's not
really converting into an Enum/int. At
least that's what I thought...
See above: DbType.GetTypeCode() is not what you want. Try it, give me the benefit of the doubt: What do you expect to get from DbType.AnsiStringFixedLength.GetTypeCode()? What is the actual result, if you try it?
Now to your code: You try to set the SqlParameter.Value property to the "correct" type. Two things: According to the documentation you probably want to set the SqlParameter.SqlValue, which is the value using SQL types according to the docs. SqlParameter.Value, on the other hand, is the value using CLR types and allows to infer both DbType and SqlValue. Sidenote, implementation detail: The SqlParameter.SqlValue setter just calls the setter of SqlParameter.Value again...
I would expect that the ADO.NET stuff converts the value on its own, if at all possible. What error are you getting without jumping through this hoops?
Related
I wrote a generic method to retrieve single values from database (MSSQL Server).I encountered into a case that I need to get a Boolean value from DB.
As you can see in the code below, a Object local field (IsExist) gets the result.
When the value in DB is False GenericScalar() method return False (as it should)
and the condition: if (IsExist == null) in GetWanLineDisconnectionData() is true and the return block is executing, even though IsExist is False and not null.
Why is that?
How can I overcome this problem?
private void GetWanLineDisconnectionData()
{
string q = "SELECT WanLineDiscconection FROM AdditionalProjectsData WHERE SpCall= " + "'" + spCall + "'";
object IsExist = Orange.ProjectManagment.DAL.Database.GenericScalar<object>(q);
if (IsExist == null) {
return;
}
if (bool.Parse(IsExist) == true) {
RadWanDiscYes.Checked = true;
} else {
RadWanDiscNo.Checked = true;
}
}
Database method:
public static T GenericScalar<T>(string query)
{
OleDbConnection connection = new OleDbConnection(sqlConnString);
connection.Open();
OleDbCommand cmd = new OleDbCommand(query, connection);
try
{
var result = cmd.ExecuteScalar();
if (result == null)
{
return default(T);
}
else
{
return (T)result;
}
}
catch (Exception ex)
{
throw ex;
}
finally
{
CloseConnection(ref connection);
}
}
EDIT:
maybe a few screen shoots will better demonstrate it:
(note that: GetWanLineDisconnectionData() is written in VB.NET and GenericScalar() is written in C# on a different project in the solution):
in the beginning IsExist is nothing (null).
the query finds one row and the value of the bool column WanLineDiscconection is false and IsExist is set to false as it should be.
here is the problem, the program enters the if block and IsExist is not nothing (null).
The variable foo in object foo = false is definitely not null, so the premise in your title is incorrect.
ExecuteScalar() returns null when there are no rows. In that case, the method GenericScalar() return default(T), which for object will be null.
How to solve this depends on what your data looks like. You probably want to represent the result in a nullable int, or int? instead of object:
var exists = Orange.ProjectManagment.DAL.Database.GenericScalar<int?>(q);
RadWanDiscYes.Checked = exists.GetValueOrDefault() > 0;
See How does GetValueOrDefault work?, What is the default value of the nullable type "int?" (including question mark)?.
Aside from that: you generally also don't want to write handy wrappers around database queries, because you're reinventing the wheel poorly, opening up your application to SQL injection. That being said, there's a lot more going wrong in that method, including but not limited to not disposing your database connection and rethrowing exceptions without their stacktrace.
You´re mixing compile-time and runtime information. Whilst GenericScalar<object>(q) is an information that exists at compile-time the type returned from ExecuteScalar at compile-time is just object. You expect it to be of type boolean, which might or might not be true for your specific case. However this is not relevant to the compiler.
In short that means that T actually is object, not whatever you expect it to be from your database. And as CodeCaster allready mentioned the default-value for object is just null.
Si I´d suggest to use GenericScalar<bool> instead of GenericScalar<object> because you seem to know what your query actually returns - in your case a bool. Then default(T) evaluates to false, not to null.
I have the following code which I got from the internet. I am not sure about the use of IConvertible. MSDN says:
"This interface provides methods to convert the value of an instance of an implementing type to a common language runtime type that has an equivalent value."
Is it meant that, I can assign anything to IConvertible and while using it converts it to the implementing type?
For example, in below example I pass parameters as key-value pair where key is string and value is IConvertible.
SqlParameter object = new SqlParameter(param.Key, param.Value)
SqlParameter object takes key as string and value as depending on the type declared in the table or stored procedure. How does it exactly works here?
private SqlCommand GetCommand(string procedureName,
IEnumerable<KeyValuePair<string, IConvertible>> parameters)
{
foreach (var param in parameters)
{
command.Parameters.Add(new SqlParameter(param.Key, param.Value));
}
}
I have a DataTable. How to set a certain value of it as one of my SqlParameters?
I receive this error message:
The SqlParameterCollection only accepts non-null SqlParameter type objects, not String objects.
Either I do:
myCommand.Parameters.Add("#managedBy");
myCommand.Parameters["#managedBy"].Value = comp.Rows[i][4].ToString();
myCommand.Parameters.Add("#modified_at");
myCommand.Parameters["#modified_at"].Value = DateTime.Now.ToString("MM/dd/yyyy");
myCommand.Parameters.Add("#cn");
myCommand.Parameters["#cn"].Value = comp.Rows[i][1].ToString();
or:
myCommand.Parameters.Add("#managedBy");
myCommand.Parameters["#managedBy"].Value = (SqlParameter)comp.Rows[i][4];
myCommand.Parameters.Add("#modified_at");
myCommand.Parameters["#modified_at"].Value = DateTime.Now.ToString("MM/dd/yyyy");
myCommand.Parameters.Add("#cn");
myCommand.Parameters["#cn"].Value = (SqlParameter)comp.Rows[i][1];
What am I missing? How to set the parameter from a DataTable?
The SqlParameterCollection.Add method overloads with one argument requires that the argument be a SqlParameter. To create an argument with a string name you also need to specify the type (and optionally a size):
myCommand.Parameters.Add("#managedBy", SqlDbType.NVarChar);
To set the value in one call you can use AddWithValue, wihch will infer the SQL type from the value type:
myCommand.Parameters.AddWithValue("#managedBy", comp.Rows[i][4]);
The SqlParametersCollection inside the SqlCommand object has an Add method that wants an SqlParameter object.
So your code should be something like this
myCommand.Parameters.Add(new SqlCommand("#managedBy", SqlDbType.AnsiString)).Value = .......;
There are numerous possibilities to add a new parameter to the collection, the easiest of which is
myCommand.Parameters.AddWithValue("#managedBy", comp.Rows[i][4].ToString());
This particular version however requires particular care as explained in this very detailed article on MSDN
Use the following code:
myCommand.Parameters.AddWithValue("ParameterName", anyObject);
Suppose i have this sql statement and I have executed a sql command to get a datareader:
"select 1 union select 2"
//.....
var rdr = cmd.ExecuteReader();
and now i want to read the value in the first column of the first row:
var myInt = (int)rdr.GetValue(0); //great this works
var myLong = (long)rdr.GetValue(0); //throws cast exception, even though you can cast int to long
So it appears the type you cast to in C# needs to match exactly the SQL type. I.E. If the sql type is bigint, you can only cast to long. If the sql type is int, you can only cast to int. No mix and match...
I just want to get something that works regardless of the type of integer c# asks for and sql returns, as long as you could theoretically cast one to the other. So if SQL Server gives me a floating point type, and I'm asking for an int, I want the truncated int you get from doing that cast.
My goal is to make this work with generics, so I can have this function work when the generic parameter doesn't exactly match the datatype in sql server:
List<T> GetFirstColumn<T>(string sql) where T : struct
{
//get connection, execute reader
// loop:
// lst.Add( (T) rdr.GetValue(0));
}
I'd like this to work for both statments:
var sql1 = "Select 1"; //sql int
var sql2 = "Select cast(1 as bigint)"; //sql equivalent of a long
var lst1 = GetFirstColumn<int>(sql1);
var lst2 = GetFirstColumn<int>(sql2);
Does anyone have a relatively painless way of doing this?
Like Fredrik says, the value from SqlDataReader is boxed. You can convert a boxed value to an int with Convert.ToInt32, like:
int i = Convert.ToInt32(read[0]);
This will try to convert even if SQL Server returns a bigint or a decimal.
System.Convert will take care of the conversion.
T GetValue<T>(SqlDataReader rdr)
{
var dbVal = rdr.GetValue(0);
var csVal = (T)System.Convert.ChangeType(dbVal, typeof(T));
}
Caveat: if T == Nullable<S>, you need to do some extra work with reflection to get the underlying type and call ChangeType with typeof(S) as the type parameter. Apparently, MS didn't update the ChangeType function with .NET 2.0 and the introduction of nullables. And if it's a nullable, and dbVal is DBNull, you can just return null.
object dbValue = 5;
//this throws
Convert.ChangeType(dbValue, typeof(int?));
//this works
if(dbValue == DBNull.Value || dbValue == null)
{
if(typeof(int?).IsNullable) //or is a class, like string
{return null;}
dbValue = null;
}
var type = GetUnderlyingType<int?>(); //== typeof(int)
Convert.ChangeType(dbValue, type);
I think your problem is that GetValue returns an object. This means that in the case of an int, you will get an int boxed in an object. Then you cannot directly cast it to a long but must first unpack it as an int:
var myLong = (long)(int)rdr.GetValue(0);
This will be quite tricky using generics, I would say. Well, you could make generic methods with two type arguments; one specifying what type the field is, and one specifying the type you want. But I don't really see the need; SqlDataReader already has methods for the various data types, such as GetInt32, GetInt64 and so on, so the generic method would not really give any added value in that case.
i've got the following in C#:
string typename = "System.Int32";
string value = "4";
theses two strings should be taken to generate an object of the specified type with the specified value...
result should be:
object o = CreateUnknownType(typename, value);
...
Int32 test = (Int32)o;
Is this what are you are thinking?
object result = Convert.ChangeType("4", Type.GetType("System.Int32"));
As stated, this is too broad and can not be solved generally.
Here are some options:
Type type = Type.GetType(typename);
object o = Activator.CreateInstance(type);
This will create an instance of the type that typename is describing. It calls the parameterless constructor of that type. (Downside: Not all objects have a parameterless constructor. Further, this does set the state of the object using value.)
Type type = Type.GetType(typename);
object o = Activator.CreateInstance(type, new[] { value });
This will create an instance of the type that typename is describing. It calls a constructor of that type that accepts one parameter of type string. (Downside: Not all objects have such a constructor. For example, Int32 does not have such a constructor so you will experience a runtime exception.)
Type type = Type.GetType(typename);
object o = Convert.ChangeType(value, type);
This will attempt to convert the string value to an instance of the required type. This can lead to InvalidCastExceptions though. For example, Convert.ChangeType("4", typeof(FileStream)) will obviously fail, as it should.
In fact, this last example (create an instance of type FileStream with its initial state determined by the string "4") shows how absurd the general problem is. There are some constructions/conversions that just can not be done.
You might want to rethink the problem you are trying to solve to avoid this morass.
Creating an instance of a type you know by name (and which should have a default constructor):
string typeName = "System.Int32";
Type type = Type.GetType(type);
object o = Activator.CreateInstance(type);
Parsing a value from a string will obviously only work for a limited set of types. You could
use Convert.ChangeType as suggested
by PhilipW
or maybe create a
Dictionary<Type,Func<string,object>>
which maps known types to known parse
functions
or use reflection to invoke the
Parse(string) method on the type,
assuming there is one:
string valueText = "4";
MethodInfo parseMethod = type.GetMethod("Parse");
object value = parseMethod.Invoke(null, new object[] { valueText });
or maybe you can use the
infrastructure provided by the .NET
component model. You can fetch the
type converter of a component and use
it like this:
TypeConverter converter = TypeDescriptor.GetConverter(type);
object value = converter.ConvertFromString(valueText);
Your logic seems a little flawed here. Obviously, if you're directly casting the object at a later time to the actual type it is, then you must know the type to begin with.
If there's something else that is missing from this question please elaborate and maybe there is a more appropriate answer than simply, "This doesn't make much sense."
Perhaps you have a set of different types, all of which implement a known interface?
For example if you have several different user controls and want to load one of them into a container, each one might implement IMyWobblyControl (a known interface) yet you might not know until runtime which of them to load, possibly from reading strings from some form of configuration file.
In that case, you'll need to use reflection to load the actual type from something like the full assembly name, then cast it into your known type to use it.
Of course, you need to make sure that your code handles invalid cast, assembly not found and any of the other exceptions that are likely to come along through something as wobbly as this...
This seems like a job for Int32.Parse(string). But to agree with the others it seems this is "unique" and one should probably think gloves.
Here's a specific example of the problem involving Azure SQL Federations...which splits data into separate db's according to a key range.
The key range types are:
SQL / .Net SQL type / CLR .Net
INT / SqlInt32 / Int32, Nullable
BIGINT / SqlInt64 / Int64, Nullable
UNIQUEIDENTIFIER / SqlGuid /Guid, Nullable
VARBINARY(n), max n 900 / SqlBytes, SqlBinary /Byte[]
Ideally, a C# function param could take either .Net SQL type or CLR .Net type but settling on just one category of type is fine.
Would an "object" type param be the way to go? And, is there a feasible way to identify the type and convert it accordingly?
The concept is something like:
public void fn(object obj, string fedName, string distName, bool filteringOn)
{
...figure out what type obj is to ensure it is one of the acceptable types...
string key = obj.toString();
return string.Format("USE FEDERATION {0} ({1}='{2}') WITH RESET, FILTERING = {3}", fedName, distName, key, (filteringOn ? "ON" : "OFF"));
}
Though the param value is cast to string, it will be recast/checked on the sql server side so validating it on the app side is desired.
After using:
Type type = Type.GetType(typename);
Try this extension method:
public static class ReflectionExtensions
{
public static T CreateInstance<T>(this Type source, params object[] objects)
where T : class
{
var cons = source.GetConstructor(objects.Select(x => x.GetType()).ToArray());
return cons == null ? null : (T)cons.Invoke(objects);
}
}
Hope this helps.