0

I have the following code in my index view

public ActionResult Index(string searchBy, string search)
 {
   Session["FullName"] = search;

   var dbsenderPostal = (from a in db.TblCUSTOMER_PROFILE
                                where a.FullName == search
                                select a.PostalAddress).ToArray();

   Session["PostalAddress"] = dbsenderPostal;

   if (searchBy == "XXX")
        {
            return View(db.TblCUSTOMER_PROFILE.Where(senders => 
            senders.GeneratedCode == search || search == null).ToList());
        }
        else
        {

            return View(db.TblCUSTOMER_PROFILE.Where(senders => 
            senders.CustomerFullName.StartsWith(search) || search == 
            null).ToList());

        }
 }

In my [HttpPost] MorePost view, I am passing data to this 'MorePost' post controller using Sessions with the value pairs. If i add the .Split() method to 'ViewBag.messages2', another error occurs as " 'System.Array' does not contain a definition for split ". This is from further research online to solve this proble. Please am i doing anything wrong with my LINQ query to ViewBag which is passing data to the object name 'sentprint'. I am able to retrieve the data from the LINQ query as 'P.O.Box AX 33 Miami'. I have also tried using 'TempData'. like this TempData["PostalAddress"]. I did try other approaches which gives other errors as "Cannot implicitly convert type 'System.Data.Entity.Infrastructure.DbQuery' to 'string' ". Sorry I am new to LINQ query. Thanks in Advance.

 [HttpPost]
 public ActionResult MorePost(string Itemn)
  {
    TblSENDERSINFORMATION sentprint = new TblSENDERSINFORMATION();

        ViewBag.messages = Session["FullName"];

        sentprint.NameOfSender = ViewBag.messages;

        ViewBag.messages2 = Session["PostalAddress"];

        sentprint.PostalAddress = ViewBag.messages2;  //Error found here

        sentprint.Item = Itemn;
  }

2 Answers 2

2

Your problem is in the linq query itself, let me explain why

In your query you are getting an array of strings due this

.ToArray()

even if it is only one item, it is an array of strings containing one item

  var dbsenderPostal = (from a in db.TblCUSTOMER_PROFILE
                                where a.FullName == search
                                select a.PostalAddress).ToArray();

So your solution is pretty simple, just change the ToArray() by FirstOrDefault(), this way you will get only one string item always (like a top 1 but slightly different)

So your query should look like this

  var dbsenderPostal = (from a in db.TblCUSTOMER_PROFILE
                                where a.FullName == search
                                select a.PostalAddress).FirstOrDefault();

Now your session variable will be a string.

In this case you have to understand better linq and also generics.

Sign up to request clarification or add additional context in comments.

2 Comments

This answer is correct, I would just add that FirstOrDefault() Method name is confusing cause if the query return no result you will get a null not a Default value like an empty string. So you might need to handle it if the postal address can't be null somewhere in your code.
@Dexter good to know it worked for you, please mark the answer as correct
0

this an example of why you shouldn't use var, until you know what you are doing.

Use strong type, unless you are able to read the code, in soft type.

var dbsenderPostal = (from a in db.TblCUSTOMER_PROFILE
                                where a.FullName == search
                                select a.PostalAddress).ToArray();

literally means:

string[] dbsenderPostal = (from a in db.TblCUSTOMER_PROFILE
                                where a.FullName == search
                                select a.PostalAddress).ToArray();

Therefore:

ViewBag.messages2 = Session["PostalAddress"];
sentprint.PostalAddress = ViewBag.messages2

this makes no sense, you are literally trying to put a string[] into a string.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.