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)
Related
I would like to create a helper class to do some common task.
For example, I am retrieving some results from Database and then assigning the values to variables. But some of the fields in my records might contain null as well. I would like to check before assigning that the value does not contain any null.
Also there are some variable those are type int, so like to check before parsing to the specific type.
int iValue=int.parse(Helpers.IsNull(dr[colName].toString()));
string strValue=Helpers.IsNull(dr[colName].toString());
How should I create a helper class and what value should I return with IsNull method?
Little confuse..
Thanks
Well what you are trying to acchieve is to avoid the NullReferenceException i guess.
You could achieve this by writing a generic method like this
public static TValue GetValueSafe<TValue,TObject>(TObject obj, Func<TObject,TValue> accessor)
{
if(obj== null)
return default(TValue);
return accessor(obj);
}
Then use it like this:
string strValue = Helpers.GetValueSafe(dr[colName], o => o.toString());
This would either return the value of toString, or if dr[colName] == null returns default(string) which is null.
You could exand this by adding a defaultParameter to define a value on "failure".
However i would'nt recommend using this.
A more radical approach (which would remove the issue) would be to eradicate NULLs from your values altogether.
The simplest way would be through ISNULL() when you query your database:
Where now you do
SELECT MyColumn FROM MyTable
You instead go
SELECT ISNULL(MyColumn, '') AS MyColumn FROM MyTable
Then you can assume no NULLs will get through to your code.
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"];
How can I cast the result of an ExecuteScalar command to a GUID structure without first using .ToString() to pass to the constructor of the GUID?
The reason for doing this is performance and not creating thousands of unnecessary string objects in memory.
It is possible using a reader and the GetGUID Method but I can not see any references to how to achieve the same when using a scalar value.
Update: I also need to handle DBNull Values
Assuming that your sql statement cannot return DBNull.Value, then yes you can:
Guid myResult = (Guid) cmd.ExecuteScalar();
EDIT: Now that we know you need to handle nulls.... :-)
I can think of two ways to handle nulls - use a nullable Guid and set it to null, or use a regular Guid and have it set to Guid.Empty if your SQL statement returns null.
Consider some form of helper function or extension method which checks for DBNull.Value.
static Guid? GetGuidFromDb(object dbValue)
{
if (dbValue == null || DBNull.Value.Equals(dbValue))
{
return null;
}
else
{
return (Guid) dbValue;
}
}
or
static Guid GetGuidFromDb(object dbValue)
{
if (dbValue == null || DBNull.Value.Equals(dbValue))
{
return Guid.Empty;
}
else
{
return (Guid) dbValue;
}
Then call
Guid? myResult = GetGuidFromDb(cmd.ExecuteScalar());
Note - this will choke if your SQL command returns a datatype other than UniqueIdentifier.
If the object being returned from the command is a UniqueIdenitifier, then yes.
Guid myResult = cmd.ExecuteScalar() as Guid? ?? Guid.Empty;
I'm getting this error message:
"Can't decide which property to consider the Key - you can create one called 'ID' or mark one with SubSonicPrimaryKey attribute"
The code in question is generated by the context.tt:
public SqlQuery Delete<T>(Expression<Func<T,bool>> column) where T:new()
{
LambdaExpression lamda = column;
SqlQuery result = new Delete<T>(this.Provider);
result = result.From<T>();
result.Constraints=lamda.ParseConstraints().ToList();
return result;
}
In my DB the respective table actually has a primary key called ID. And I tried to insert the SubSonicPrimaryKey Attribute:
uint _ID;
[SubSonicPrimaryKey]
public uint ID
{
get { return _ID; }
set
{...
How can I fix this?
You should post the stack trace of your exception.
This is just a wild guess but I suppose subsonic finds two possible primary keys, the one called ID and one with the SubSonicPrimaryKey attribute and does not check wether they are equal and since the count of the possible keys is not equal to 1 the exception is thrown.
You should try to remove the SubSonicPrimaryKey attribute of your class, since the Property is already called ID.
My guess is that your issue is related to the uint value type. SubSonic has is problems handling unsigned value types.
Try using a int property instead for your primary key!
In this code (from the WCF REST starterkit - preview2):
protected override SampleItem OnAddItem(SampleItem initialValue, out string id)
{
// TODO: Change the sample implementation here
id = Guid.NewGuid().ToString();
this.items.Add(id, initialValue);
return initialValue;
}
Am I getting back id as String, or the initialValue as SampleItem?
Edit:
Looks like I get both back, so what would a simple example of the method call look like assigned to a couple of variables?
You will get back id in the string that you pass as a parameter to the method. Also, the method will return the SampleItem instance.
SampleItem myItem = new SampleItem();
string newId = string.Empty;
myItem = OnAddItem(myItem, out newId);
// now myItem will be assigned with SampleItem returned from the
// OnAddItem method, and newId will be updated with the id assigned
// within that method
You're getting both.
You are getting back both.
You will pass in a string variable for ID and that will be returned to you via the 'out' modifier. The function will also return the SampleItem instance initialValue that you passed in.
You are getting back both. An out parameter is just an additional way to to return a value offered by some programming languages.