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 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:
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)