views:

86

answers:

1

Greetings all,

I am working on redesigning my personal Web site using VS 2008 and have chosen to use LINQ to create by data-access layer. Part of my site will be a little app to help manage my budget better. My first LINQ query does successfully execute and display in a GridView, but when I try to use a RowDataBound event to work with the results and refine them a bit, I get the error:

The type or namespace name 'var' could not be found (are you missing a using directive or an assembly reference?)

This interesting part is, if I just try to put in a var s = "s"; anywhere else in the same file, I get the same error too. If I go to other files in the web project, var s = "s"; compiles fine.

Here is the LINQ Query call:

public static IQueryable pubGetRecentTransactions(int param_accountid)
{
    clsDataContext db;

    db = new clsDataContext();

    var query = from d in db.tblMoneyTransactions
                join p in db.tblMoneyTransactions on d.iParentTransID equals p.iTransID into dp
                from p in dp.DefaultIfEmpty()
                where d.iAccountID == param_accountid
                orderby d.dtTransDate descending, d.iTransID ascending
                select new
                {
                    d.iTransID,
                    d.dtTransDate,
                    sTransDesc = p != null ? p.sTransDesc : d.sTransDesc,
                    d.sTransMemo,
                    d.mTransAmt,
                    d.iCheckNum,
                    d.iParentTransID,
                    d.iReconciled,
                    d.bIsTransfer
                };

    return query;
}

protected void Page_Load(object sender, EventArgs e)
{
    if (!this.IsPostBack)
    {
        this.prvLoadData();
    }
}

internal void prvLoadData()
{
    prvCtlGridTransactions.DataSource = clsMoneyTransactions.pubGetRecentTransactions(2);

    prvCtlGridTransactions.DataBind();
}


protected void prvCtlGridTransactions_RowDataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        var datarow = e.Row.DataItem;
        var s = "s";

        e.Row.Cells[0].Text = datarow.dtTransDate.ToShortDateString();
        e.Row.Cells[1].Text = datarow.sTransDesc;
        e.Row.Cells[2].Text = datarow.mTransAmt.ToString("c");
        e.Row.Cells[3].Text = datarow.iReconciled.ToString();
    }//end if
}//end RowDataBound

My googling to date hasn't found a good answer, so I turn it over to this trusted community. I appreciate your time in assisting me.

A: 

Seems rather odd to me, var is a language keyword but somehow the compiler treats it as a type in this case. Since you have (successfully) used var in pubGetRecentTransactions() I assume you are compiling against .NET 3.5, right?

VS does very odd things sometimes. Did you try restarting VS and do a full rebuild afterwards`?

Johannes Rudolph
Yes, I checked the Web project, and it's target is set to 3.5. Just tried restarting and doing the full rebuild - same error.
Michael
New info: I removed the "var" line that was giving me errors, and the var s = "s"; line that previously appeared fine then started throwing compiler errors too.So I commented out all the vars so I could compile, and added a <% var s = "s"; %> to one of the aspx pages, and got the same error, but found particularly interesting:C:\Program Files\Microsoft Visual Studio 9.0\Common7\IDE> "C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\csc.exe" /t:library /utf8output etc...etc...Now the question is why do both VS and ASP.Net seem to be using the wrong compiler version?
Michael
well, the good news is I got the Web site project compiling in 3.5. Ended up removing all the references, commenting out 3.5 code, changing target to 2.0, building, changing back to 3.5, uncommenting code, adding references, and then it built fine with the "var". Go figure...
Michael
Just came across a page that talks about adding this web.config code to tell the web site to build using the c# v3 compiler. Must've been added during my fiddling. <system.codedom> <compilers> <compiler language="c#;cs;csharp" extension=".cs" type="Microsoft.CSharp.CSharp3CodeProvider, CSharp3CodeDomProvider"/> </compilers> </system.codedom>
Michael
glad I could point you in the right direction at least. You should make all of this in an answer and accept it.
Johannes Rudolph