Datacontext in LINQ. "Does not exist in the current context" - c#

I keep on receiving this error message. I followed the instructions but it seems it is still not working? what is the solution for this? here's my code
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.Linq;
using System.Data.Linq.Mapping;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
MyDatabaseDataContext = new MyDatabaseDataContext(#"C:\Users\John\Documents\visual studio 2010\Projects\WindowsFormsApplication1\WindowsFormsApplication1\MyDatabase.mdf");
}
}
}
Here's the stacktrace
Error 1 'WindowsFormsApplication1.Form1' does not contain a definition for 'Form1_Load' and no extension method 'Form1_Load' accepting a first argument of type 'WindowsFormsApplication1.Form1' could be found (are you missing a using directive or an assembly reference?) C:\Users\John\Documents\Visual Studio 2010\Projects\WindowsFormsApplication1\WindowsFormsApplication1\Form1.Designer.cs 92 55 WindowsFormsApplication1

This happens if you add an event handler in the designer, then delete the handler method.
Since you didn't delete the auto-generated code that adds the method to the event, you get a compiler error.
Go to the .Designer.cs file and delete the line with the error.

Related

New Form is not accessing in my project

I am a new programmer of C#.net I've been working on a project already have 6 to 7 forms in it on which I am working, last night when I add new form and try to access directly to this form in program.cs file then neither there is frmForm1 suggestion is coming and if I write it manually then the error arise
Error 2 , The type or namespace name 'frmForm2' could not be found
(are you missing a using directive or an assembly
reference?) C:\Users\Ahsan\Desktop\SurveyBuilder\SurveyBuilder\Program.cs 29 37 SurveyBuilder
Whenever there is a form named Form2.cs is in my project, tried to attach snap of it but due to low reputation(its my first question on this site) I am not able to attach.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace SurveyBuilder
{
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
private void Form2_Load(object sender, EventArgs e)
{
}
}
}

Couldn't find modelDataContext

Im trying to use LINQ in my ASP.NET/C# website. For that I need to access the class 'modelDataContext' which is a part of the LINQ namespace.
Im using Visual Studio 2010 Professional
This is my C# code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Data.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class admin_new_feed : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
modelDataContext data = new modelDataContext();
}
}
Full error message I get
Error 1 The type or namespace name 'modelDataContext' could not be
found (are you missing a using directive or an assembly
reference?)
Things i've tried, which doesn't solve problem
Adding the reference (Right-click solution, add reference - System.Data.Linq)
Re-create the website
Right clicking on the line, to see if the tab "Resolve" was there. But it wasn't.

Missing Using directive for hierarchical_grouping in MSDN crystal reports tutorial

I was working through the MSDN tutorial for Crystal Reports, and I get to the Binding the Embedded Report to the CrystalReportViewer Control step:
When I try the build, it tells me that I am missing my using declaration for Hierarchical_Grouping class. This brings up two questions for me...
What is the namespace for this class definition?
Is there an easy way of determining the namespace of a given class?
In another answer I saw the 'ALT, SHIFT, F10' and the 'CTRL, [period]' suggestions for intellisense, but they don't work in my Visual Studio.
I'm sure I have done something ridiculously stupid....sorry in advance....
Here's the code for the form:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using CrystalDecisions.CrystalReports.Engine;
using CrystalDecisions.Shared;
namespace CrystalTest3
{
public partial class Form1 : Form
{
private Hierarchical_Grouping hierarchicalGroupingReport;
public Form1()
{
InitializeComponent();
}
private void ConfigureCrystalReports()
{
hierarchicalGroupingReport = new Hierarchical_Grouping();
crystalReportViewer.ReportSource = hierarchicalGroupingReport;
}
private void Form1_Load(object sender, EventArgs e)
{
ConfigureCrystalReports();
}
}
}
I am assuming that you are referring to this tutorial.
The Hierarchical_Grouping seems to refer to the report file - which will contain the class named Hierarchical_Grouping. Have you added the report to your project?
The report should be somewhere in these Sample Reports Directories.

Cant type System.IO.Ports shorthand

I have the following code but still have to type full path to use the System.IO.Ports namespace even though I have the using clause in place. Am I missing something in my reference list?
The = new SerialPort returns a Error 5 'SerialPort' is a 'namespace' but is used like a 'type'
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO.Ports;
namespace SerialPort
{
public partial class Form1 : Form
{
System.IO.Ports.SerialPort counter = new SerialPort("COM5");
public Form1()
{
InitializeComponent();
}
Thanks
You're declaring a namespace of SerialPort. Don't do that. That's what's causing the problem.
All you've got to do is change the namespace, and you'll be fine. You could use an alias as per Honza's request, but I think the code would be clearer to everyone if you just renamed the namespace.
It's because your namespace carries the same name. Either rename your namespace or use an alias for the serial port, like this:
using SP = System.IO.Ports.SerialPort
And then you can use
SP counter = new SP("COM5");
But as Jon suggested, renaming your namespace is a clearer solution to anyone who'll read your code.

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

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace WindowsFormsApplication3
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Form2 myform = new Form2();
myform.ShowDialog();
}
}
}
The following code should "launch" form2. They are in the same project, and have the same namespace. I don't understand. I have tried changing the .NET framework from version 4 client to version 4.
Any other ideas?
right click on Form2, chose "resolve" and pick one of the resolutions, alternatively move it to the WindowsFormsApplication3 namespace.
In my case I created form2, but renamed it later.
I found that although the name of the form changed, the name of the class remained as I originally created it.
For example:
1. Project->Add Windows Form (Setup.cs)
2. File->Save Setup.cs as... (frmSetup.cs)
3. Add code: frmSetup SetupForm = new frmSetup();
However, the actual name of the Setup class did not change with the file name, so changing my code to:
Setup SetupForm = new Setup();
So I was referencing the class name and not the file name solved the problem for me.
You can solve this issue in two wais:
You remove the namespace outside your form's class code or
Add a "using projectName" at the top of the code in your form.
Both forms should have the same namespace

Categories