Ben Luik
Ben Luik

Reputation: 27

How can i solve the error of 'var' in ASP.Net C#?

protected void Button1_Click(object sender, EventArgs e)
{
        var query2 =  from cm in DC.custMasts where cm.custCity == TextBox1.Text.Trim()_ select cm.custName, custCity, custCompany;     
        GridView1.DataSource  = query2 ;
}

I'm getting error

CS0819: Implicitly-typed local variables cannot have multiple declarators

How can i solve this error?

EDIT :: Thank you guys, it worked and error disappeared.

Upvotes: 1

Views: 186

Answers (3)

Ghost Answer
Ghost Answer

Reputation: 1498

try this

protected void Button1_Click(object sender, EventArgs e) 
{
   var query2 =  from cm in DC.custMasts 
                 where cm.custCity == TextBox1.Text.Trim()
                 select new 
                 {
                     Name=cm.custName, 
                     City=cm.cmcustCity, 
                     Company=cm.custCompany
                 }.ToList();

   GridView1.DataSource  = query2 ;   
   GridView1.DataBind();     
}

Upvotes: 3

KV Prajapati
KV Prajapati

Reputation: 94653

Try this,

var query2 =  from cm in DC.custMasts where cm.custCity == TextBox1.Text.Trim()
     select new {
               Name=cm.custName, 
               City=cm.cmcustCity, 
               Company=cm.custCompany
               }.ToList();

  GridView1.DataSource  = query2 ;
  GridView1.DataBind();

Upvotes: 0

Didier Ghys
Didier Ghys

Reputation: 30676

You have to create a new object in the select part of the linq query:

var query2 =  from cm in DC.custMasts where cm.custCity == TextBox1.Text.Trim()_
              select new {
                  Name = cm.custName,
                  City = custCity,
                  Company = custCompany
              }; 

Upvotes: 2

Related Questions