Dexter
Dexter

Reputation: 69

Cannot implicitly convert type 'string[]' to 'string'

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;
  }

Upvotes: 0

Views: 1651

Answers (2)

Morten Bork
Morten Bork

Reputation: 1632

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.

Upvotes: 0

Victor Hugo Terceros
Victor Hugo Terceros

Reputation: 3169

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.

Upvotes: 2

Related Questions