Adding Linq-to-Sql class breaks simple ASP.NET web application - asp.net

Using VS2012, latest SP/update applied.
I have been very frustrated trying to get an ObjectDataSource to work. See http://bit.ly/XTpdvN and http://bit.ly/XTpsHi.
I started a new Web Application project, compiling and running after each step, trying to make the steps as granular as possible.
1) Create new empty web application.
2) Add WebForm1.aspx.
3) Clean, rebuild, run (either View in Browser or in debugger).
4) Add new class to App_Code.
5) Repeat #3.
5) Add existing .mdf to App_Data.
6) Repeat #3.
7) Add Linq-to-Sql class to App_Code (Items.dbml). Leave it empty.
8) Clean, Rebuild, run in Debugger:
Compiler Error Message: CS0234: The type or namespace name 'Linq' does
not exist in the namespace 'System.Data' (are you missing an assembly
reference?)
Source Error:
Line 12: namespace ODS_Restart.App_Code Line 13: { Line 14: using
System.Data.Linq; Line 15: using System.Data.Linq.Mapping; Line 16:
using System.Data;
As far as I can tell, there is nothing in the code in App_Code that needs Linq:
namespace ODS_Restart.App_Code
{
public class BAL
{
public static List<string> GetCountries()
{
return new List<string>() { "USA", "Aus", "NZ"};
}
}
}
As I said, very frustrating. Any insight on how to get past this problem would be greatly apprectiated....

use one wizard for linq to sql program:
step 1. Take new project as windows form application .
step 2. Drag one button and DataGridView from toolbox on it.
step 2. Go to solution explorer. add database(.mdf) file to windows form application project which we've taken.
step 3. Same way by right clicking on windows form application project which we've taken add linq to sql class(.dbml) file.
step 4. Go to Server explorer add table in database taken in step 3. Add data to that table.
step 5. open that .dbml file from solution explorer and drag table created from server explorer on it.
step 6. Open form created in step 2. Double click on that button and add following code:
DataClasses1DataContext obj1 = new DataClasses1DataContext();
List<pp> obj2 = obj.pps.ToList();
dataGridView1.DataSource = obj1;
DataClasses1 is .dbml file added in step 3. pp is table name added in step 4. obj1 and obj2 are objects created. pps represents tablename(pp).

Related

.Net 6.0 console application keeps displaying popup message while I am trying to code

I am typing in an example from the book, "C# 10 and .Net 6".
It is a console application with reflection.
Here is the code so far:
using System.Reflection;
Assembly? assembly = Assembly.GetEntryAssembly();
if (assembly == null) return;
// Loop through the assemlies that this app references
foreach (AssemblyName name in assembly.GetReferencedAssemblies())
{
// Load the assembly so we can read its details
Assembly a = Assembly.Load(name);
// declare a variable to count the number of methods
int methodCount = 0;
// loop through all the types in the assembly
foreach (TypeInfo t in a.DefinedTypes)
{
// add up the counts of methods
methodCount += t.GetMethods().Count();
}
//
}
I got this far and was about to type in the last part and I got the popup dialogue in the pic:
It says:
Specified argument was out of the range of valid values.
Parameter name: length
Why does this obtrusive popup keep displaying and stopping me from typing? What is it? What does it mean? Why does it not wait for you to try and build, compile or run before reporting the problem.
What is this and how do you make it go away?
I just want to add I was able to type it all in and the example runs correctly. But why does VS 2022 keep displaying this popup while I am trying to type?
This looks like a problem with Visual Studio or an extension. It's not a problem with your code.
You could try:
Restarting VS,
Restarting Windows,
Repairing/updating/reinstalling VS.

What must I do to let an aspx.vb file see a .vb file in the same project?

I'm getting a compile error "Type 'commonClass' is not defined" in a file directly beneath the project (\\alf.aspx > alf.aspx.vb) on this line of code:
Dim pDAO As New commonClass
There is such a class, in \\App_Code\commonClass.vb:
Public Class commonClass
What is needed for "alf" to see "commonClass"?
This is probably a very basic (no pun intended) question, but I am a newbie to VB.
UPDATE
Does this have anything to do with the code in the legacy project "seeing" the other code, and the new one not: in the legacy project, the Visual Studio menu items are:
FILE
EDIT
VIEW
**WEBSITE**
BUILD
DEBUG
. . .
...whereas in the new project, they are:
FILE
EDIT
VIEW
**PROJECT**
BUILD
DEBUG
. . .
IOW, the new project (which I created using File > New Project > Templates > VB > New Project) has a "PROJECTS" menu item, whereas the legacy project has a "WEBSITE" menu item.
Will re-doing it as File > New Website > ... make the difference? Is there a way to change it to a website without going through that?
UPDATE 2
Both the legacy code, which compiles, and the new project, which doesn't, contain:
// alf.aspx.vb
Partial Class alf
Inherits Page
-and:
// App_Code\commonClass.vb
Public Class commonClass
I see no "Namespace" in either file...
UPDATE 3
So, wondering if the fact that I created a new project rather than a new website is the crux of the problem, I tried creating a new website, but it tells me, "An item with the same key has already been added" and then the WebSite project - which I have not touched beyond creating it - starts off with 21 Errors and 13 Warnings
You have two files. One for your asp.net file (alf.aspx.vb) and one for your commonClass (I assume commonClass.vb). Do both of these files have a Namespace defined and are they both the same Namespace (alf)? If they don't match then you will get the error you are describing.
Namespace alf
Public Class commonClass
Public Shared Sub Execute()
Console.WriteLine("Blah")
End Sub
End Class
End Namespace
I solved this wide-ranging and confounding issue with a three-pronged approach, which is detailed here.

Entity framework update model from database not generating cs class for newly added table in tt class

I am using entity framework 5 with visual studio 2012.
I do have an existing model. Now I want to add a new table to that existing model. For that I have opened the edxm file and using right click I updated the model successfully.
Now, in "Model Browser" under "EntityTypes" for model, I can see the table name exists. But in Solution Explorer it is not showing the auto-generated .cs file for the table I have added newly under .tt file.
I tried "run custom tool" but it has not generated the class. Also have restarted the Visual Studio but result is the same.
Can anyone help me?
Thanks
Problems
In case you have table name tblEmployee in Database But after the adding 'Entity Data Model' you have Changed Entity tblEmployee to Employee in EDMX file then Save and Build. But Classes for Changed Name are not Generated Automatically in Model1.tt file.
When "Update Model From Database" for adding new table same Problem occurs.
Also not available in MVC When creating view with Model Class/Create Controller With Actions using EntityFramework
This problem is for early version of VS2012.This problem is solved in upgraded version of VS2012.
Solution
we have solution for this with early version of VS2012 & EF 5.0
Follow steps
Right click On Model1.tt and select 'Run Custom Tool' save and Build Now see classes are generated.
Right click On Model1.Context.tt and select 'Run Custom Tool' save and Build Now see property IN Context class is generated like
public DbSet<Employee> Employees { get; set; }
Save and Build Solution
Model1Context context=new Model1Context();
List<Employee> empList= context.Employees.ToList();
This worked for me.
But Keep in mind that still EF 6.0 not able to make Scaffolding when 'creating controller and view using entityframework' in MVC with this viersion of VS2012.
You must Use EF 5.0 or update VS2012 with new update.
Resolved it myself.
The problem was in files filename.Context.tt and filename.tt.
The diagram file name for variable const string inputFile specified in both of the files were different than the existing diagram file (.edmx file). Updated it with existing diagram file name and then updated model from database. Working fine now.
In this case what i do is i just delete the exciting model and then click on add and just add your newly added table for there!
If this is the bug with the edmx file located in a folder it is now fixed - download and install VS 2012 Update 1. You can get it from:
http://www.microsoft.com/visualstudio/eng/downloads#d-visual-studio-2012-update
I have added primary key to the new table. That resolves problem.

ASP.NET: Errors in autogenerated .Designer.cs file for Model

I'm new to ASP.NET and having a problem with a tutorial.
I've created a new MVC4 Project in VS2012 Express for Web.
And I've added a SQL Database with 1 Table "Persons" and filled it with some random testdata:
Id int (primary key, is identity=true)
name varchar(50)
birthdate date
adam 01.01.2001
berta 02.02.2002
As a Model I've used ADO.NET Entity Data Model, named it "PersonsModel.edmx"
and used the Personsdatabase for it.
To see the PersonsModel.Designer.cs file, I activated "Codegeneration Status" to "Standard". Refreshed and clicked on the PersonsModel.Designer.cs file.
But in this file I've errors... So I wanted to use something like this in my controller:
HomeController.cs:
PersonsEntities1 db = new PersonsEntities();
db.person...
but it doesn't work, and I think(?) it's because of the errors in the .Designer.cs file.
PersonsModel.Designer.cs: e.g.:
public PersonsEntities1() : base("name=PersonsEntities1", "PersonsEntities1")
{
this.ContextOptions.LazyLoadingEnabled = true;
OnContextCreated();
}
errors in the base: ... line
and in ContextOptions.
Unfortunately I've no english VS, but it says something like:
The best accordance für the overloaded System.Data.Entity.DbContext(string, System.Data.Entity.Infrastructure.DbCompiledModel)-Method has some invalid arguments
And no definition found for "ContextOptions", there is no method "ContextOptions" which accepts "MvcApplication7.Models.PersonsEntities1" as a first argument.
I'm a bit confused, because I did it like in the tutorial explained.
I think this code is in error:
base("name=PersonsEntities1", "PersonsEntities1")
There is no constructor that takes two strings. Your second argument is supposed to be of type DbCompiledModel. (See here.)
Now, I don't know why your designer would produce code that can't compile, so I'm wondering whether you have the wrong version of Entity Framework installed.
Apparently the problem was VS2012. It all works with VS2010.
I've downloaded the installer again from asp.net/mvc and added Visual Web Developer 2010 Express. After installation of VS2010 and all dependencies I tried the tutorial again and all works fine now. No wrong code in the .Designer.cs file :).
Thanks a lot Ann L. for your support. Your hint (wrong version of Entity Framework installed) prompt me to try another VS version.
In addition I've to say: I also tried VS2012 Ultimate (complete DVD version) but it didn't help.

Can't set up asp.net mvc 2 RC and spark view engine

Does omebody has ideas how to fix "Method not found: 'Void System.Web.Mvc.ViewContext..ctor(System.Web.Mvc.ControllerContext, System.Web.Mvc.IView, System.Web.Mvc.ViewDataDictionary, System.Web.Mvc.TempDataDictionary)'." exception. This solution doesn't work http://dotnetslackers.com/articles/aspnet/installing-the-spark-view-engine-into-asp-net-mvc-2-preview-2.aspx.
Thans for all.
I had to download the spark view engine source code (http://sparkviewengine.codeplex.com/Release/ProjectReleases.aspx?ReleaseId=27600). Once I did that I went through each of the projects that had a reference to the 1.0 version of System.Web.Mvc assembly and updated to reference to point to System.Web.Mvc 2.0. From there you can build the solution (in visual studio) and you will find that a whole bunch of tests start to fail. You can attempt to fix them (by adding the additional TextWriter parameter you will find is now needed). You will also see that the SparkView.cs file complains about a missing parameter. In the Render method (line 100 of the source code I downloaded) I had to update the instantiation of the wrappedViewContext to look like this (add writer to the end of the list of parameters):
public void Render(ViewContext viewContext, TextWriter writer)
{
var wrappedHttpContext = new HttpContextWrapper(viewContext.HttpContext, this);
var wrappedViewContext = new ViewContext(
new ControllerContext(wrappedHttpContext, viewContext.RouteData, viewContext.Controller),
viewContext.View,
viewContext.ViewData,
viewContext.TempData,
writer); // <-- add the writer to the end of the list of parameters
...
}
Once the code is updated you can run the build.cmd script that is in the root of the source you downloaded. The build process will create a zip file in the build/dist folder. Take those new dll's and add them to your website. Things should work once again.
At the time of this answer, MVC 2 RC2 bits are available at sparkviewengine.codeplex.com
http://sparkviewengine.codeplex.com/releases/view/41143
It was actually Erik from the post mentioned by R0MANARMY who helped get those bits out there.
Looks like you can also download compiled binaries from here. As the post says, it isn't a final (or official) release, but at least it seems like the unit tests pass.

Resources