i'm trying to programmatically add a where clause to the LinqDatasource of my gridview, but i keep getting an exception saying: "Operator '=' incompatible with operand types 'Guid' and 'Int32'"
if (e.CommandName == "getuser")
{
int index = Convert.ToInt32(e.CommandArgument);
GridViewRow selectedRow = GridView1.Rows[index];
Guid id = new Guid(selectedRow.Cells[1].Text);
LinqDataSource2.Where = "UserID = " + id;
GridView1.DataSourceID = LinqDataSource2.ID;
GridView1.DataBind();
}
anyone know how, when and why my Guid is converted to an int ?
Fixed
added a开发者_如何学Go line
LinqDataSource2.WhereParameters.Add("UserID", System.Data.DbType.Guid, id.ToString());
i'm guessing int32 is a standard type when nothing is set in whereparameters, but only the where. Just guessing though, if anyone knows more please inform me.
As always with SQL - You're better using the facilities in your data access layer to pass your parameters as their actual type (with appropriate data layer -> database conversions, as required), rather than using a string.
It looks like LinqDataSource has a WhereParameters collection, so you'd create your where something like:
LinqDataSource2.Where = "UserID = @UserID";
And then add an appropriate parameter to the WhereParameters collection. (Note, I've not written such code, just pulled in a few quick details, but it looks right)
If you'll be comparing your id as a string to your GUID, then you'd need to cast the GUID field to a string. The easiest way to achieve this is to call the ToString() method on your GUILD field.
Try this:
LinqDataSource2.Where = "UserID = '" + id.ToString("B") + "'";
This will pass the GUID in such form: {00000000-0000-0000-0000-000000000000}
Hopefully, that's what the database is expecting.
精彩评论