adiyaman
adiyaman

Reputation: 17

error occurred during retrieve image from database use Linq query

following code i used to retrieve Image from database through linq query but i getting error like

LINQ to Entities does not recognize the method 'System.String ToString(System.String)' method, and this method cannot be translated into a store expression.

Error line coded with //error occurred.

public void ProcessRequest (HttpContext context) {

        HttpRequest req = context.Request;


        MembershipUser user_id1 = Membership.GetUser();
        string user_id = user_id1.ProviderUserKey.ToString();      

        // string categoryID =Convert.ToInt32("4");// req.QueryString["CategoryID"].ToString();
        // Get information about the specified category          
        TMSEntities db = new TMSEntities();
        //get User ID From Login


        var category = (from data in db.Register1_db
                       where data.User_ID == Convert.ToString(user_id)
                       select data.Student_Photo);

        //error occurred.
        int len = category.First().Length;

        // Output the binary data          
        // But first we need to strip out the OLE header          
        int OleHeaderLength = 78;
        int strippedImageLength = len - OleHeaderLength;
        byte[] imagdata = new byte[strippedImageLength];          
        Array.Copy(category.First().ToArray(), OleHeaderLength, imagdata, 0, strippedImageLength);          
        if ((imagdata) != null)          
        {              
            MemoryStream m = new MemoryStream(imagdata);              
            System.Drawing.Image image = System.Drawing.Image.FromStream(m);              
            image.Save(context.Response.OutputStream, ImageFormat.Jpeg);          
        }




}




public bool IsReusable {
    get {
        return false;
    }
}

Upvotes: 0

Views: 158

Answers (2)

Achim
Achim

Reputation: 15702

This one should work:

var string_id = Convert.ToString(user_id);
var category = (from data in db.Register1_db
                       where data.User_ID == string_id
                       select data.Student_Photo);

LINQ creates expressions trees, so Convert.ToString would not get called, but would have to be interpreted by the LINQ provider.

Upvotes: 0

Joachim Isaksson
Joachim Isaksson

Reputation: 180917

Looking at the line giving the error;

var category = (from data in db.Register1_db
                where data.User_ID == Convert.ToString(user_id)
                select data.Student_Photo);

Convert.ToString(user_id) is not an expression that can be used in Linq to Entities.

When looking at your code, I can see that user_id already is a string and doesn't need the conversion, so this should work better;

var category = (from data in db.Register1_db
                where data.User_ID == user_id
                select data.Student_Photo);

Upvotes: 1

Related Questions